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:
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>(name, parameters) |
Returns the value of a property.
|
SetProperty(name, value, parameters) |
Assigns a new value to the specified property.
|
CallMethod(name, parameters) , andCallMethod<T>(name, parameters) |
Invoke a method. The second variant invokes a method and returns the result.
|
Method | Description |
---|---|
getProperty(aClass, s, objects) |
Returns the value of a property.
|
setProperty(s, o, objects) |
Assigns a new value to the specified property.
|
callMethod(s, objects) , andcallMethod(aClass, s, objects) |
Invoke a method. The second variant invokes a method and returns the result.
|
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.
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