Need to know the number of check boxes, rows or items associated with a particular parent object?
Perhaps each child object containing a specific flag holds valuable information.
You can tell Squish to find all the parent object’s children, and only the children of a given type.
Here’s how:
Using Squish’s object.children() function, you can retrieve all child objects of a given parent. Further examining each child object by className, id or another attribute, can tell you more about each child object.
In the following example, a list of child objects, containing a specific className and with a maximum quantity retrieval of 1000 items, returns all matching child objects. The filtered list of child objects can be used as a key driving mechanism in other test cases.
def getChildrenOfType(parent, typename, depth=1000):
children = []
for child in object.children(parent):
if className(child) == typename:
children.append(child)
if depth:
children.extend(getChildrenOfType(child, typename, depth - 1))
return children
I believe there is an alternative way to implement your ‘getChildrenOfType’ function which yields more reusable code:
Instead of encoding the whole ‘recursively walk object hierarchy, test if object has the right type – if so, add it to the result list’ logic into a single function, you could write a so-called ‘generator’ function which merely walks the object hierarchy:
def walkChildren(parent, depth=1000):
for child in object.children(parent):
yield child
if depth:
for grandChild in walkChildren(child, depth – 1):
yield grandChild
Using this, you can use plain Python to implement the filtering and whatnot. E.g. your function can be implemented as
def getChildrenOfType(parent, typename, depth=1000):
return [o for o in walkChildren(parent, depth)
if className(o) == typename]
…but you can *also* use it for doing things like:
* Counting the number of disabled objects in some container; this shows the advantage of separating the ‘walking the object hierarchy’ part from the ‘what to do for each object’ part: a plain Python list comprehension is used to get those elements for which ‘o.visible’ holds:
numButtons = len([o for o in walkChildren(myDialog)
if o.visible])
anyObjectEnabled = any(o.enabled for o in
walkChildren(myDialog))
Just some food for thought.