Selecting Toolbar and Navigation Bar Items

Applies to TestComplete 15.47, last modified on January 20, 2023

One of the most frequent actions you perform on a toolbar or navigation bar is pressing individual items. This topic explains various approaches that can be used to select the items in scripts:

Simulating Touches on Items

The iOS ToolBar and iOS NavigationBar objects, which are used to work with toolbars and navigation bars, provide a number of TouchItem actions for simulating touches on items: TouchItem, LongTouchItem, TouchItemXY and LongTouchItemXY.

All these actions have a parameter that specifies the item to be touched. To specify an item, you can use its name or index (the position within the toolbar). Note that it is possible to use wildcards (* and ?) or regular expressions to specify an item’s caption. An asterisk (*) corresponds to a string of any length (including an empty string), a question mark corresponds to any single character (including none). If you wish to specify an asterisk (*) as part of a item’s caption, you should double it, because otherwise, it will be treated as a wildcard. To specify more complicated parts of a caption, use regular expressions. For more information, see Addressing Tool Bar and Navigation Bar Items. Below is an example of how to select an item:

toolbar.TouchItem("*")

The sample script below simulates a touch on the "Item" item and then a long touch on the third item of an application’s toolbar.

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the Toolbar object
  var p = Mobile.Device().Process("SampleApp");
  var toolbar = p.Window().Toolbar();
  
  // Touch the "Item" item
  toolbar.TouchItem("Item");
  
  // Long touch the fourth item
  toolbar.LongTouchItem(3);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the Toolbar object 
  p = Mobile.Device().Process("SampleApp")
  toolbar = p.Window().Toolbar()
  
  # Touch the "Item" item
  toolbar.TouchItem("Item")
  
  # Long touch the fourth item
  toolbar.LongTouchItem(3)

VBScript

Sub Test()
  Dim p, toolbar
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the Toolbar object
  Set p = Mobile.Device.Process("SampleApp")
  Set toolbar = p.Window(0).Toolbar()
  
  ' Touch the "Item" item
  toolbar.TouchItem("Item")
  
  ' Long touch the fourth item
  toolbar.LongTouchItem(3)
End Sub 

DelphiScript

procedure Test();
var
  p, toolbar;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the Toolbar object
  p := Mobile.Device.Process('SampleApp');
  toolbar := p.Window(0).Toolbar(0);
  
  // Touch the "Item" item
  toolbar.TouchItem('Item');
  
  // Long touch the fourth item
  toolbar.LongTouchItem(3);
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the Toolbar object
  var p = Mobile["Device"].Process("SampleApp");
  var toolbar = p["Window"]()["Toolbar"]();
  
  // Touch the "Item" item
  toolbar["TouchItem"]("Item");
  
  // Long touch the fourth item
  toolbar["LongTouchItem"](3);
}

Changing the State of a Switch Control

Some items represent options that can be in the "on" or "off" state, which enables or disables certain application feature. To set the state in tests, you can use the SwitchItem action. This action has the Item and State parameters, which specify the item and its desired state - True (On) or False (Off).

To determine whether an item can be checked or unchecked, use the wIsItemSwitchable property. The wSwitchState property, in its turn, lets you get the current item state -- "On" (True) or "Off" (False).

The example below iterates through the toolbar items and performs the SwitchItem action on every switch.

JavaScript, JScript

function Test()
{
  // Select the mobile device
  Mobile.SetCurrent("iPhone");
  // Obtain the Toolbar object
  var p = Mobile.Device().Process("SampleApp");
  var toolbar = p.Window().Toolbar();
  
  // Iterate through the items
  for (var i=0; i<toolbar.wItemCount; i++)
  {
    if (toolbar.wIsItemSwitchable(i))
      // Set the control to the ON state
      toolbar.SwitchItem(i,"On")
  }
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the Toolbar object 
  p = Mobile.Device().Process("SampleApp")
  toolbar = p.Window().Toolbar()
  
  # Iterate through the items
  for i in range(0, toolbar.wItemCount):
    if toolbar.wIsItemSwitchable[i]:
      # Set the control to the ON state
      toolbar.SwitchItem(i,"On")

VBScript

Sub Test()
  Dim p, toolbar
  ' Select the mobile device
  Mobile.SetCurrent("iPhone")
  ' Obtain the Toolbar object
  Set p = Mobile.Device.Process("SampleApp")
  Set toolbar = p.Window(0).Toolbar()
  
  ' Iterate through the items
  For i = 0 To toolbar.wItemCount-1
    If (toolbar.wIsItemSwitchable(i)) Then
      ' Set the control to the ON state
      Call toolbar.SwitchItem(i, "On")
      End If 
   Next
End Sub

DelphiScript

procedure Test();
var
  i, p, toolbar;
begin
  // Select the mobile device
  Mobile.SetCurrent('iPhone');
  // Obtain the Toolbar object
  p := Mobile.Device.Process('SampleApp');
  toolbar := p.Window(0).Toolbar(0);
  
  // Iterate through the items
  for i := 0 to toolbar.wItemCount-1 do
  begin
    if toolbar.wIsItemSwitchable(i) then
      // Set the control to the ON state
      toolbar.SwitchItem(i, true)
  end;
end;

C++Script, C#Script

function Test()
{
  // Select the mobile device
  Mobile["SetCurrent"]("iPhone");
  // Obtain the Toolbar object
  var p = Mobile["Device"].Process("SampleApp");
  var toolbar = p["Window"]()["Toolbar"]();
  
  // Iterate through the items
  for (var i=0; i<toolbar["wItemCount"]; i++)
  {
    if (toolbar["wIsItemSwitchable"](i))
      // Set the control to the ON state
      toolbar["SwitchItem"](i,"On")
  }
}

Simulating Actions in Keyword Tests

To select items of tool bars and navigation bars from keyword tests, call the methods described above by using the On-Screen Action or Call Object Method operation. See Calling Object Methods.

See Also

Working With iOS Toolbar and Navigation Bar Controls
SwitchItem Action (Specific to iOS Toolbar and NavigationBar Controls)
TouchItem Method (Mobile Controls)
LongTouchItem Action (iOS Controls)

Highlight search results