Selecting Items From Drop-Down Menus

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

Besides standard push buttons, toolbars can contain drop-down buttons that display a menu or a popup window when clicked. Examples of such buttons are the Back, Forward and Views buttons on Windows Explorer toolbar. Usually, these buttons have an attached down arrow, however this is not necessary (for example, the Explorer’s Views button does not have an arrow). Depending on the button type, the popup menu is invoked upon clicking the button’s arrow, or the button itself.

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.

To check whether a button has a toolbar button or a popup menu, you can use the wIsDropDownButton property of the Win32ToolBar, StripToolBar and WPFToolBar objects. If you need to simulate a selection of drop-down menu items, you should write a script that performs the following actions:

  • Simulates a click on the toolbar button (this step is unnecessary if the tested application uses ToolStrip toolbars).
  • Obtains the Menu corresponding to the popup menu.
  • Selects the desired item from the menu.

The implementation of these steps is different for different toolbar types. Detailed information is provided in the sections below.

Win32 and WPF Toolbars

If your tested application is a Win32 or a WPF application and it uses standard toolbars, then in order to access the button’s popup menu you should first invoke it. In tests, you can do this using the ClickItem or ClickItemXY actions of the Win32ToolBar or WPFToolBar object. The ClickItem action can be used to click buttons that do not have an attached arrow; whereas ClickItemXY let you click on the arrow area of split buttons.

The ClickItemXY action requires you to know the down arrow coordinates within the button. You can determine them by recording a click on the arrow. Note however, that the toolbar look can change from one application run to another. For example, toolbars can be configured so that they display small or large buttons, show text labels under buttons, and so on. Since the arrow coordinates within the button can be different in different toolbar looks, a test recorded with one toolbar look may not work for another look. If you want to create a stable test that is independent on the toolbar look, you can determine the button’s arrow coordinates at run time using the wButtonBounds property (we will implement this approach in the script below).

After the button’s popup menu is displayed, you can access it using the PopupMenu property of the toolbar object. This property returns a Menu object that provides scripting access to the button’s menu. You can work with this menu using methods and properties of the obtained Menu object. For example, you can select menu items using the Menu.Click method, determine the items count using the Menu.Count property, and so on. For more information on how you can work with menu controls from your tests, see Working With Menus.

The following example demonstrates how you can work with various kinds of drop-down buttons on a Windows Explorer toolbar.

JavaScript, JScript

function Main ()
{
  var p, w, ToolBar, btnBounds, x, y;

  // Open My Computer
  WshShell.Run("explorer.exe /select,C:\\", SW_SHOWNORMAL);

  // Obtain the Explorer process, window and toolbar
  p = Sys.Process("Explorer");
  w = p.Window("*WClass", "My Computer");
  ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1);

  // Open drive C:
  w.Window("SHELLDLL_DefView").Window("DUIViewWndClassName").Window("DirectUIHWND").Window("CtrlNotifySink").Window("SysListView32", "FolderView").DblClickItem("*C:*");

  // Select the Details view
  ToolBar.ClickItem("Views");
  ToolBar.PopupMenu.Click("Details");

  // Go back to the list of drives
  btnBounds = ToolBar.wButtonBounds("Back");
  x = btnBounds.Right - 1;
  y = (btnBounds.Top + btnBounds.Bottom) / 2;
  ToolBar.ClickItemXY("Back", x, y);
  ToolBar.PopupMenu.Click("My Computer");
}

Python

def Main():

  # Open My Computer
  WshShell.Run("explorer.exe /select,C:\\", SW_SHOWNORMAL)

  # Obtain the Explorer process, window and toolbar 
  p = Sys.Process("Explorer")
  w = p.Window("*WClass", "My Computer")
  ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1)

  # Open drive C:
  w.Window("SHELLDLL_DefView").Window("DUIViewWndClassName").Window("DirectUIHWND").Window("CtrlNotifySink").Window("SysListView32", "FolderView").DblClickItem("*C:*")

  # Select the Details view
  ToolBar.ClickItem("Views")
  ToolBar.PopupMenu.Click("Details")

  # Go back to the list of drives
  btnBounds = ToolBar.wButtonBounds("Back")
  x = btnBounds.Right - 1
  y = (btnBounds.Top + btnBounds.Bottom) / 2
  ToolBar.ClickItemXY("Back", x, y)
  ToolBar.PopupMenu.Click("My Computer")

VBScript

