Addressing Objects in Mobile Applications

Applies to TestComplete 15.46, last modified on December 01, 2022

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

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 using name mapping and aliases

You may find that the unique identifier that the Object Browser or Object Spy generates for test objects may be too long and not convenient to use in tests. Instead, you can assign custom names to tested objects. This is called name mapping: TestComplete adds the object to the Name Mapping repository, assigns a meaningful name — alias — to the object and search criteria that will be used to locate the object in the tested application during the test run:

A mobile object in the Name Mapping repository

Click the image to enlarge it.

You can change the assigned alias and criteria in the way you like.

In tests, you use the assigned custom name to address the objects:

Locating objects in a mobile application by using name mapping

Click the image to enlarge it.

Addressing objects without name mapping and aliases

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++)
  {
    …
  }
}

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:

Selecting an object from a keyword test operation

Click the image to enlarge it.

As an alternative, you can 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.

The test will use the search expression to address the objects:

Locating objects in a mobile application without name mapping

Click the image to enlarge it.

See Also

About Mobile Tests

Highlight search results