2. Write Test Code

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

1. Get the IDriver Interface

To create test code, you use the SmartBear.TestLeft.IDriver interface. The interface provides access to the TestLeft engine. It also provides access to the hierarchy of objects in tested applications and allows performing various test-related operations. To learn more about the interface, see About Driver Objects.

Our project is created from the TestLeft MSTest project template. The project’s UnitTestClassBase class that we use to create out test has the Driver property that returns the needed interface:

IDriver interface

Click the image to enlarge it.

Note that to get the interface, we use the LocalDriver class. This way we access the TestLeft test engine on the local computer.

To access a TestLeft test engine running on a remote computer, use the RemoteDriver class.

If you create a test from scratch, without using TestLeft templates, get the interface in your code manually. See About Driver Objects.

2. Simulate User Actions

  1. In this tutorial, we will work with the Notepad application. Let’s create code that will run Notepad at the beginning of the test:


    IProcess process = Driver.Applications.Run("notepad.exe");

    Visual Basic .NET

    Dim process As IProcess = Driver.Applications.Run("notepad.exe")

    We use the IDriver.Applications.Run method to run Notepad and keep a reference to the launched application in the process variable. To learn more about running a tested application from tests, see Running Applications From Tests.

    Note that we do not specify the path to the Notepad executable because its location is set in the PATH environment variable. To start your tested application, you may need to specify the path to it. To make your tests portable, use environment variables in the path (like %PROGRAMFILES% or %APPDATA%).

  2. Create code that will access Notepad’s window and simulate text input to it:

    Get an object

    The IDriver interface has a number of methods you can use to get access to an object in a tested application. However, the easiest way to create code that will get the needed object is to use TestLeft UI Spy:

    • If TestLeft UI Spy is hidden, select View | TestLeft UI Spy from the Visual Studio main menu to make it visible.

    • Click on the TestLeft UI Spy toolbar and configure the code generation settings:

      Selecting the language to generate code

      Click the image to enlarge it.

      The generated code will search for test objects starting from the application's main window. To get the tested application, it will use the process variable, which we declared earlier.

    • Pick the edit box on the screen with the Pick Object tool:

      Selecting the text edit control with TestLeft UI Spy

      Click the image to enlarge it.

      TestLeft UI Spy will capture the control and select it in the object tree.

      Note: It may take several seconds for TestLeft to capture the needed object and to display a red frame around the object. Please wait.
    • Right-click the object in the object tree and then click Copy Identification.

      Copying Object Identity

      Click the image to enlarge it.

      The TestLeft UI Spy will generate code to access the needed object and will copy the code to the clipboard.

    • Insert the code from the clipboard to your test method:


      IWinTextEdit edit = process.Find<ITopLevelWindow>(new WindowPattern(){
          WndClass = "Notepad"
      }).Find<IWinTextEdit>(new WindowPattern(){
          WndClass = "Edit"

      Visual Basic .NET

      Dim edit As IWinTextEdit = process.Find(Of ITopLevelWindow)(new WindowPattern() With {
                  .WndClass = "Notepad" 
              }).Find(Of IWinTextEdit)(new WindowPattern() With {
                  .WndClass = "Edit" 

    Note: Another approach to getting objects in a tested application is to use object models. Object models are classes that describe the object hierarchy in the tested application and that you map to an actual tested application to easily access the needed objects. See Mapping Test Objects.

    TestLeft includes a Web Orders Application Model sample project that uses object models. You can examine it to learn how to create object models and use them in tests.

    To work with processes, windows, and controls of tested applications, TestLeft uses test interfaces. TestLeft casts a window or control in a tested application to a proper interface and then uses the interface’s methods and properties to simulate user actions over the window or control. Which interface TestLeft uses to work with an object depends on the object class.

    In our case, TestLeft recognizes Notepad’s edit box as an IWinTextEdit interface. This interface is declared in the SmartBear.TestLeft.TestObjects.Win namespace, so we should add this namespace to our code.


    using SmartBear.TestLeft;
    using SmartBear.TestLeft.TestObjects;
    using SmartBear.TestLeft.TestObjects.Win;

    Visual Basic .NET

    Imports SmartBear.TestLeft
    Imports SmartBear.TestLeft.TestObjects
    Imports SmartBear.TestLeft.TestObjects.Win

    Simulate user actions on an object

    You can use an object’s properties and methods to simulate user actions over the control. Visual Studio’s IntelliSense shows the list of available properties and methods:

    Extended methods and properties

    Click the image to enlarge it.

    To simulate a mouse click in Notepad’s text editor, we will call the object’s Click method. To simulate text input, we will call the object’s SetText method:


    string inputText = "test";

    Visual Basic .NET

    Dim inputText As String = "test"

3. Check Object State

In your tests, you often may need to check the state of an object to make sure that the application under test functions properly. To do this, you can create code that compares object properties against the expected values.

In this tutorial, we will create code that will check whether the text input has been simulated successfully. We will compare the actual text that Notepad’s text editor contains (returned by the wText property) against the expected text:


Assert.AreEqual(inputText, edit.wText);

Visual Basic .NET

Assert.AreEqual(inputText, edit.wText)

If the assertion fails, the test will stop and all further test instructions will not be executed.

4. Save Test Results

When a TestLeft test is running, TestLeft saves information on all the test actions it simulates over the tested application (mouse clicks, button presses and so on) to the test log. If the test fails, you can view the test log and learn what happened during the test run and what caused the test failure.

You can post custom messages to the TestLeft test log. Let us post a screenshot of Notepad’s edit box and a custom warning message to the TestLeft test log:


Driver.Log.Screenshot(edit, "Notepad's edit box screenshot");
Driver.Log.Warning("A warning message");

Visual Basic .NET

Driver.Log.Screenshot(edit, "Notepad's edit box screenshot")
Driver.Log.Warning("A warning message")

By default, TestLeft stores its log to a temporary folder and deletes it after the test run is over. You can command TestLeft to export test results to an external file (MHT or HTML) to view and analyze it after the test run.

Note: The TestLeft MSTest project template has a method that exports the TestLeft log automatically to the default MSTest results folder.

If a test passes, MSTest clears the folder. You can view the test results only if the test fails.

To view the test results after the test passes, you can do one of the following:

  • Export the test log to another location (the approach described in this tutorial).

— or —

  • Configure the test settings in your project to command MSTest not to clear the test results.

To view our TestLeft test results, we will add instructions to our test method that will store the test log to the current user’s folder:


using System.IO;

string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Now.ToString("MM_dd_yyyy_H_mm_ss") );

Driver.Log.Save(logPath, Log.Format.Html);

Visual Basic .NET

Imports System.IO

Dim logPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateAndTime.Now.ToString("MM_dd_yyyy_H_mm_ss") )

driver.Log.Save(logPath, Log.Format.Html)

You can find more information on posting custom messages to the TestLeft test log and exporting the log in the Working With TestLeft Test Logs section.

Final Code

The overall TestLeftTest class code is as follows:


Prev     Next

See Also

Your First Test With TestLeft - .NET and .NET Core Tutorial
Working With TestLeft Test Logs
Running Applications From Tests

Highlight search results