Applies to TestLeft 4.20, last modified on September 11, 2019

A window or control may be associated with menus of different types: top-level windows may have the main, system and context menus; child windows and controls may have a context menu.

To simulate user actions over a menu:

  1. TestLeft UI Spy does not show menus in the object hierarchy. To access menus, use the appropriate properties of parent objects.

    Get the window or control over whose menu you want to simulate actions:

    • In TestLeft UI Spy, if needed, click and configure the code generation settings. For example, select the language you use to create tests.

    • Drag the Pick Object’s target glyph to the needed window or control and release the mouse button.

      — or —

      Click Point and Fix on the UI Spy toolbar and move the mouse pointer to the needed window or control. When a red frame appears around the window or control, press the Shift+Ctrl+Z shortcut.

    • TestLeft UI Spy will highlight the selected window or control in the object hierarchy.

    • Right-click the object and then click Copy Identification.

    • Add the generated identification code to your test.

  2. TestLeft always can access the main and system menus, however, to enable it to access a context menu, you need to invoke the context menu on screen first. To invoke a context menu, you can add methods that simulate a right-click in the appropriate window or control or send the Application key press to it.

  3. Access the menu of the window or control:

    • To access the system menu, use the SystemMenu property provided by the ITopLevelWindow interface.

    • To access the main and context menus created with standard Windows components, use the MainMenu property provided by the ITopLevelWindow interface and the PopupMenu property provided by the IWindow interface.

    • To access menus created with third-party components, use the StripMainMenu, ActionMainMenu, XtraMainMenu and other properties provided by the appropriate interfaces.

    • To access the system menu, use the getSystemMenu() method provided by the TopLevelWindow class.

    • To access the main and context menus created with standard Windows components, use the getMainMenu() method provided by the TopLevelWindow class and the getPopupMenu() method provided by the Window class.

    • To access menus created with third-party components, use the getStripMainMenu(), getActionMainMenu(), getXtraMainMenu() and other methods provided by the appropriate classes.

  4. Use the IMenu interface to work with the menu. The interface provides methods and properties that let you perform various operations:

    Method or Property Description
    Click Simulates a click on a menu item.
    Check Simulates checking or unchecking a menu item.
    Select Simulates placing the mouse pointer over a menu item.
    Items Provides access to individual menu items.
    Count Returns the number of items in a menu.

    Use the Menu class to work with the menu. The class provides methods and properties that let you perform various operations:

    Method or Property Description
    click Simulates a click on a menu item.
    check Simulates checking or unchecking a menu item.
    select Simulates placing the mouse pointer over a menu item.
    getItems Returns an individual menu item.
    getCount Returns the number of items in a menu.
Example

The example below selects the File | New command in Notepad main menu (Notepad must be running):

C#

using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;


public void Test()
{
  ITopLevelWindow wndNotepad = Driver.Find<IProcess>(new ProcessPattern()
  {
    ProcessName = "notepad"
  }).Find<ITopLevelWindow>(new WindowPattern()
  {
    WndClass = "Notepad"
  });

  IMenu menu = wndNotepad.MainMenu;
  menu.Click("File|New");
}

Visual Basic .NET

Imports SmartBear.TestLeft
Imports SmartBear.TestLeft.TestObjects


Public Sub Test()
  Dim wndNotepad As ITopLevelWindow = Driver.Find(Of IProcess)(New ProcessPattern() With {
          .ProcessName = "notepad"
  }).Find(Of ITopLevelWindow)(New WindowPattern() With {
          .WndClass = "Notepad"
  })

  Dim menu As IMenu = wndNotepad.MainMenu
  menu.Click("File|New")
End Sub

Java

import com.smartbear.testleft.*;
import com.smartbear.testleft.testobjects.*;


@Test
public void Test() throws Exception{

  TopLevelWindow wndNotepad = driver.find(TestProcess.class, new ProcessPattern() {{
    ProcessName = "notepad";
  }}).find(TopLevelWindow.class, new WindowPattern() {{
    WndClass = "Notepad";
  }});

  Menu menu = wndNotepad.getMainMenu();
  menu.click("File|New");
}

Using Native Menu Actions

If the application under test is prepared for testing and TestLeft is able to access its internal properties and methods, you can use native properties and methods of the menu component to simulate the needed user actions.

Capturing Images for Simulated Menu Actions

When creating or debugging tests, it may be difficult to determine the state of the tested window before simulating menu actions. You can enable TestLeft Visualizer in your test to capture images of the tested application’s window automatically.

See Also

Simulating User Actions
Creating TestLeft Tests
About Driver Objects
Object Identification

Highlight search results