Applies to TestLeft 4.30, last modified on November 21, 2019

What Methods and Properties Are Accessible

TestLeft provides a number of basic test methods and properties common for all test objects –

For .NET, they are implemented by the IObject interface. In the TestLeft UI Spy, they are listed under the IObject category.

For Java, they are implemented by the TestObject interface. In the TestLeft UI Spy, they are listed under the TestObject category.

In addition, it exposes the following members –

  • Native members - Internal methods, fields and properties of tested applications.
  • Common members - Some methods and properties specific to different types of test objects. These methods and properties depend on the object type: Web, WPF, Java, and so on.

In the UI Spy panel, these members are listed under the appropriate category at the bottom of the list of the supported interfaces:

Properties and methods available through the IObject interface (.NET)

Click the image to enlarge it.

Properties and methods available through the TestObject interface (Java)

Click the image to enlarge it.

Working With Exposed Methods, Fields and Properties

You call exposed methods and properties in a specific way, you cannot call them using a typical object.method notation:

Method Description
GetProperty<T>(nameparameters) Returns the value of a property.
  • name - The name of the desired property.
  • parameters - Optional. Specifies an array of property parameters used to read indexed properties.
    To read the value of an “ordinary” property, omit parameters.
SetProperty(namevalueparameters) Assigns a new value to the specified property.
  • name - The name of the desired property.
  • value - The value to be set.
  • parameters - Optional. Specifies an array of property parameters for setting indexed properties.
    To set the value of an “ordinary” property, omit parameters.
CallMethod(nameparameters), and
CallMethod<T>(nameparameters)
Invoke a method. The second variant invokes a method and returns the result.
  • name - The name of the desired method.
  • parameters - An array of method parameters.
    If the desired method has no parameters, omit the parameters parameter.
Method Description
getProperty(aClasssobjects) Returns the value of a property.
  • aClass - The class of the value that the property returns.
  • s - The name of the desired property.
  • objects - Optional. Specifies an array of property parameters used to read indexed properties.
    To read the value of an “ordinary” property, omit the objects parameter.
setProperty(soobjects) Assigns a new value to the specified property.
  • s - The name of the desired property.
  • o - The value to be set.
  • objects - Optional. Specifies an array of property parameters for setting indexed properties.
    To set the value of an “ordinary” property, omit the objects parameter.
callMethod(sobjects), and
callMethod(aClasssobjects)
Invoke a method. The second variant invokes a method and returns the result.
  • aClass - The class of the value that the method returns.
  • s - The name of the desired method.
  • objects - An array of method parameters.
    If the desired method has no parameters, omit the objects parameter.

Calling Exposed Properties and Methods Using UI Spy

The TestLeft UI Spy can automatically generate code that calls the needed properties and methods. Select an object in the UI Spy, then right-click the needed property or method, and click Copy Member Call.

Generating native member call code in UI Spy

Click the image to enlarge it.

The UI Spy will generate code that will get the needed object in the test, and use the appropriate methods to get the selected property or method. Modify the code if needed.

Example - Desktop Application

The sample code below demonstrates how to access native members of a desktop application. It obtains the main window of an application (Visual Studio for .NET code samples, IntelliJ IDEA for Java code sample), changes the value of its Title property and calls the native method that sets the main window focus:

C#

using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;
using SmartBear.TestLeft.TestObjects.WPF;

public void NativeCLRTest()
 {
     // Get Visual Studio's main window
     IControl mainWindow = Driver.Find<IProcess>(new ProcessPattern()
     {
         ProcessName = "devenv"
     }).Find<IControl>(new WPFPattern()
     {
         ClrFullClassName = "Microsoft.VisualStudio.PlatformUI.MainWindow"
     }, 2);

     // Get the value of a native property
     string propValue = mainWindow.GetProperty<string>("Title");
     Driver.Log.Message("Title property value: " + propValue);

     // Assign a new value to the property
     mainWindow.SetProperty("Title", "New Title");

     propValue = mainWindow.GetProperty<string>("Title");
     Assert.AreEqual("New Title", propValue);
     
     //Call a native method
     int methodResult = mainWindow.CallMethod<int>("Focus");
     Driver.Log.Message("Result of Focus call: " + methodResult);
 }

Visual Basic .NET

Imports SmartBear.TestLeft
Imports SmartBear.TestLeft.TestObjects
Imports SmartBear.TestLeft.TestObjects.WPF

Public Sub NativeCLRTestVB()

    ' Get Visual Studio's main window
    Dim mainWindow As IControl = Driver.Find(Of IProcess)(New ProcessPattern() With {
        .ProcessName = "devenv"
    }).Find(Of IControl)(New WPFPattern() With {
        .ClrFullClassName = "Microsoft.VisualStudio.PlatformUI.MainWindow"
    }, 2)

    ' Get the value of a native property
    Dim propValue As String = mainWindow.GetProperty(Of String)("Title")
    Driver.Log.Message("Title property value: " & propValue)

    ' Assign a new value to the property
    mainWindow.SetProperty("Title", "New Title")

    propValue = mainWindow.GetProperty(Of String)("Title")
    Assert.AreEqual("New Title", propValue)

    ' Call a native method
    Dim methodResult As Integer = mainWindow.CallMethod(Of Integer)("Focus")
    Driver.Log.Message("Result of Focus call: " & methodResult.ToString)
