Selecting Toolbar Buttons in Desktop Windows Applications

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

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.

One of the actions that you would perform the most over a toolbar is selecting its individual buttons. This topic explains various approaches that can be used to select toolbar buttons in scripts:

Simulating Clicks on Buttons

The Win32ToolBar, StripToolBar and WPFToolBar objects, which are used to work with various types of toolbars, provide a number of ClickItem actions for simulating clicks on toolbar buttons: ClickItem, DblClickItem, ClickItemXY, and others. All of these actions have parameters that specify the toolbar button to be clicked and the combination of shift keys to be “pressed” during the click. To specify the toolbar button, you can use its caption or index (position within the toolbar). Note that it is possible to use wildcards (* and ?) or regular expressions to specify a button’s caption. 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 button’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. Here is an example of how to select any button:

ToolBarObj.ClickItem("*")

If the tested application uses a standard Win32 toolbar, you can also specify the button by ID. In this case, you should pass False in the ByPosition parameter of the ClickItem action. For more information, see Addressing Toolbar Buttons in Desktop Windows Applications.

The example below illustrates the ClickItem action. It clicks the Home button on Internet Explorer’s toolbar.

The following code snippet works with Internet Explorer 11.

JavaScript, JScript

function Test()
{

  // Obtain the IE process and toolbar
  Browsers.Item(btIExplorer).Run("about:blank");
  var browser = Sys.Browser("*");
  var toolbar = browser.BrowserWindow(0).FindChild("WndCaption", "Favorites and Tools Bar", 10);

  // Go to the home page
  toolbar.ClickItem("Ho&me");

}

Python

def Test():
  
  # Obtain the IE process and toolbar
  Browsers.Item[btIExplorer].Run("about:blank")
  browser = Sys.Browser("*")
  toolbar = browser.BrowserWindow(0).FindChild("WndCaption", "Favorites and Tools Bar", 10)

  # Go to the home page
  toolbar.ClickItem("Ho&me")

VBScript

Sub Test

  Dim browser, toolbar
  ' Obtain the IE process and toolbar
  Browsers.Item(btIExplorer).Run "about:blank"
  Set browser = Sys.Browser("*")
  Set toolbar = browser.BrowserWindow(0).FindChild("WndCaption", "Favorites and Tools Bar", 10)

  ' Go to the home page
  toolbar.ClickItem("Ho&me")

End Sub

DelphiScript

procedure Test();
var browser, toolbar;
begin

  // Obtain the IE process and toolbar
  Browsers.Item(btIExplorer).Run('about:blank');
  browser := Sys.Browser('*');
  toolbar := browser.BrowserWindow(0).FindChild('WndCaption', 'Favorites and Tools Bar', 10);

  // Go to the home page
  toolbar.ClickItem('Ho&me');

end;

C++Script, C#Script

function Test()
{

  // Obtain the IE process and toolbar
  Browsers["Item"](btIExplorer)["Run"]("about:blank");
  var browser = Sys["Browser"]("*");
  var toolbar = browser["BrowserWindow"](0)["FindChild"]("WndCaption", "Favorites and Tools Bar", 10);

  // Go to the home page
  toolbar["ClickItem"]("Ho&me");

}
Checking and Unchecking Toolbar Buttons

Some toolbar buttons represent options that can be checked or unchecked, which enables or disables a specific application feature. An example is WordPad’s Bold, Italic and Underline buttons on the Formatting Toolbar that control the style of the newly typed or selected text. To check or uncheck these buttons from tests, you can use the CheckItem action of the Win32ToolBar, StripToolBar or WPFToolBar object. This action has the Item and Checked parameters, which specify the toolbar button and its desired state - True (checked) or False (Unchecked).

Note: The Win32ToolBar.CheckItem action has an extra ByPosition parameter, which you should use if you specify the button by position or ID.

To determine whether a button can be checked or unchecked, use the wIsCheckButton property. The wChecked property, in its turn, lets you get the current button state -- checked (True) or unchecked (False).

The code snippet below demonstrates how you can toggle toolbar buttons in WordPad (WordPad must be running):

JavaScript, JScript

