Checking the Action Bar Items' Type

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

An action bar contains two groups of elements: navigation items and menu items. TestComplete works with different types of navigation items (lists and tabs) in the same way. However, you may need to know whether a menu item is checkable or not in order to perform certain actions. This topic explains how you can do this from tests.

Note: Besides navigation items and menu items, an action bar can contain other controls (for example, toggle buttons, switches and so on). TestComplete does not recognize them as part of the action bar. To work with these controls, you need to find them in the Object Browser. To learn how to do this, see Addressing Objects in Android Open Applications.
Checking Whether an Item Is Checkable in Scripts

The Android ActionBar object, which provide scripting access to various kinds of action bar controls, contain special properties that let you determine the action bar control type:

This property has the Item parameter, which specifies the desired action bar menu item (see Addressing Action Bar Items).

The property returns False for standard item.

The following example iterates through the action bar items, determines the type of each item and posts it to the test log.

JavaScript, JScript

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

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

  // Obtain an action bar
  var actionbarObj = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar");
  
  LogItemTypes(ActionBarObj);
}

function LogItemTypes(ActionBarObj)
{
  for (var i = 0; i < ActionBarObj.wItemCount-1; i++)
  {
    if (ActionBarObj.wIsItemCheckable(i))
    {
      Log.Message("The action bar menu item " + i +
                  " (" + ActionBarObj.wItemText(i) + ") is checkable.");

    }
    else
    {
      Log.Message("The action bar menu item " + i +
                  " (" + ActionBarObj.wItemText(i) + ") is not checkable.");
    }
  }
}

Python

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

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

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

def LogItemTypes(ActionBarObj):
  for i in range(0, ActionBarObj.wItemCount-1):
    if ActionBarObj.wIsItemCheckable[i]:
      Log.Message("The action bar menu item " + i + \
                  " (" + ActionBarObj.wItemText[i] + ") is checkable.")
    else:
      Log.Message("The action bar menu item " + i + \
                  " (" + ActionBarObj.wItemText[i] + ") is not checkable.")

VBScript

Sub Main
  Dim app, actionbarObj

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

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

  ' Obtain an action bar
  Set actionbarObj = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")
  
  LogItemTypes(actionbarObj)
End Sub 

Sub LogItemTypes (ActionBarObj)
  Dim i

  For i = 0 To ActionBarObj.wItemCount-1
    If ActionBarObj.wIsItemCheckable(i) Then 
      Log.Message("The action bar menu item " & i & _
                  " (" & ActionBarObj.wItemText(i) & ") is checkable.")
    Else 
      Log.Message("The action bar menu item " & i & _
                  " (" & ActionBarObj.wItemText(i) & ") is not checkable.")
    End If 
  Next 
End Sub 

DelphiScript

procedure LogItemTypes(ActionBarObj);
var
  i :OleVariant;
begin
  for i := 0 to ActionBarObj.wItemCount-1 do 
  begin
     if (ActionBarObj.wIsItemCheckable(i)) then 
      Log.Message('The action bar menu item ' + i +
                  ' (' + ActionBarObj.wItemText(i) + ') is checkable.')
     else
      Log.Message('The action bar menu item ' + i +
                  ' (' + ActionBarObj.wItemText(i) + ') is not checkable.')
  end;
end;

function Main();
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
  actionbarObj := app.RootLayout('').Layout('action_bar_container').ActionBar('action_bar');
  
  LogItemTypes(actionbarObj);
end;

C++Script, C#Script

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

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

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

function LogItemTypes(ActionBarObj)
{

  for (var i = 0; i < ActionBarObj["wItemCount"]-1; i++)
  {
    if (ActionBarObj["wIsItemCheckable"](i))
    {
      Log["Message"]("The action bar menu item " + i +
                  " (" + ActionBarObj["wItemText"](i) + ") is checkable.");
    }
    else
    {
      Log["Message"]("The action bar menu item " + i +
                  " (" + ActionBarObj["wItemText"](i) + ") is not checkable.");
    }
  }
}

Simulating Actions From Keyword Tests

This topic explains how to check the type of the action bar items in scripts. You can use the described properties in keyword tests too. To do this, use the On-Screen Action or the Call Object Method operations.

See Also

Working With Android Action Bar Controls
Checking the Action Bar Items' State

Highlight search results