Addressing Objects in Mobile Applications

Applies to TestComplete 14.80, last modified on May 04, 2021

To simulate user actions over a tested mobile application, locate the needed object in the application first.

Currently, Name Mapping is not supported.

Requirements and prerequisites

Before exploring the tested application and locating tested objects, make sure that all requirements and prerequisites are met:

Addressing objects by their unique identifier

The easiest way to locate objects in your tested mobile application is to use the unique search expression that TestComplete provides for each object it recognizes in your tested mobile application. The Object Browser and Object Spy of TestComplete show the list of available search expressions (if several expressions are available):

Viewing the object's name in Object Spy

Click the image to enlarge it.

You can copy the expression to the clipboard and then insert it into your test.

Addressing objects in script tests

To address a tested object, you can use various FindElement methods:

For example, the code snippet below shows how to use the FindElementById method to get an object in a tested mobile application:

JavaScript

function Test()
{

  …
  var device = Mobile.Device("MyDevice");
  var list = device.FindElementById("smartbear.example.orders:id/listView1");
  …

}

Python

def Test():
  …
  device = Mobile.Device("MyDevice")
  list = device.FindElementById("smartbear.example.orders:id/listView1")
  …

VBScript

Sub Test
  …
  Set device = Mobile.Device("MyDevice")
  Set list = device.FindElementById("smartbear.example.orders:id/listView1")
  …

End Sub

DelphiScript

procedure Test();
var device, list;
begin
  …
  device := Mobile.Device('MyDevice');
  list := device.FindElementById('smartbear.example.orders:id/listView1');
  …

end;

C++Script, C#Script

function Test()
{

  …
  var device = Mobile["Device"]("MyDevice");
  var list = device["FindElementById"]("smartbear.example.orders:id/listView1");
  …

}

To pause the test run until the needed object becomes available, you can use various WaitElement methods:

The code snippet below shows how to pause the test run until a tested object with the specified ID becomes available:

JavaScript

function Test()
{

  …
  var device = Mobile.Device("MyDevice");
  var list = device.WaitElementById("smartbear.example.orders:id/listView1", 3000);
  …

}

Python

def Test():
  …
  device = Mobile.Device("MyDevice")
  list = device.WaitElementById("smartbear.example.orders:id/listView1", 3000)
  …

VBScript

Sub Test
  …
  Set device = Mobile.Device("MyDevice")
  Set list = device.WaitElementById("smartbear.example.orders:id/listView1", 3000)
  …

End Sub

DelphiScript

procedure Test();
var device, list;
begin
  …
  device := Mobile.Device('MyDevice');
  list := device.WaitElementById('smartbear.example.orders:id/listView1', 3000);
  …

end;

C++Script, C#Script

function Test()
{

  …
  var device = Mobile["Device"]("MyDevice");
  var list = device["WaitElementById"]("smartbear.example.orders:id/listView1", 3000);
  …

}

To get all tested objects that match a search condition, you can use various FindElements methods:

The code snippet below shows how to get all objects with the specified class name:

JavaScript

function Test()
{
  var items = Mobile.Device("MyDevice").FindElementsByClassName("android.widget.TwoLineListItem");
  for (let i = 0; i < items.length; i++)
  {
    …
  }
}

Python

def Test():
  items = Mobile.Device("MyDevice").FindElementsByClassName("android.widget.TwoLineListItem");
  for i in range (0, len(items) - 1):
    …

VBScript

Sub Test
  items = Mobile.Device("MyDevice").FindElementsByClassName("android.widget.TwoLineListItem")
  For i = 0 To UBound(items)
    …
  Next
End Sub

DelphiScript

procedure Test();
var items, i;
begin
  items := Mobile.Device('MyDevice').FindElementsByClassName('android.widget.TwoLineListItem');
  for i := 0 to VarArrayHighBound(items, 1) do
  begin
    …
  end;
end;

C++Script, C#Script

function Test()
{
  var items = Mobile["Device"]("MyDevice")["FindElementsByClassName"]("android.widget.TwoLineListItem");
  for (let i = 0; i < items["length"]; i++)
  {
    …
  }
}

Addressing objects in keyword tests

The easiest way to get the needed objects in keyword tests is to select the object visually in the Mobile Screen window when configuring an operation in the test:

Selecting an object from a keyword test operation

Click the image to enlarge it.

As an alternative, do the following:

  1. Locate the needed object in the tested application’s object hierarchy. You can do it either manually in the Object Browser, or by selecting the object visually in the Mobile Screen window with the Object Spy:

    Selecting the object with the Object Spy

    Click the image to enlarge it.

  2. Copy the search expression that TestComplete provides for the object:

    Copying object identifier in Object Browser

    Click the image to enlarge it.

  3. In your keyword test, add the operation you want to run against the object (for example, the On-Screen Action operation), and when configuring the operation, use the copied expression to specify the target object.

    Specifying a tested object in a keyword test

    Click the image to enlarge it.

See Also

About Mobile Tests

Highlight search results