function Main()
{
  // Toolbar button IDs
  var bBoldID = 32799;
  var bItalicID = 32800;
  var bUnderlineID = 32802;
  var bAlignLeftID = 32803;
  var bAlignCenterID = 32804;
  var bAlignRightID = 32805;

  var p, w, ToolBar;

  // Obtain the WordPad process, its text area and the toolbar
  p = Sys.Process("WORDPAD");
  w = p.Window("WordPadClass").Window("RICHEDIT50W");
  ToolBar = p.Window("WordPadClass").Window("AfxControlBar42u", "Standard").Window("ToolbarWindow32", "Formatting");

  // Enter text using different styles
  ToolBar.CheckItem(bAlignLeftID, true, false);
  w.Keys("Sample ");
  ToolBar.CheckItem(bBoldID, true, false);
  w.Keys("text[Enter]");
  ToolBar.CheckItem(bAlignCenterID, true, false);
  ToolBar.CheckItem(bItalicID, true, false);
  w.Keys("Sample ");
  ToolBar.CheckItem(bUnderlineID, true, false);
  w.Keys("text[Enter]");
  ToolBar.CheckItem(bAlignRightID, true, false);
  ToolBar.CheckItem(bItalicID, false, false);
  w.Keys("Sample");
  ToolBar.CheckItem(bUnderlineID, false, false);
  ToolBar.CheckItem(bBoldID, false, false);
  w.Keys(" text[Enter]");
}

Python

def Main():
  
  # Toolbar button IDs 
  bBoldID = 32799
  bItalicID = 32800
  bUnderlineID = 32802
  bAlignLeftID = 32803
  bAlignCenterID = 32804
  bAlignRightID = 32805

  # Obtain the WordPad process, its text area and the toolbar
  p = Sys.Process("WORDPAD")
  w = p.Window("WordPadClass").Window("RICHEDIT50W")
  ToolBar = p.Window("WordPadClass").Window("AfxControlBar42u", "Standard").Window("ToolbarWindow32", "Formatting")

  # Enter text using different styles
  ToolBar.CheckItem(bAlignLeftID, True, False)
  w.Keys("Sample ")
  ToolBar.CheckItem(bBoldID, True, False)
  w.Keys("text[Enter]")
  ToolBar.CheckItem(bAlignCenterID, True, False)
  ToolBar.CheckItem(bItalicID, True, False)
  w.Keys("Sample ")
  ToolBar.CheckItem(bUnderlineID, True, False)
  w.Keys("text[Enter]")
  ToolBar.CheckItem(bAlignRightID, True, False)
  ToolBar.CheckItem(bItalicID, False, False)
  w.Keys("Sample")
  ToolBar.CheckItem(bUnderlineID, False, False)
  ToolBar.CheckItem(bBoldID, False, False)
  w.Keys(" text[Enter]")

VBScript

Sub Main
  ' Toolbar button IDs
  Const bBoldID = 32799, bItalicID = 32800, bUnderlineID = 32802
  Const bAlignLeftID = 32803, bAlignCenterID = 32804, bAlignRightID = 32805

  Dim p, w, ToolBar

  ' Obtain the WordPad process, its text area and the toolbar
  Set p = Sys.Process("WORDPAD")
  Set w = p.Window("WordPadClass").Window("RICHEDIT50W")
  Set ToolBar = p.Window("WordPadClass").Window("AfxControlBar42u", "Standard").Window("ToolbarWindow32", "Formatting")

  ' Enter text using different styles
  Call ToolBar.CheckItem(bAlignLeftID, True, False)
  w.Keys("Sample ")
  Call ToolBar.CheckItem(bBoldID, True, False)
  w.Keys("text[Enter]")
  Call ToolBar.CheckItem(bAlignCenterID, True, False)
  Call ToolBar.CheckItem(bItalicID, True, False)
  w.Keys("Sample ")
  Call ToolBar.CheckItem(bUnderlineID, True, False)
  w.Keys("text[Enter]")
  Call ToolBar.CheckItem(bAlignRightID, True, False)
  Call ToolBar.CheckItem(bItalicID, False, False)
  w.Keys("Sample")
  Call ToolBar.CheckItem(bUnderlineID, False, False)
  Call ToolBar.CheckItem(bBoldID, False, False)
  w.Keys(" text[Enter]")