End Sub

Java

import com.smartbear.testleft.*;
import com.smartbear.testleft.testobjects.*;

public void NativeCLRTestJava() throws Exception{

  // Get IntelliJ IDEA main window
  Control mainWindow = driver.find(TestProcess.class, new ProcessPattern() {{
    ProcessName = "idea";
  }}).find(Control.class, new AWTPattern() {{
    JavaFullClassName = "com.intellij.openapi.wm.impl.IdeFrameImpl";
  }}, 2);

  // Get the value of a native property
  String propValue = mainWindow.getProperty(String.class, "title");
  driver.getLog().message("Title property value: " + propValue);

  // Assign a new value to the property
  mainWindow.setProperty("title", "New Title");

  propValue = mainWindow.getProperty(String.class, "title");
  Assert.assertEquals("New Title", propValue);

  // Call a native method
  Boolean methodResult = mainWindow.callMethod(Boolean.class, "requestFocusInWindow");
  driver.getLog().message("Result of requestFocusInWindow call: " + String.valueOf(methodResult));
}

Example - Web Application

The sample code below demonstrates how to access native attributes, properties and methods of a Web application. It opens the SmartBear web site, obtains the Logo image, gets the value of its src attribute, changes the title attribute and calls the hasAttribute DOM method:

C#

using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;
using SmartBear.TestLeft.TestObjects.Web;

public void NativeWebTest()
 {
     // Launch the browser
     IWebBrowser browser = Driver.Applications.RunBrowser(BrowserType.Firefox, "https://smartbear.com");

     // Get the Logo image
     IControl image = browser.Find<IWebPage>(new WebPagePattern()
     {
         URL = "https://smartbear.com/",
     }).Find<IControl>(new WebElementPattern()
     {
         ObjectType = "Form",
         idStr = "form"
     }).Find<IControl>(new WebElementPattern()
     {
         ObjectType = "Image",
         alt = "SmartBear"
     }, 8);

     // Get the value of an HTML attribute
     string propValue = image.GetProperty<string>("src");
     Driver.Log.Message("Value of src property: " + propValue);

     // Assign a new value to the property
     image.SetProperty("title", "New Title");
     propValue = image.GetProperty<string>("title");
     Assert.AreEqual("New Title", propValue);

     // Call a native method with parameters
     bool methodResult = image.CallMethod<bool>("hasAttribute", new Object[] { "alt" });
     Driver.Log.Message("Result of hasAttribute call: " + methodResult);
 }

Visual Basic .NET

Imports SmartBear.TestLeft
Imports SmartBear.TestLeft.TestObjects
Imports SmartBear.TestLeft.TestObjects.Web

Public Sub NativeWebTestVB()
    ' Launch the browser
    Dim browser As IWebBrowser = Driver.Applications.RunBrowser(BrowserType.Firefox, "https://smartbear.com")

    ' Get the Logo image
    Dim image As IControl = browser.Find(Of IWebPage)(New WebPagePattern() With {
        .URL = "https://smartbear.com/"
    }).Find(Of IControl)(New WebElementPattern() With {
        .ObjectType = "Form",
        .idStr = "form"
    }).Find(Of IControl)(New WebElementPattern() With {
        .ObjectType = "Image",
        .alt = "SmartBear"
    }, 8)

    ' Get the value of an HTML attribute
    Dim propValue As String = image.GetProperty(Of String)("src")
    Driver.Log.Message("Value of src property: " & propValue)

    ' Assign a new value to the property
    image.SetProperty("title", "New Title")
    propValue = image.GetProperty(Of String)("title")
    Assert.AreEqual("New Title", propValue)

    ' Call a native method with parameters
    Dim methodResult As Boolean = image.CallMethod(Of Boolean)("hasAttribute", {"alt"})
    Driver.Log.Message("Result of hasAttribute call: " & methodResult.ToString)
End Sub

Java

import com.smartbear.testleft.*;
import com.smartbear.testleft.testobjects.*;
import com.smartbear.testleft.testobjects.web.*;

public void NativeWebTestJava() throws Exception{
  // Launch the browser
  WebBrowser browser = driver.getApplications().runBrowser(BrowserType.Firefox, "https://smartbear.com");

  // Get the Logo image
  Control image = browser.find(WebPage.class, new WebPagePattern() {{
    URL = "https://smartbear.com/";
  }}).find(Control.class, new WebElementPattern() {{
    ObjectType = "Form";
    idStr = "form";
  }}).find(Control.class, new WebElementPattern() {{
    ObjectType = "Image";
    alt = "SmartBear";
  }}, 8);

  // Get the value of an HTML attribute
  String propValue = image.getProperty(String.class, "src");
  driver.getLog().message("Value of src property: " + propValue);

  // Assign a new value to the property
  image.setProperty("title", "New Title");
  propValue = image.getProperty(String.class, "title");
  Assert.assertEquals("New Title", propValue);

  // Call a native method with parameters
  Boolean methodResult = image.callMethod(Boolean.class, "hasAttribute", new Object[] {"alt"});
  driver.getLog().message("Result of hasAttribute call: " + String.valueOf(methodResult));
}

See Also

Creating TestLeft Tests
Calling "Native" Properties and Methods
Verifying Object Properties

Highlight search results