Verifying Object Properties

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

In your test, most likely, you need to check whether an object’s property meets a specific condition. In TestLeft tests, you can do this by using assertions of your Unit testing framework or the if statement. Typically, assertions perform a comparison and throw an exception when it fails. Therefore, your test stops and is marked as failed. The if statement, in its turn, only checks the object’s state and performs the desired actions depending on a result. You can use it to post a message to the test log and continue the execution. This topic explains how to use both of these approaches.

Using Assert Class

Usually, Unit testing frameworks provide various assertion classes. Assertion methods return true if an assertion passes and false if it fails. When an assertion failed, the test is stopped and an exception is thrown. It makes the test marked as failed. For the full description of the class and its methods that your Unit testing framework provides, see the documentation for your framework.

To add an assertion to your TestLeft test, add a call of the desired method to your code. For example, the following code verifies whether the notepad window is visible on the screen.

C#

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

ITopLevelWindow notepadWnd = notepad.Find<ITopLevelWindow>(new WindowPattern()
{
  WndClass = "Notepad"
});

// Verify whether the notepad window is visible on the screen
Assert.AreEqual(true, notepadWnd.VisibleOnScreen);

Visual Basic .NET

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

Dim notepadWnd As ITopLevelWindow = notepad.Find(Of ITopLevelWindow)(New WindowPattern() With {
        .WndClass = "Notepad"
})

' Verify whether the notepad window is visible on the screen
Assert.AreEqual(True, notepadWnd.VisibleOnScreen)

Java

// Run notepad
TestProcess notepad = driver.getApplications().run("notepad.exe");

TopLevelWindow notepadWnd = notepad.find(TopLevelWindow.class, new WindowPattern() {{
    WndClass = "Notepad";
}});

// Verify whether the notepad window is visible on the screen
Assert.assertEquals(true, notepadWnd.getVisibleOnScreen());

Using If Statement

As an alternative, to verify the object state, you can use the if statement. In this case, if verification fails, the test does not throw an exception and continues running.

You can also use the if statement to specify the actions the test will perform after verification fails or passes. For example, you can post the verification results to the test log.

Note: By default, TestLeft does not store the test log after the test run is over. To learn how to export the test log to an external file, see Saving Test Logs.

The following code snippet verifies the text entered in the notepad window and posts an error message to the test log if it fails.

C#

// Enter text into the edit box
edit.Keys("Hello World!!")

// Check whether the text is correct
if (edit.wText != "Hello World!") {
  // Post an error message to the test log if the text is not correct
  Driver.Log.Error("Entered text is not correct");
}
...
// Exports the test log
Driver.Log.Save(logPath, Log.Format.Html);

Visual Basic .NET

' Enter text into the edit box
edit.Keys("Hello World!!")

' Check whether the text is correct
If edit.wText <> "Hello World!" Then
  ' Post an error message to the test log if the text is not correct
  Driver.Log.Error("Entered text is not correct")
End If
...
' Exports the test log
Driver.Log.Save(logPath, Log.Format.Html)

Java

// Enter text into the edit box
edit.keys("Hello World!!");

// Check whether the text is correct
if (edit.getwText() != "Hello World!")
{
  // Post an error message to the test log if the text is not correct
  driver.getLog().error("Entered text is not correct");
}
...
// Exports the test log
driver.getLog().save(logPath, Log.Format.Html);

See Also

Creating TestLeft Tests
Saving Test Logs

Highlight search results