Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Squish/Interacting with Menus: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:Tools::Squish]] | |||
= Interacting with menus and menu items = | |||
[[ | In [[:Category:Tools::Squish::Verifying_the_existence_of_a_menu_item | 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. | ||
Consider the menu structure above | [[Image:http://www.kdab.com/~tobias/interacting-with-menus.png|Menu structure]] | ||
Consider the menu structure above - if we simply record interaction with File <s>> New…, Edit</s>> Find <s>> Find…<br />and Edit</s>> Find <s>> Advanced</s>> Find & Replace…, we end up with code such as:<br /><code> # File <s>> New…<br /> activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar&quot;, "File&quot;))<br /> activateItem(waitForObjectItem(":MainWindow.File_QMenu&quot;, "New…")) | |||
<br /> # Edit</s>> Find <s>> Find…<br /> activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar&quot;, "Edit&quot;))<br /> activateItem(waitForObjectItem(":MainWindow.Edit_QMenu&quot;, "Find&quot;))<br /> activateItem(waitForObjectItem(":Edit.Find_QMenu&quot;, "Find…")) | |||
<br /> # Edit</s>> Find <s>> Advanced</s>> Find & Replace…<br /> activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar&quot;, "Edit&quot;))<br /> activateItem(waitForObjectItem(":MainWindow.Edit_QMenu&quot;, "Find&quot;))<br /> activateItem(waitForObjectItem(":Edit.Find_QMenu&quot;, "Advanced&quot;))<br /> activateItem(waitForObjectItem(":Find.Advanced_QMenu&quot;, "Find & Replace…"))</code> | |||
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. | 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:<br /> | 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:<br /><code>def activateMenuItem(*menuPath):<br /> menu = ":MainWindow.menuBar_QMenuBar&quot;<br /> parent = "{name='MainWindow' type='MainWindow' visible='1' windowTitle='MainWindow'}"<br /> for item in menuPath[:–1]:<br /> activateItem(waitForObjectItem(menu, item))<br /> menu = "{title='%s' type='QMenu' visible='1' window=s}" (item, parent)<br /> parent = menu<br /> activateItem(waitForObjectItem(menu, menuPath[–1]))</code> | ||
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. | 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:<br /> | Usage would simply be:<br /><code> activateMenuItem("File&quot;, "New…")<br /> activateMenuItem("Edit&quot;, "Find&quot;, "Find…")<br /> activateMenuItem("Edit&quot;, "Find&quot;, "Advanced&quot;, "Find & Replace…")</code> | ||
Revision as of 10:13, 24 February 2015
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.
Consider the menu structure above - if we simply record interaction with File > New…, Edit> Find > Find…> Find
and Edit> Advanced> Find & Replace…, we end up with code such as:
# File <s>> New…<br /> activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar&quot;, "File&quot;))<br /> activateItem(waitForObjectItem(":MainWindow.File_QMenu&quot;, "New…"))
<br /> # Edit</s>> Find <s>> Find…<br /> activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar&quot;, "Edit&quot;))<br /> activateItem(waitForObjectItem(":MainWindow.Edit_QMenu&quot;, "Find&quot;))<br /> activateItem(waitForObjectItem(":Edit.Find_QMenu&quot;, "Find…"))
<br /> # Edit</s>> Find <s>> Advanced</s>> Find & Replace…<br /> activateItem(waitForObjectItem(":MainWindow.menuBar_QMenuBar&quot;, "Edit&quot;))<br /> activateItem(waitForObjectItem(":MainWindow.Edit_QMenu&quot;, "Find&quot;))<br /> activateItem(waitForObjectItem(":Edit.Find_QMenu&quot;, "Advanced&quot;))<br /> activateItem(waitForObjectItem(":Find.Advanced_QMenu&quot;, "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):<br /> menu = ":MainWindow.menuBar_QMenuBar&quot;<br /> parent = "{name='MainWindow' type='MainWindow' visible='1' windowTitle='MainWindow'}"<br /> for item in menuPath[:–1]:<br /> activateItem(waitForObjectItem(menu, item))<br /> menu = "{title='%s' type='QMenu' visible='1' window=s}" (item, parent)<br /> parent = menu<br /> 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&quot;, "New…")<br /> activateMenuItem("Edit&quot;, "Find&quot;, "Find…")<br /> activateMenuItem("Edit&quot;, "Find&quot;, "Advanced&quot;, "Find & Replace…")