Checking the Toolbar Buttons' State in Desktop Windows Applications

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

While testing toolbar controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

When testing applications that contain toolbar controls, you often need to check the toolbar buttons’ state. For example, you may need to check whether a button is enabled before selecting it. The Win32ToolBar, StripToolBar, WPFToolBar and other objects, which correspond to various toolbar 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 Button is Enabled

Toolbar buttons can be enabled or disabled, depending on the application’s state. For example, if no text is selected in the editor’s window, the Cut, Copy and Delete commands are disabled. Since it is only possible to perform actions over enabled controls, if you try to simulate an action over a disabled button TestComplete will post an error message in the test log. You can avoid these errors if you first check the buttons’s state and only simulate an action if it is enabled. To check whether a toolbar button is enabled or disabled, you can use the wEnabled property added to the toolbar object by TestComplete.

The following example clicks the Up button on the Windows Explorer toolbar until the Desktop is reached and the button becomes disabled. In order for the script to execute successfully, Explorer must be running:

JavaScript, JScript

function GoToDesktop ()
{
  var p, w, ToolBar;

  p = Sys.Process("Explorer");
  w = p.Window("*WClass", "*");
  ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1);

  while (ToolBar.wEnabled("Up"))
    ToolBar.ClickItem("Up");
}

Python

def GoToDesktop ():

  p = Sys.Process("Explorer")
  w = p.Window("*WClass", "*")
  ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1)

  while ToolBar.wEnabled["Up"]:
    ToolBar.ClickItem("Up")

VBScript

Sub GoToDesktop
  Dim p, w, ToolBar

  p = Sys.Process("Explorer")
  w = p.Window("*WClass", "*")
  ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1)

  While (ToolBar.wEnabled("Up"))
    ToolBar.ClickItem("Up")
  End While
End Sub

DelphiScript

procedure GoToDesktop;
var p, w, ToolBar : OleVariant;
begin
  p := Sys.Process('Explorer');
  w := p.Window('*WClass', '*');
  ToolBar := w.Window('WorkerW', '', 1).Window('ReBarWindow32').Window('ToolbarWindow32', '', 1);

  while (ToolBar.wEnabled['Up']) do
    ToolBar.ClickItem('Up');
end;

C++Script, C#Script

function GoToDesktop ()
{
  var p, w, ToolBar;

  p = Sys["Process"]("Explorer");
  w = p["Window"]("*WClass", "*");
  ToolBar = w["Window"]("WorkerW", "", 1)["Window"]("ReBarWindow32")["Window"]("ToolbarWindow32", "", 1);

  while (ToolBar["wEnabled"]("Up"))
    ToolBar["ClickItem"]("Up");
}

Checking if the Button is Visible

Toolbars in many applications can be customized, so that some buttons are hidden from view. It is possible to only perform actions over visible UI elements, so before simulating a toolbar action you may need to check whether the desired toolbar button is visible.

To check the button’s visibility, use the toolbar object’s wVisible property. This property returns True if the specified button is visible and False otherwise. The example below demonstrates the wVisible property usage example:

Note: This script works with the C# Orders application that resides in the following folder:

<TestComplete Samples>\Desktop\Orders\C#\bin\Release

Before running the script, you should compile the application and launch it.

JavaScript, JScript

function Main ()
{
  var p, ToolBar;

  // Obtain the Orders process and toolbar
  p = Sys.Process("Orders");
  ToolBar = p.WinFormsObject("MainForm", "*").WinFormsObject("ToolBar", "");

  // Show the "New Order" button
  ToolBar.Buttons.Item(4).Visible = true;

  if (ToolBar.wVisible(4, true))
    Log.Message ("The \"New Order\" button is visible.")
  else
    Log.Message ("The \"New Order\" button is invisible.");

  // Hide the "New Order" button
  ToolBar.Buttons.Item(4).Visible = false;

  if (ToolBar.wVisible(4, true))
    Log.Message ("The \"New Order\" button is visible.")
  else
    Log.Message ("The \"New Order\" button is invisible.");
}

Python

def Main ():

  # Obtain the Orders process and toolbar
  p = Sys.Process("Orders")
  ToolBar = p.WinFormsObject("MainForm", "*").WinFormsObject("ToolBar", "")

  # Show the "New Order" button
  ToolBar.Buttons.Item[4].Visible = True

  if ToolBar.wVisible[4, True]:
    Log.Message ("The \"New Order\" button is visible.")
  else:
    Log.Message ("The \"New Order\" button is invisible.")

  # Hide the "New Order" button
  ToolBar.Buttons.Item[4].Visible = False

  if (ToolBar.wVisible[4, True]):
    Log.Message ("The \"New Order\" button is visible.")
  else:
    Log.Message ("The \"New Order\" button is invisible.")

VBScript

