Squish/Interacting with Menus

From Qt Wiki
Jump to: navigation, search


Interacting with menus and menu items

In Verifying the existence of a menu item, we saw how to access single menu items using Squish's real name approach. We can use this approach again, to implement support for dynamically interacting with menus and menu items. That is, we implement one single function for interacting with all menu items in the main window of the AUT, including menu items in sub-menus.

Menu structure

Consider the menu structure above - if we simply record interaction with File -> New…, Edit-> Find -> Find… and Edit-> Find -> Advanced-> Find & Replace…, we end up with code such as:

 # File -> New…
 activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar", "File"))
 activateItem(waitForObjectItem(":MainWindow.File_QMenu", "New…"))

 # Edit-> Find -> Find…
 activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar", "Edit"))
 activateItem(waitForObjectItem(":MainWindow.Edit_QMenu", "Find"))
 activateItem(waitForObjectItem(":Edit.Find_QMenu", "Find…"))

 # Edit-> Find -> Advanced-> Find & Replace…
 activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar", "Edit"))
 activateItem(waitForObjectItem(":MainWindow.Edit_QMenu", "Find"))
 activateItem(waitForObjectItem(":Edit.Find_QMenu", "Advanced"))
 activateItem(waitForObjectItem(":Find.Advanced_QMenu", "Find & Replace…"))

This code requires the menu objects, including the sub-menus, to be added to the object map, which in turn requires us to always record when interacting with a menu or sub-menu that Squish does not yet know about. With the real name approach, we can get around this.

Adding support for any level of menu depth can be done by implementing a function that takes an arbitrary number of arguments, such as below:

def activateMenuItem(*menuPath):
 menu = ":MainWindow.menuBar_QMenuBar"
 parent = "{name='MainWindow' type='MainWindow' visible='1' windowTitle='MainWindow'}"
 for item in menuPath[:1]:
 activateItem(waitForObjectItem(menu, item))
 menu = "{title='%s' type='QMenu' visible='1' window=s}" (item, parent)
 parent = menu
 activateItem(waitForObjectItem(menu, menuPath[1]))

The code above only requires one object to be in the object map, and that is the menu bar. Of course, that can also be changed into a real name instead, if necessary.

Usage would simply be:

 activateMenuItem("File", "New…")
 activateMenuItem("Edit", "Find", "Find…")
 activateMenuItem("Edit", "Find", "Advanced", "Find & Replace…")