Checking the Action Bar Items' State

Applies to TestComplete 15.63, last modified on April 22, 2024

When testing applications that contain action bar controls, you often need to check the action bar items’ state. For example, you may need to check whether a menu items is enabled before selecting it. The Android ActionBar and other objects, which correspond to various action bar types, provide a number of properties that let you perform such checks. In the following sections, you will find information on how to perform some common checks. Note that you can write the verification code manually, or take advantage of property checkpoints.

Checking if the Item Is Enabled

Action bar menu items can be enabled or disabled, depending on the application’s state. For example, the tested application is a text editor, and the action bar contains the Cut, Copy and Delete menu items. If there is no selected text, these items will be disabled. Since it is only possible to perform actions over enabled controls, if you try to simulate an action over a disabled item TestComplete will post an error message in the test log. You can avoid these errors if you first check the item’s state and only simulate an action if it is enabled. To check whether an action bar menu item is enabled or disabled, you can use the wItemEnabled property added to the action bar object by TestComplete.

The following example simulates a touch on the Up menu item on the application’s action bar, if it is enabled:

JavaScript, JScript

function GoUp()
{
  // 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");
  
  // Check whether the desired menu item is enabled
  if (actionbar.wItemEnabled("Up"))
  {
    // Touch the item
    actionbar.TouchItem("Up");
  }
}

Python

def GoUp():
  # 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")
  
  # Check whether the desired menu item is enabled
  if actionbar.wItemEnabled("Up"):
    # Touch the item
    actionbar.TouchItem("Up")

VBScript

Sub GoUp

  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 actionbar = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")
  
  ' Check whether the desired menu item is enabled
  If actionbar.wItemEnabled("Up") then
    ' Touch the item
    Call actionbar.TouchItem("Up")
  End If
  End Sub

DelphiScript

function GoUp();
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');
  
  // Check whether the desired menu item is enabled
  if (actionbar.wItemEnabled('Up')) then 
  begin
    // Touch the item
    actionbar.TouchItem('Up');
  end;
end;

C++Script, C#Script

function GoUp()
{
  // 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");
  
  // Check whether the desired menu item is enabled
  if (actionbar["wItemEnabled"]("Up"))
  {
    // Touch the item
    actionbar["TouchItem"]("Up");
  }
}

Checking if the Item Is Visible

Some menu 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 action bar menu item is visible.

To check the item’s visibility, use the action bar object’s 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 Example()
{
  // 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");
  
  // Check whether the desired menu item is visible
  if (actionbar.wItemVisible("Item3"))
  {
    // Touch the item
    actionbar.TouchItem("Item3");
  }
}

Python

def Example():
  # 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")
  
  # Check whether the desired menu item is visible
  if actionbar.wItemVisible["Item3"]:
    # Touch the item
    actionbar.TouchItem("Item3")

VBScript

Sub Example
  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 actionbar = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")
  
  ' Check whether the desired menu item is visible
  If actionbar.wItemVisible("Item3") then
    ' Touch the item
    Call actionbar.TouchItem("Item3")
  End If
  End Sub

DelphiScript

function Example();
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');
  
  // Check whether the desired menu item is visible
  if (actionbar.wItemVisible('Item3')) then 
  begin
    // Touch the item
    actionbar.TouchItem('Item3');
  end;
end;

C++Script, C#Script

function Example()
{
  // 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");
  
  // Check whether the desired menu item is visible
  if (actionbar["wItemVisible"]("Item3"))
  {
    // Touch the item
    actionbar["TouchItem"]("Item3");
  }
}

Verifying That the Item Is Checked or Unchecked

Action bars can include checkable items, which enables or disables a specific application feature. Applications can provide several possibilities to toggle these options. So, in your tests you may need to check that the action bar item state corresponds to the actual application state.

You can determine whether an action bar menu item is checked using the wItemChecked property of the scripting object that corresponds to the application’s action bar. This property returns True if the specified item is checked and False if it is unchecked.

The example below demonstrates how you can use the wItemChecked property:

JavaScript, JScript

function Example()
{
  // 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");
  
  // Check whether the desired menu item is checked
  if (actionbar.wItemChecked("Item3"))
  {
    // Post an item’s state
    Log.Message("The item is checked");
  }
}

Python

def Example(): 
  # 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")
  
  # Check whether the desired menu item is checked
  if actionbar.wItemChecked["Item3"]: 
    # Post an item's state
    Log.Message("The item is checked")

VBScript

Sub Example

  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 actionbar = app.RootLayout("").Layout("action_bar_container").ActionBar("action_bar")
  
  ' Check whether the desired menu item is checked
  If actionbar.wItemChecked("Item3") then
    ' Post an item’s state
    Call Log.Message("The item is checked")
  End If
End Sub

DelphiScript

function Example();
var
  app, actionbar;
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');
  
  // Check whether the desired menu item is checked
  if (actionbar.wItemChecked('Item3')) then 
  begin
    // Post an item’s state
    Log.Message('The item is checked');
  end;
end;

C++Script, C#Script

function Example()
{
  // 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");
  
  // Check whether the desired menu item is checked
  if (actionbar["wItemChecked"]("Item3"))
  {
    // Post an item’s state
    Log.Message("The item is checked");
  }
}

Getting the Selected Navigation Item

Navigation items are used to switch views in the application. So, you may need to determine which item is currently selected and perform some actions depending on this. Otherwise, an error may occur or your test may perform actions over wrong controls. To determine which item is selected, use the wSelectedNavItem property of the AndroidActionBar object. It returns the zero-based index of the currently selected navigation item. The example below demonstrates how you can use the wSelectedNavItem property:

JavaScript, JScript

function Example()
{
  // 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");
  
  // Get the selected navigation item
  var itemIndex = actionbarObj.wSelectedNavItem;
  
  // Post information on which tab is selected to the test log
  switch (itemIndex)
  {
    case (0):
    {
      Log.Message("The first tab is selected");
      break;
    }
    case(1):
    {
      Log.Message("The second tab is selected");
      break;
    }
  }
}

Python

def Example():
  # 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")
  
  # Get the selected navigation item
  itemIndex = actionbarObj.wSelectedNavItem
  
  # Post information on which tab is selected to the test log
  if itemIndex == 0:
    Log.Message("The first tab is selected")
  elif itemIndex == 1:
    Log.Message("The second tab is selected")

VBScript

Sub Example

  Dim app, actionbarObj, itemIndex

  ' 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")
  
  ' Get the selected navigation item
  itemIndex = actionbarObj.wSelectedNavItem
  
  ' Post information on which tab is selected to the test log
  Select case (itemIndex)
    case (0): Log.Message("The first tab is selected")
    case (1): Log.Message("The second tab is selected")
  End Select

End Sub

DelphiScript

function Example();
var
  app, actionbarObj, itemIndex: 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');
  
  // Get the selected navigation item
  itemIndex := actionbarObj.wSelectedNavItem;
  
  // Post information on which tab is selected to the test log
  case (itemIndex) of
    0: Log.Message('The first tab is selected');
    1: Log.Message('The second tab is selected');
  end;

end;

C++Script, C#Script

function Example()
{
  // 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");
  
  // Get the selected navigation item
  var itemIndex = actionbarObj["wSelectedNavItem"];
  
  // Post information on which tab is selected to the test log
  switch (itemIndex)
  {
    case (0):
    {
      Log["Message"]("The first tab is selected");
      break;
    }
    case(1):
    {
      Log["Message"]("The second tab is selected");
      break;
    }
  }
}

Simulating Actions From Keyword Tests

This topic explains how to check the state 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' Type

Highlight search results