About Driver Objects

Applies to TestLeft 15.40, last modified on March 17, 2022

To work with the TestLeft test engine on your local computer, you use the LocalDriver object. To work with the test engine on a remote computer, you use the RemoteDriver object.

Creating a LocalDriver Object

From Template (for Visual Studio Only)

If you use Visual Studio and have created your project by using one of the following templates, you already have one instance of the LocalDriver object:

  • TestLeft MSTest Project

    The LocalDriver object is returned by the UnitTestClassBase.Driver property.

  • TestLeft NUnit Project

    The LocalDriver object is returned by the TestFixtureBase.Driver property.

  • TestLeft xUnit Project

    The LocalDriver object is returned by the TestFixture.Driver property.

Thus, you do not need to create a LocalDriver object manually. You can just use the Driver property instead.

C#

Driver.Applications.Run(@"C:\Orders\Orders.exe");

Visual Basic .NET

Driver.Applications.Run("C:\Orders\Orders.exe")

From Scratch

If you created your project from scratch, then you need to perform a number of preliminary actions prior to creating and using the object.

  1. Add a reference to the TestLeft library to your project.

    For .NET

    You can add the TestLeft library to your project either by using NuGet, or manually.

    To learn how to add it by using NuGet, see Install TestLeft Package via NuGet.

    To add the library manually, locate it at <TestLeft>\API\dotNET\SmartBear.TestLeft.dll.

    For Java

    The library is located at <TestLeft>\API\Java\TestLeft-15.40.jar.

  2. Add the needed packages (if you use Java) or namespaces (if you use .NET) to the module that contains your test code:

    C#

    using SmartBear.TestLeft;

    Visual Basic .NET

    Imports SmartBear.TestLeft

    Java

    import com.smartbear.testleft.*;
  3. Create an instance of the LocalDriver object by calling its constructor function.

    The constructor has one optional parameter that defines the port number of the test engine. If this parameter is omitted, the default port number (2377) is used. We recommend that you skip this parameter and use the default port number.

    C#

    int port = 2377;
    IDriver driver = new LocalDriver(port);

    Visual Basic .NET

    Dim port As Integer = 2377
    Dim driver As New LocalDriver(port)

    Java

    int port = 2377;
    Driver driver = new LocalDriver(port);

Creating a RemoteDriver Object

Before using the RemoteDriver object, you need to create its instance first. The procedure is similar to creating the LocalDriver object. However, the RemoteDriver constructor has more parameters that define the remote computer:

  1. Add a reference to the TestLeft library to your project.

    For .NET

    You can add the TestLeft library to your project either by using NuGet, or manually.

    To learn how to add it by using NuGet, see Install TestLeft Package via NuGet.

    To add the library manually, locate it at <TestLeft>\API\dotNET\SmartBear.TestLeft.dll.

    For Java

    The library is located at <TestLeft>\API\Java\TestLeft-15.40.jar.

  2. Add the needed packages (if you use Java) or namespaces (if you use .NET) to the module that contains your test code:

    C#

    using SmartBear.TestLeft;

    Visual Basic .NET

    Imports SmartBear.TestLeft

    Java

    import com.smartbear.testleft.*;
  3. Create an instance of the RemoteDriver object by calling its constructor function. The constructor has four parameters:

    • host - specifies the host name or IP address of the remote computer,

    • userName - denotes the user name in the format "Domain\Username",

    • password - denotes the user password,

    • port - defines the port number of the test engine on the remote computer. This parameter is optional. If it is omitted, the default port number (2377) is used. We recommend that you skip this parameter and use the default port number.

    C#

    string host = "10.0.50.95"; // IP or host name
    string user = "domain\\user";
    string password = "password";
    int port = 2377;
    IDriver driver = new RemoteDriver(host, user, password, port);

    Visual Basic .NET

    Dim host As String = "10.0.50.95" ' IP or host name
    Dim user As String = "domain\user"
    Dim password As String = "password"
    Dim port As Integer = 2377
    Dim driver As New RemoteDriver(host, user, password, port)

    Java

    String host = "10.0.50.95"; // IP or host name
    String user = "domain\\user";
    String password = "password";
    int port = 2377;
    Driver driver = new RemoteDriver(host, user, password, port);

Creating Driver Objects via UI Spy