End Sub

DelphiScript

procedure Main;
const
  // Toolbar button IDs
  bBoldID = 32799;
  bItalicID = 32800;
  bUnderlineID = 32802;
  bAlignLeftID = 32803;
  bAlignCenterID = 32804;
  bAlignRightID = 32805;
var
  p, w, ToolBar : OleVariant;
begin
  // Obtain the WordPad process, its text area and the toolbar
  p := Sys.Process('WORDPAD');
  w := p.Window('WordPadClass').Window('RICHEDIT50W');
  ToolBar := p.Window('WordPadClass').Window('AfxControlBar42u', 'Standard').Window('ToolbarWindow32', 'Formatting');

  // Enter text using different styles
  ToolBar.CheckItem(bAlignLeftID, true, false);
  w.Keys('Sample ');
  ToolBar.CheckItem(bBoldID, true, false);
  w.Keys('text[Enter]');
  ToolBar.CheckItem(bAlignCenterID, true, false);
  ToolBar.CheckItem(bItalicID, true, false);
  w.Keys('Sample ');
  ToolBar.CheckItem(bUnderlineID, true, false);
  w.Keys('text[Enter]');
  ToolBar.CheckItem(bAlignRightID, true, false);
  ToolBar.CheckItem(bItalicID, false, false);
  w.Keys('Sample');
  ToolBar.CheckItem(bUnderlineID, false, false);
  ToolBar.CheckItem(bBoldID, false, false);
  w.Keys(' text[Enter]');
end;

C++Script, C#Script

function Main()
{
  // Toolbar button IDs
  var bBoldID = 32799;
  var bItalicID = 32800;
  var bUnderlineID = 32802;
  var bAlignLeftID = 32803;
  var bAlignCenterID = 32804;
  var bAlignRightID = 32805;

  var p, w, ToolBar;

  // Obtain the WordPad process, its text area and the toolbar
  p = Sys["Process"]("WORDPAD");
  w = p["Window"]("WordPadClass")["Window"]("RICHEDIT50W");
  ToolBar = p["Window"]("WordPadClass")["Window"]("AfxControlBar42u", "Standard")["Window"]("ToolbarWindow32", "Formatting");

  // Enter text using different styles
  ToolBar["CheckItem"](bAlignLeftID, true, false);
  w["Keys"]("Sample ");
  ToolBar["CheckItem"](bBoldID, true, false);
  w["Keys"]("text[Enter]");
  ToolBar["CheckItem"](bAlignCenterID, true, false);
  ToolBar["CheckItem"](bItalicID, true, false);
  w["Keys"]("Sample ");
  ToolBar["CheckItem"](bUnderlineID, true, false);
  w["Keys"]("text[Enter]");
  ToolBar["CheckItem"](bAlignRightID, true, false);
  ToolBar["CheckItem"](bItalicID, false, false);
  w["Keys"]("Sample");
  ToolBar["CheckItem"](bUnderlineID, false, false);
  ToolBar["CheckItem"](bBoldID, false, false);
  w["Keys"](" text[Enter]");
}

Simulating Keyboard Shortcuts

Usually, the most frequently used toolbar buttons have special keyboard shortcuts assigned to them. For example, in Windows WordPad, Ctrl+N is a shortcut for the New command, Ctrl+S - a shortcut for the Save command, Ctrl+B toggles the bold formatting, and so on. So, you can press a toolbar button that has a shortcut assigned by pressing this keystroke. To simulate keypresses from tests, use the Keys action applied to the scripting object corresponding to the toolbar control (Win32ToolBar, StripToolBar or WPFToolBar or an onscreen object).

The example below demonstrates how you can “press” buttons on a Win32 toolbar using the keyboard. It performs the same actions as the previous example, but instead of simulating mouse clicks on toolbar buttons, it simulates their keyboard shortcuts:

