Selecting Action Bar Items

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

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

Simulating Touches on Items

The Android ActionBar object, which is used to work with action bar, provide a number of TouchItem actions for simulating touch on action bar items:

All of these actions have a parameter that specify the action bar item to be touched. To specify the action bar item, you can use its name or index (position within the action bar). When you specify item’s caption, it is possible to use wildcards (* and ?) or regular expressions. 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 Action Bar Items. Here is an example of how to select any item:

actionBar.TouchItem("*")

The sample script below simulates a touch on the "Item1" menu item and on the "Tab1" navigation item of an application’s action bar.

JavaScript, JScript

function TouchItem()
{
  // Select an Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain an application
  var app = Mobile.Device().Process("com.example.myapp");

  // Obtain an action bar
  var actionbar = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar");

  // Simulating touch on a menu item
  actionbar.TouchItem("Item1");

  // Simulating touch on a navigation item
  actionbar.TouchNavItem("Tab1");
}

Python

def TouchItem():
  # Select an Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain an application
  app = Mobile.Device().Process("com.example.myapp")

  # Obtain an action bar
  actionbar = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")

  # Simulating touch on a menu item
  actionbar.TouchItem("Item1")

  # Simulating touch on a navigation item
  actionbar.TouchNavItem("Tab1")

VBScript

Sub TouchItem
  Dim app, actionbar

  ' Select an Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain an application
  Set app = Mobile.Device.Process("com.example.myapp")

  ' Obtain an action bar
  Set ab = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")

  ' Simulating touch on a menu item
  Call ab.TouchItem("Item1")

  ' Simulating touch on a navigation item
  Call ab.TouchNavItem("Tab1")
End Sub

DelphiScript

function TouchItem();
var
  app, actionbar: OleVariant;
begin
  
  // Select an Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain an application
  app := Mobile.Device.Process('com.example.myapp');

  // Obtain an action bar
  actionbar := app.RootLayout('').Layout('action_bar_container').ActionBar('action_bar');

  // Simulating touch on a menu item
  actionbar.TouchItem('Item1');

  // Simulating touch on a navigation item
  actionbar.TouchNavItem('Tab1');
end;

C++Script, C#Script

function TouchItem()
{
  // Select an Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain an application
  var app = Mobile["Device"]["Process"]("com.example.myapp");

  // Obtain an action bar
  var actionbar = app["RootLayout"]("")["Layout"]("action_bar_container")["ActionBar"]("action_bar");

  // Simulating touch on a menu item
  actionbar["TouchItem"]("Item1");

  // Simulating touch on a navigation item
  actionbar["TouchNavItem"]("Tab1");
}

Checking and Unchecking Action Bar Items

Some action bar menu items represent options that can be checked or unchecked, which enables or disables a specific application feature. To check or uncheck these items from tests, you can use the CheckItem action of the Android ActionBar object. This action has the Item and Checked parameters, which specify the action bar item and its desired state - True (checked) or False (Unchecked).

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

The example below iterates through the action bar menu items and performs the CheckItem action if an item is checkable.

JavaScript, JScript

function Test()
{
  // Select an Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain an application
  var app = Mobile.Device().Process("com.example.myapp");

  // Obtain an action bar
  var actionbar = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar");

  for (var i = 0; i < actionbar.wItemCount; i++)
  {
    if (actionbar.wIsItemCheckable(i))
    {
      // Check the item
      actionbar.CheckItem(i, true);
    }
  }
}

Python

def Test():
  # Select an Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain an application
  app = Mobile.Device().Process("com.example.myapp")

  # Obtain an action bar
  actionbar = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")

  for i in range (0, actionbar.wItemCount-1): 
    if actionbar.wIsItemCheckable[i]:
      # Check the item 
      actionbar.CheckItem(i, True)

VBScript

Sub Test()
  ' Select an Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain an application
  Set app = Mobile.Device.Process("com.example.myapp")

  ' Obtain an action bar
  Set ab = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")

  For i = 0 To ab.wItemCount -1
    If (ab.wIsItemCheckable(i)) Then 
      ' Check the item
      Call ab.CheckItem(i, true)

    End If
  Next 
End Sub

DelphiScript

function Test();
var
app, actionbar, i: OleVariant;
begin
  // Select an Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain an application
  app := Mobile.Device.Process('com.example.myapp');

  // Obtain an action bar
  actionbar := app.RootLayout('').Layout('action_bar_container').ActionBar('action_bar');

  for i := 0 to actionbar.wItemCount -1 do 
  begin
    if (actionbar.wIsItemCheckable(i)) then
      // Check the item
      actionbar.CheckItem(i, true)
  end;
end;

C++Script, C#Script

function Test()
{
  // Select an Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain an application
  var app = Mobile["Device"]["Process"]("com.example.myapp");

  // Obtain an action bar
  var actionbar = app["RootLayout"]("")["Layout"]("action_bar_container")["ActionBar"]("action_bar");

  for (var i = 0; i < actionbar["wItemCount"]; i++)
  {
    if (actionbar["wIsItemCheckable"](i))
    {
      // Check the item
      actionbar["CheckItem"](i, true);
    }
  }
}

See Also

Working With Android Action Bar Controls
CheckItem Action (Android Controls)
TouchItem Method (Mobile Controls)
TouchNavItem Method (Specific to Android ActionBar Controls)

Highlight search results