Sub Main
  Dim p, w, ToolBar, btnBounds, x, y

  ' Open My Computer
  Call WshShell.Run("explorer.exe /select,C:\\", SW_SHOWNORMAL)

  ' Obtain the Explorer process, window and toolbar
  Set p = Sys.Process("Explorer")
  Set w = p.Window("*WClass", "My Computer")
  Set ToolBar = w.Window("WorkerW", "", 1).Window("ReBarWindow32").Window("ToolbarWindow32", "", 1)

  ' Open drive C:
  w.Window("SHELLDLL_DefView").Window("DUIViewWndClassName").Window("DirectUIHWND").Window("CtrlNotifySink").Window("SysListView32", "FolderView").DblClickItem("*C:*")

  ' Select the Details view
  ToolBar.ClickItem("Views")
  ToolBar.PopupMenu.Click("Details")

  ' Go back to the list of drives
  Set btnBounds = ToolBar.wButtonBounds("Back")
  x = btnBounds.Right - 1
  y = (btnBounds.Top + btnBounds.Bottom) / 2
  Call ToolBar.ClickItemXY("Back", x, y)
  ToolBar.PopupMenu.Click("My Computer")
End Sub

DelphiScript

procedure Main;
var p, w, ToolBar, btnBounds, x, y : OleVariant;
begin
  // Open My Computer
  WshShell.Run('explorer.exe /select,C:\', SW_SHOWNORMAL);

  // Obtain the Explorer process, window and toolbar
  p := Sys.Process('Explorer');
  w := p.Window('*WClass', 'My Computer');
  ToolBar := w.Window('WorkerW', '', 1).Window('ReBarWindow32').Window('ToolbarWindow32', '', 1);

  // Open drive C:
  w.Window('SHELLDLL_DefView').Window('DUIViewWndClassName').Window('DirectUIHWND').Window('CtrlNotifySink').Window('SysListView32', 'FolderView').DblClickItem('*C:*');

  // Select the Details view
  ToolBar.ClickItem('Views');
  ToolBar.PopupMenu.Click('Details');

  // Go back to the list of drives
  btnBounds := ToolBar.wButtonBounds['Back'];
  x := btnBounds.Right - 1;
  y := (btnBounds.Top + btnBounds.Bottom) / 2;
  ToolBar.ClickItemXY('Back', x, y);
  ToolBar.PopupMenu.Click('My Computer');
end;

C++Script, C#Script

function Main ()
{
  var p, w, ToolBar, btnBounds, x, y;

  // Open My Computer
  WshShell["Run"]("explorer.exe /select,C:\\", SW_SHOWNORMAL);

  // Obtain the Explorer process, window and toolbar
  p = Sys["Process"]("Explorer");
  w = p["Window"]("*WClass", "My Computer");
  ToolBar = w["Window"]("WorkerW", "", 1)["Window"]("ReBarWindow32")["Window"]("ToolbarWindow32", "", 1);

  // Open drive C:
  w["Window"]("SHELLDLL_DefView")["Window"]("DUIViewWndClassName")["Window"]("DirectUIHWND")["Window"]("CtrlNotifySink")["Window"]("SysListView32", "FolderView")["DblClickItem"]("*C:*");

  // Select the Details view
  ToolBar["ClickItem"]("Views");
  ToolBar["PopupMenu"]["Click"]("Details");

  // Go back to the list of drives
  btnBounds = ToolBar["wButtonBounds"]("Back");
  x = btnBounds["Right"] - 1;
  y = (btnBounds["Top"] + btnBounds["Bottom"]) / 2;
  ToolBar["ClickItemXY"]("Back", x, y);
  ToolBar["PopupMenu"]["Click"]("My Computer");
}

ToolStrip Toolbars

If your tested application is a .NET application and it uses ToolStrip toolbars, selecting items is a lot easier. This is mostly because the first step -- simulating a toolbar button click -- is unnecessary, since TestComplete can access and work with the button’s popup menu even if it not displayed on screen. To access the Menu object corresponding to the popup menu of a specific button, use the StripToolBar.wDropDownMenu property. After having obtained the Menu object, you can perform the desired action of the popup menu (for more information, see Working With Menus). For instance, you can select items from the popup menu using the following statement:

toolBarObj.wDropDownMenu(Button).Click(MenuItem)

Here, toolBarObj is a StripToolBar object that corresponds to the tested ToolStrip toolbar; Button is the caption or index of a toolbar button that has the popup menu, and MenuItem is the caption or index of the desired item in the popup menu.

See Also

Working With Toolbars
wIsDropDownButton Property (ToolBar Controls)
wIsDropDownButton Property (Specific to Win32, MFC ToolBar and MFC MenuBar Controls)
PopupMenu Property (Window Objects)
wDropDownMenu Property (ToolBar Controls)
Working With Menus

Highlight search results