JavaScript, JScript

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

  // Obtain the WordPad process, its text area and the toolbar
  p = Sys.Process("WORDPAD");
  w = p.Window("WordPadClass").Window("RICHEDIT50W");
  ToolBar = p.Window("WordPadClass").Window("AfxControlBar42u", "Standard").Window("ToolbarWindow32", "Formatting");

  // Enter text using different styles
  w.Keys("^l"   + "Sample ");
  w.Keys("^b"   + "text[Enter]");
  w.Keys("^e^i" + "Sample ");
  w.Keys("^u"   + "text[Enter]");
  w.Keys("^r^i" + "Sample");
  w.Keys("^b^u" + " text[Enter]");
}

Python

def Main():

  # Obtain the WordPad process, its text area and the toolbar
  p = Sys.Process("WORDPAD")
  w = p.Window("WordPadClass").Window("RICHEDIT50W")
  ToolBar = p.Window("WordPadClass").Window("AfxControlBar42u", "Standard").Window("ToolbarWindow32", "Formatting")

  # Enter text using different styles
  w.Keys("^l"   + "Sample ")
  w.Keys("^b"   + "text[Enter]")
  w.Keys("^e^i" + "Sample ")
  w.Keys("^u"   + "text[Enter]")
  w.Keys("^r^i" + "Sample")
  w.Keys("^b^u" + " text[Enter]")

VBScript

Sub Main
  Dim p, w, ToolBar

  ' Obtain the WordPad process, its text area and the toolbar
  Set p = Sys.Process("WORDPAD")
  Set w = p.Window("WordPadClass").Window("RICHEDIT50W")
  Set ToolBar = p.Window("WordPadClass").Window("AfxControlBar42u", "Standard").Window("ToolbarWindow32", "Formatting")

  ' Enter text using different styles
  w.Keys("^l"   & "Sample ")
  w.Keys("^b"   & "text[Enter]")
  w.Keys("^e^i" & "Sample ")
  w.Keys("^u"   & "text[Enter]")
  w.Keys("^r^i" & "Sample")
  w.Keys("^b^u" & " text[Enter]")
End Sub

DelphiScript

procedure Main;
var p, w, ToolBar : OleVariant;
begin
  // Obtain the WordPad process, its text area and the toolbar
  p := Sys.Process('WORDPAD');
  w := p.Window('WordPadClass').Window('RICHEDIT50W');
  ToolBar := p.Window('WordPadClass').Window('AfxControlBar42u', 'Standard').Window('ToolbarWindow32', 'Formatting');

  // Enter text using different styles
  w.Keys('^l'   + 'Sample ');
  w.Keys('^b'   + 'text[Enter]');
  w.Keys('^e^i' + 'Sample ');
  w.Keys('^u'   + 'text[Enter]');
  w.Keys('^r^i' + 'Sample');
  w.Keys('^b^u' + ' text[Enter]');
end;

C++Script, C#Script

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

  // Obtain the WordPad process, its text area and the toolbar
  p = Sys["Process"]("WORDPAD");
  w = p["Window"]("WordPadClass")["Window"]("RICHEDIT50W");
  ToolBar = p["Window"]("WordPadClass")["Window"]("AfxControlBar42u", "Standard")["Window"]("ToolbarWindow32", "Formatting");

  // Enter text using different styles
  w["Keys"]("^l"   + "Sample ");
  w["Keys"]("^b"   + "text[Enter]");
  w["Keys"]("^e^i" + "Sample ");
  w["Keys"]("^u"   + "text[Enter]");
  w["Keys"]("^r^i" + "Sample");
  w["Keys"]("^b^u" + " text[Enter]");
}

See Also

Working With Toolbars in Desktop Windows Applications
Addressing Toolbar Buttons in Desktop Windows Applications
ClickItem Action (Specific to Win32, MFC ToolBar and MFC MenuBar Controls)
ClickItem Action (ToolBar Controls)
CheckItem Action (Specific to Win32, MFC ToolBar and MFC MenuBar Controls)
CheckItem Action (Specific to Mycrosoft, Infragistics, Syncfusion, and Telerik ToolBar Controls)
CheckItem Action (Toolbar Controls)
Keys Action

Highlight search results