So you have installed GameDriver and are ready to start testing. Great! Here's what you need to do.

Assuming you followed the installation instructions to the end, you should have something similar to the code below added to a test in Visual Studio:

using System;
using NUnit.Framework;
using gdio.unity_api;

namespace GameDriver_Test
{
    [TestFixture]
    public class UnitTest
    {
        public string testHost = "localhost";
        public string testMode = "IDE";
        bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
        
        [OneTimeSetUp]
        public void Connect()
        {
            bool connected;

            if (testMode != "standalone")
            {
                //Start Unity Editor Play Mode 
                connected = Api.WaitForGame(testHost);
            }
            else if (isWindows == true)
            {
                //Launch the Windows executable - modify this to match your executable path
                connected = Api.Launch(@"C:\Users\user\Desktop\GameDriver\Demo.exe");
            }
            //Launch the macOS executable - modify this to match your executable path
            else connected = Api.Launch(@"~/Desktop/Demo.app/Contents/MacOS/Demo");

            if (connected)
            {
                Api.EnableKeybordHooks();
                Api.EnableMouseHooks();
            }

            //Start the Game
            Api.WaitForObject("//*[@name='StartButton']");
            Api.ClickObject("//*[@name='StartButton']", Api.MouseButtons.LEFT);
        }
        
        [Test]
        public void Test1()
        {
            // Do some testing...
        }

        [Test]
        public void Test2()
        {
            // Do some more testing...            
        }
        
        [OneTimeTearDown]
        public void Disconnect()
        {
            Api.DisableKeybordHooks();
            Api.DisableMouseHooks();
            Api.Quit();
            Api.Wait(3000); // Sometimes it helps to leave some time to sync at the end
        }
    }
}


For the purpose of this article, we will implement a single [Test]. As a best practice these should be self-contained in the sense that they can run whether or not other tests in have passed; Meaning the test should include whatever steps are necessary to navigate to that point. We will use a simple example here.

First, we will load the scene, then check the status of some object to verify the scene is loaded. We do this using the WaitForObject function, and can optionally wrap that in an Assert.IsTrue function which will automatically fail the test if the scene isn't loaded:


[TestMethod]
public void Test1() // This will be our main test method, where we want to achieve some result. Be sure to give it a useful name
    {
    Api.LoadScene("Menu"); // Scenes are loaded using their object name
    
    // Use an Assert to test whether the menu loaded
    Assert.IsTrue(Api.WaitForObject("//*[@name='Load_MoveObject']"), "Menu not loaded!");

Next, we will want to perform some specific action to test the behavior of the game. This could be anything from moving the character around the scene, collecting items, locating and shooting enemies, or simply drag-and-drop objects around the UI. It all depends on your project and the goal of your tests. Keep in mind the goal, to test features or functionality that is critical to the game, and provide immediate feedback to developers if something isn't working. In this example, we will move an object around the scene, and test the final location.


//Get the current location of the Cylinder object
    Vector3 pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    Vector3 start = pos;
    
    //MouseDrag the Cylinder in a spiral, checking the location at each step to use as the next starting position.
    Api.MouseDrag(Api.MouseButtons.LEFT, pos.x, pos.y, pos.x + 180, pos.y, 30);
    Api.WaitForEmptyInput();
    Api.Wait(200);
    
    pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    Api.MouseDrag(Api.MouseButtons.LEFT, pos.x, pos.y, pos.x, pos.y + 180, 30);
    Api.WaitForEmptyInput();
    Api.Wait(200);
    
    pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    Api.MouseDrag(Api.MouseButtons.LEFT, pos.x, pos.y, pos.x - 360, pos.y, 30);
    Api.WaitForEmptyInput();
    Api.Wait(200);
    
    pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    Api.MouseDrag(Api.MouseButtons.LEFT, pos.x, pos.y, pos.x, pos.y - 360, 30);
    Api.WaitForEmptyInput();
    Api.Wait(200);
    
    pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    Api.MouseDrag(Api.MouseButtons.LEFT, pos.x, pos.y, pos.x + 360, pos.y, 30);
    Api.WaitForEmptyInput();
    Api.Wait(200);
    
    pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    Api.MouseDrag(Api.MouseButtons.LEFT, pos.x, pos.y, pos.x, pos.y + 180, 30);
    Api.WaitForEmptyInput();
    Api.Wait(200);
    
    pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    Api.MouseDrag(Api.MouseButtons.LEFT, pos.x, pos.y, pos.x - 180, pos.y, 30);
    Api.WaitForEmptyInput();
    Api.Wait(200);
    
    pos = Api.GetObjectPosition("//*[@name='Cylinder']", Api.CoordinateConversion.WorldToScreenPoint).ToVector3();
    
    //Simple output messaging to validate component locations
    Console.WriteLine("Starting Position: x=" + start.x.ToString() + ", y=" + start.y.ToString());
    Console.WriteLine("Ending Position: x=" + pos.x.ToString() + ", y=" + pos.y.ToString());
    
    //Using test assertions to validate object positions are within a specified tolerance.
    Assert.AreEqual(start.x, pos.x, 0.2f, "X coordinates aren't within 0.2 tolerance of expected value");
    Assert.AreEqual(start.y, pos.y, 0.2f, "Y coordinates aren't within 0.2 tolerance of expected value");


There you have it! We loaded a scene, moved an object in a pre-defined pattern using the previous location as a reference point, and verified the result.

Happy Testing!