Squish/Verifying the Existence of a Menu Item: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Verifying the existence of a menu item=
[[Category:Tools::Squish]]


To verify the existence of a menu item within a menu, there are multiple approaches: 1) accessing the QAction list supplied to us by the QWidget class through the Qt <span class="caps">API</span> or 2) using Squish’s real name to see if the object exists.
= Verifying the existence of a menu item =


In the end, the usage of any of these approaches are as simple as:<br />
To verify the existence of a menu item within a menu, there are multiple approaches: 1) accessing the QAction list supplied to us by the QWidget class through the Qt API or 2) using Squish's real name to see if the object exists.


==Qt <span class="caps">API</span> approach==
In the end, the usage of any of these approaches are as simple as:<br /><code>test.verify(menuItemExists(&quot;File&amp;quot;, &quot;&amp;Save&amp;quot;))<code>


This approach uses the Qt <span class="caps">API</span> to access the menu and the menu’s actions, and then iterate through all of the actions to see if it contains the one we are looking for.
== Qt API approach ==
 
This approach uses the Qt API to access the menu and the menu's actions, and then iterate through all of the actions to see if it contains the one we are looking for.
 
</code>def menuItemExists(menu, menuItem):<br /> menuName = &quot;{title='s' type='QMenu' window=':Window'}&quot; menu<br /> waitFor(&quot;object.exists(menuName)&quot;, 20000)<br /> actions = findObject(menuName).actions()<br /> for i in range(actions.size()):<br /> action = actions.at(i)<br /> if action.text == menuItem:<br /> return True<br /> return False<code>


We start out by using the real name of the menu to identify it, as that gives us the ability to dynamically specify which menu Squish should be looking for. We then wait for it to exist, to make sure the script is not played back to fast, retrieve the QAction list from the menu and iterate through them, comparing the text property of QAction with the text we supply in the argument.
We start out by using the real name of the menu to identify it, as that gives us the ability to dynamically specify which menu Squish should be looking for. We then wait for it to exist, to make sure the script is not played back to fast, retrieve the QAction list from the menu and iterate through them, comparing the text property of QAction with the text we supply in the argument.


==Squish’s real name approach==
== Squish's real name approach ==


Similar to the approach above where we use the real name to get to the menu, we do the same thing but with the menu item instead.
Similar to the approach above where we use the real name to get to the menu, we do the same thing but with the menu item instead.


At a first glance, the real name approach is easier, but consider also that the Qt <span class="caps">API</span> approach is much more powerful when we go beyond checking for just existance, such as making sure menu items all have shortcut keys or tooltips set.
</code>def menuItemExists(menu, menuItem):<br /> menuName = &quot;{title='s' type='QMenu' window=':Window'}&quot; menu<br /> menuItemName = &quot;{container=%s text='s' type='QAction'}&quot; (menuName, menuItem)<br /> return object.exists(menuItemName)<code>
 
===Categories:===
 
* [[:Category:Tools|Tools]]
** [[:Category:Tools::Squish|Squish]]

Revision as of 10:18, 24 February 2015


Verifying the existence of a menu item

To verify the existence of a menu item within a menu, there are multiple approaches: 1) accessing the QAction list supplied to us by the QWidget class through the Qt API or 2) using Squish's real name to see if the object exists.

In the end, the usage of any of these approaches are as simple as:

test.verify(menuItemExists(&quot;File&amp;quot;, &quot;&amp;Save&amp;quot;))<code>

== Qt API approach ==

This approach uses the Qt API to access the menu and the menu's actions, and then iterate through all of the actions to see if it contains the one we are looking for.

def menuItemExists(menu, menuItem):
menuName = "{title='s' type='QMenu' window=':Window'}" menu
waitFor("object.exists(menuName)", 20000)
actions = findObject(menuName).actions()
for i in range(actions.size()):
action = actions.at(i)
if action.text == menuItem:
return True
return False

We start out by using the real name of the menu to identify it, as that gives us the ability to dynamically specify which menu Squish should be looking for. We then wait for it to exist, to make sure the script is not played back to fast, retrieve the QAction list from the menu and iterate through them, comparing the text property of QAction with the text we supply in the argument.

== Squish's real name approach ==

Similar to the approach above where we use the real name to get to the menu, we do the same thing but with the menu item instead.

def menuItemExists(menu, menuItem):
menuName = "{title='s' type='QMenu' window=':Window'}" menu
menuItemName = "{container=%s text='s' type='QAction'}" (menuName, menuItem)
return object.exists(menuItemName)