Squish tip of the week: How to determine all child objects of a given type

Squish tip of the week: How to determine all child objects of a given type

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:
if depth:
children.extend(getChildrenOfType(child, typename, depth - 1))
return children

Click here to learn more about finding and using child objects

1 Comment

  1. Frerich 6 years ago

    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)
    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)



  2. * Testing if any element is enabled; this is actually a case where the generator shines, because you don’t have to build a list of all objects first (which you would get if your ‘walkChildren’ function returned a plain list) only to check whether any of them is enabled. The generator makes this code only actually walk the object hierarchy until it finds the first enabled object:


        anyObjectEnabled = any(o.enabled for o in



    Just some food for thought.

Leave a reply

Your email address will not be published. Required fields are marked *