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/Verifying the Existence of a Menu Item: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Move [[Category::Tools::Squish]] -> [[Category::Squish]])
m (style)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Delete|reason=Page not category. Include as single block in [[Squish: verifying the existence of a menu item]] if relevant}}
[[Category:Squish]]
= 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.
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:
In the end, the usage of any of these approaches are as simple as:
<code>test.verify(menuItemExists("File", "&Save"))<code>
<code>
test.verify(menuItemExists("File", "&Save"))
</code>


== Qt API approach ==
== Qt API approach ==
Line 13: Line 10:
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.
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):
<code>
menuName = "{title='s' type='QMenu' window=':Window'}" menu
def menuItemExists(menu, menuItem):
waitFor("object.exists(menuName)", 20000)
  menuName = '{title="%s" type="QMenu" window=":Window"}' % menu
actions = findObject(menuName).actions()
  waitFor("object.exists(menuName)", 20000)
for i in range(actions.size()):
  actions = findObject(menuName).actions()
action = actions.at(i)
  for i in range(actions.size()):
if action.text == menuItem:
    action = actions.at(i)
return True
    if action.text == menuItem:
return False<code>
      return True
  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.
Line 29: Line 28:
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.


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

Latest revision as of 17:20, 12 August 2020

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("File", "&Save"))

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)