If you use TestLeft UI Spy to generate test code, you can command it to create a new LocalDriver or RemoteDriver object. To do this, set the root object to Driver in the Code Generation Settings dialog.

If you already have a LocalDriver or RemoteDriver object in your test, you can command UI Spy to use it in the generated code. To do this, in the Code Generation Settings dialog, set the root object setting to Process / Browser and then enter the name of the variable that holds your object in the test.

Using Driver Objects

The LocalDriver and RemoteDriver objects have the same set of members. They are used to launch applications and web browsers, get access to application controls, manage test engine options and so on. The difference between the objects is the target machine - LocalDriver interacts with the test engine on a local machine, whereas RemoteDriver interacts with the test engine on a remote machine.

Member Description
Find and TryFind methods Search for an object in tested applications. See Object Identification.
FindAll Searches for a sequence of objects in tested applications. See Searching for Test Objects.
Applications property Launches tested applications from TestLeft tests. See Running Applications From Tests.
Desktop property Provides access to the computer's desktop. Use this property to capture desktop screenshots, get mouse coordinates, the screen width and height and so on.
Log property Provides access to the TestLeft test log. See Working With TestLeft Test Logs.
Options property Provides access to test engine options. See TestLeft Options.
Method Description
find and tryFind Search for an object in tested applications. See Object Identification.
findAll Searches for a sequence of objects in tested applications. See Searching for Test Objects.
getApplications Launches tested applications from TestLeft tests. See Running Applications From Tests.
getDesktop Provides access to the computer's desktop. Use this property to capture desktop screenshots, get mouse coordinates, the screen width and height and so on.
getLog Provides access to the TestLeft test log. See Working With TestLeft Test Logs.
getOptions Provides access to test engine options. See TestLeft Options.

Example

The code below demonstrates how to work with the test engine. It creates a LocalDriver object, connects to the test engine on a local computer, launches an application and simulates several user actions:

C#

using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;
  …
public void LocalTest()
{
  // Creates a LocalDriver object
  IDriver driver = new LocalDriver();

  // Launches Notepad
  // We do not specify the path to Notepad as it is set in the PATH environment variable
  IProcess notepad = driver.Applications.Run("notepad.exe");

  // Gets Notepad's edit box
  ITextEdit edit = notepad.Find<ITopLevelWindow>(new WindowPattern()
  {
    WndClass = "Notepad"
  }).Find<ITextEdit>(new WindowPattern()
  {
    WndClass = "Edit"
  });

  // Simulates user actions
  edit.Click();
  edit.SetText("Test");

  …
}

Visual Basic .NET

Imports SmartBear.TestLeft
Imports SmartBear.TestLeft.TestObjects
  …
Public Sub LocalTest()

  ' Creates a LocalDriver object
  Dim driver As New LocalDriver()

  ' Launches Notepad
  ' We do not specify the path to Notepad as it is set in the PATH environment variable
  Dim notepad As IProcess = driver.Applications.Run("notepad.exe")

  ' Gets Notepad's edit box
  Dim edit As ITextEdit = notepad.Find(Of ITopLevelWindow)(New WindowPattern() With {
      .WndClass = "Notepad"
  }).Find(Of ITextEdit)(New WindowPattern() With {
      .WndClass = "Edit"
  })

  ' Simulates user actions
  edit.Click()
  edit.SetText("Test")

  …

End Sub

Java

import com.smartbear.testleft.Driver;
import com.smartbear.testleft.LocalDriver;
import com.smartbear.testleft.testobjects.*;
  …
@Test
public void LocalTest() throws Exception{

  // Creates a LocalDriver object
  Driver driver = new LocalDriver();

  // Launches Notepad
  // We do not specify the path to Notepad as it is set in the PATH environment variable
  TestProcess notepad = driver.getApplications().run("notepad.exe");

  // Gets Notepad edit box
  TextEdit edit = (TextEdit) notepad.find(TopLevelWindow.class, new WindowPattern() {{
    WndClass = "Notepad";
  }}).find(TextEdit.class, new WindowPattern() {{
    WndClass = "Edit";
  }});

  // Simulates user actions
  edit.click();
  edit.setwText("Test");

  …

}

To learn how to use the RemoteDriver object, see Running TestLeft Tests on Remote Computers.

See Also

Creating TestLeft Tests
Connecting to Test Engine

Highlight search results