Checking the State of Toolbar and Navigation Bar Items

Applies to TestComplete 14.40, last modified on April 22, 2021

When testing applications that contain toolbar or navigation bar controls, you often need to check the state of items. For example, you may need to check whether a button is enabled before touching it. The iOS ToolBar object provides a number of properties that let you perform such checks. Below, you will find information on how to perform some common checks. Note that you can write verification code manually, or you can make use of property checkpoints.

Checking if an Item Is Enabled

Items can be enabled or disabled, depending on the application’s state. For example, the tested application is a web browser, and its toolbar contains the Refresh, Back and Forward items. If there is no previous page, the "Back" item is disabled. Since it is possible to perform actions only over enabled controls, TestComplete will post an error message to the test log if you try to simulate an action over a disabled item. You can avoid these errors if you first check the item’s state and simulate an action only if it is enabled. To check whether an item is enabled or disabled, you can use the wItemEnabled extended property.

The following example simulates a touch on the first item on an application’s toolbar, if it is enabled:

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();
  
  // Check if the item is enabled
  if (toolbar.wItemEnabled(0))
    // Touch the item
    toolbar.TouchItem(0);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the Toolbar object 
  p = Mobile.Device().Process("SampleApp")
  toolbar = p.Window().Toolbar()
  
  # Check if the item is enabled
  if toolbar.wItemEnabled[0]:
    # Touch the item
    toolbar.TouchItem(0)

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()
  
  ' Check if the item is enabled
  If toolbar.wItemEnabled(0) Then
    ' Touch the item
    toolbar.TouchItem(0)
  End If
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);
  
  // Check if the item is enabled
  if toolbar.wItemEnabled[0] then
    // Touch the item
    toolbar.TouchItem[0];
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"]();
  
  // Check if the item is enabled
  if (toolbar["wItemEnabled"](0))
    // Touch the item
    toolbar["TouchItem"](0);
}

Checking if an Item Is Visible

Some items can be hidden. It is possible to perform actions only over visible UI elements, so before simulating an action, you may need to check whether the desired item is visible.

To do this, use the wItemVisible property. This property returns True if the specified item is visible and False otherwise. The example below demonstrates how you can use the wItemVisible property:

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();
  
  // Check if the item is visible
  if (toolbar.wItemVisible(0))
    // Touch the item
    toolbar.TouchItem(0);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the Toolbar object 
  p = Mobile.Device().Process("SampleApp")
  toolbar = p.Window().Toolbar()
  
  # Check if the item is visible
  if toolbar.wItemVisible[0]:
    # Touch the item
    toolbar.TouchItem(0)

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()
  
  ' Check if the item is visible
  If toolbar.wItemVisible(0) Then
    ' Touch the item
    toolbar.TouchItem(0)
  End If
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);
  
  // Check if the item is visible
  if toolbar.wItemVisible(0) then
    // Touch the item
    toolbar.TouchItem(0);
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"]();
  
  // Check if the item is visible
  if (toolbar["wItemVisible"](0))
    // Touch the item
    toolbar["TouchItem"](0);
}

Checking the State of a Switch Control

Toolbars and navigation bars may contain switches. It is possible to check the state of a switch before performing actions over toolbar items.

To check the state of a switch control, use the wSwitchState property of a toolbar object. This property returns True if the specified switch is turned on and False otherwise. The example below demonstrates how you can use the wSwitchState property:

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();
  
  // Check if the item is enabled
  if (toolbar.wSwitchState(3))
    // Touch the item
    toolbar.TouchItem(0);
}

Python

def Test():
  # Select the mobile device
  Mobile.SetCurrent("iPhone")
  # Obtain the Toolbar object 
  p = Mobile.Device().Process("SampleApp")
  toolbar = p.Window().Toolbar()
  
  # Check if the item is enabled
  if toolbar.wSwitchState[3]:
    # Touch the item
    toolbar.TouchItem(0)

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()
  
  ' Check if the item is enabled
  If toolbar.wSwitchState(3) Then
    ' Touch the item
    toolbar.TouchItem(0)
  End If
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);
  
  // Check if the item is enabled
  if toolbar.wSwitchState(3) then
    // Touch the item
    toolbar.TouchItem(0);
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"]();
  
  // Check if the item is enabled
  if (toolbar["wSwitchState"](3))
    // Touch the item
    toolbar["TouchItem"](0);
}

Simulating Actions in Keyword Tests

To check the state of the tool and navigation bar items from keyword tests, use the On-Screen Action or Call Object Method operation to access the properties described above. See Getting and Setting Object Property Values.

See Also

Working With iOS Toolbar and Navigation Bar Controls
Checking the Type of Toolbar and Navigation Bar Items

Highlight search results