Sub Main
  Dim p, ToolBar

  ' Obtain the Orders process and toolbar
  Set p = Sys.Process("Orders")
  Set ToolBar = p.WinFormsObject("MainForm", "*").WinFormsObject("ToolBar", "")

  ' Show the "New Order" button
  ToolBar.Buttons.Item(4).Visible = True

  If ToolBar.wVisible(4, True) Then
    Call Log.Message ("The ""New Order"" button is visible.")
  Else
    Call Log.Message ("The ""New Order"" button is invisible.")
  End If

  ' Hide the "New Order" button
  ToolBar.Buttons.Item(4).Visible = False

  If ToolBar.wVisible(4, True) Then
    Call Log.Message ("The ""New Order"" button is visible.")
  Else
    Call Log.Message ("The ""New Order"" button is invisible.")
  End If
End Sub

DelphiScript

procedure Main;
var p, ToolBar : OleVariant;
begin
  // Obtain the Orders process and toolbar
  p := Sys.Process('Orders');
  ToolBar := p.WinFormsObject('MainForm', '*').WinFormsObject('ToolBar', '');

  // Show the 'New Order' button
  ToolBar.Buttons.Item[4].Visible := true;

  if ToolBar.wVisible[4, true] then
    Log.Message ('The "New Order" button is visible.')
  else
    Log.Message ('The "New Order" button is invisible.');

  // Hide the 'New Order' button
  ToolBar.Buttons.Item[4].Visible := false;

  if ToolBar.wVisible[4, true] then
    Log.Message ('The "New Order" button is visible.')
  else
    Log.Message ('The "New Order" button is invisible.');
end;

C++Script, C#Script

function Main ()
{
  var p, ToolBar;

  // Obtain the Orders process and toolbar
  p = Sys["Process"]("Orders");
  ToolBar = p["WinFormsObject"]("MainForm", "*")["WinFormsObject"]("ToolBar", "");

  // Show the "New Order" button
  ToolBar["Buttons"]["Item"](4)["Visible"] = true;

  if (ToolBar["wVisible"](4, true))
    Log["Message"]("The \"New Order\" button is visible.")
  else
    Log["Message"]("The \"New Order\" button is invisible.");

  // Hide the "New Order" button
  ToolBar["Buttons"]["Item"](4)["Visible"] = false;

  if (ToolBar["wVisible"](4, true))
    Log["Message"]("The \"New Order\" button is visible.")
  else
    Log["Message"]("The \"New Order\" button is invisible.");
}

Verifying that the Button is Checked or Unchecked

Toolbars can include toggle buttons that can be checked or unchecked, which enables or disables a specific application feature. An example is the Folders button on the Windows Explorer toolbar, which displays or hides the folders tree. Quite often, applications provide several possibilities to toggle these options. For instance, the toolbar commands can be duplicated in the application menu. So, in your tests you may need to check that the toolbar button state corresponds to the actual application state.

You can determine whether a toolbar button is checked using the wChecked property of the scripting object that corresponds to the application’s toolbar. This property returns True if the specified button is checked (pressed) and False if it is unchecked (un-pressed).

The example below demonstrates how you can determine whether Windows Explorer displays the folders tree. The script requires Explorer to be opened before running the script:

JavaScript, JScript

function Test ()
{
  var p, w, ToolBar;

  p = Sys.Process("Explorer");
  w = p.Window("*WClass", "*");
  ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1);

  if (ToolBar.wChecked("Folders"))
    Log.Message("The Folders tree is displayed.")
  else
    Log.Message("The Folders tree is hidden.")
}

Python

def Test ():

  p = Sys.Process("Explorer")
  w = p.Window("*WClass", "*")
  ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1)

  if (ToolBar.wChecked["Folders"]):
    Log.Message("The Folders tree is displayed.")
  else:
    Log.Message("The Folders tree is hidden.")

VBScript

Sub CheckFoldersTree
  Dim p, w, ToolBar

  Set p = Sys.Process("Explorer")
  Set w = p.Window("*WClass", "*")
  Set ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1)

  If ToolBar.wChecked("Folders") Then
    Log.Message("The Folders tree is displayed.")
  Else
    Log.Message("The Folders tree is hidden.")
  End If
End Sub

DelphiScript

procedure Test;
var p, w, ToolBar : OleVariant;
begin
  p := Sys.Process('Explorer');
  w := p.Window('*WClass', '*');
  ToolBar := w.Window('WorkerW', '', 1).Window('ReBarWindow32').Window('ToolbarWindow32', '', 1);

  if ToolBar.wChecked['Folders'] then
    Log.Message('The Folders tree is displayed.')
  else
    Log.Message('The Folders tree is hidden.')
end;

C++Script, C#Script

function Test ()
{
  var p, w, ToolBar;

  p = Sys["Process"]("Explorer");
  w = p["Window"]("*WClass", "*");
  ToolBar = w["Window"]("WorkerW", "", 1)["Window"]("ReBarWindow32")["Window"]("ToolbarWindow32", "", 1);

  if (ToolBar["wChecked"]("Folders"))
    Log["Message"]("The Folders tree is displayed.")
  else
    Log["Message"]("The Folders tree is hidden.")
}

See Also

Working With Toolbars in Desktop Windows Applications
Addressing Toolbar Buttons in Desktop Windows Applications
Checking the Toolbar Buttons' Type in Desktop Windows Applications

Highlight search results