Searching for objects in all container instances

Last edited on

Overview

Squish does not search through all “special” container objects when they are likely to contain a very large number of objects.

The following examples demonstrate how to search for one or all instances of certain objects in all suitable containers.

Finding the first instance of an object in all containers

This example demonstrates how to search through all instances of a specific container type for the first instance of an object.

The object to be searched for and the desirable container objects must be identified (“described”) via How Squish looks up Real Names from Symbolic Names:

JavaScript:

function main()
{
    startApplication("addressbook");

    //...

    var containerRealName = "{type='QTableWidget'}";
    var objectRealName = "{column='0' row='0' type='QModelIndex'}";
    var o = waitForInAllContainers(objectRealName, containerRealName);
    if (o == null) {
        test.fatal("Object not found in any of the containers! Aborting...");
        return;
    }
    mouseClick(o);

    //...
}

function waitForInAllContainers(objectRealName, containerRealName, retries) {
    if (retries == null) {
        retries = 20;
    }
    for (var i = 0; i < retries; i++) {
        var o = findInAllContainers(objectRealName, containerRealName);
        if (o != null) {
            return o;
        }
        snooze(1);
    }
    return null;
}

function findInAllContainers(objectRealName, containerRealName) {
    var cnProperties = containerRealName.substr(1, containerRealName.length - 2);
    var onProperties = objectRealName.substr(1, objectRealName.length - 2);
    var occ = 0;
    while (true) {
        var cn = "{" + cnProperties + " occurrence='" + occ + "'}";
        if (!object.exists(cn)) {
            // Handle all Squish editions, keep looking for occurrence='2'
            // all the time:
            if (occ > 2) {
                continue;
            }
            test.log("Container not found, stopping search: " + cn);
            return null;
        }
        var nn = "{" + onProperties + " container=" + cn + "}";
        test.log("Looking for: " + nn);
        if (object.exists(nn)) {
            try {
                return waitForObject(nn, 1);
            } catch(e) {
                test.log("Object not ready: " + nn);
            }
        }
        occ += 1;
    }
    return null;
}
test.js

Finding all object instances in all container instances

This examples demonstrates retrieving all object instances in all container instances.

The object to be searched for and the desirable container objects must be identified (“described”) via How Squish looks up Real Names from Symbolic Names:

Python:

def main():
    startApplication(...)

    crn = "{type='org.eclipse.swt.custom.CTabFolder' isvisible='true'}"
    orn = "{type='org.eclipse.swt.custom.CTabItem' parent.visible='true'}"
    o = find_all_objects_in_all_containers(crn, orn)

def find_all_objects_in_all_containers(container_real_name, obj_real_name, container_property_name="container"):
    if "occurrence=" in container_real_name:
        test.warning("Real name must not contain the occurrence property on the actual object (but it is okay for containers): %s" % container_real_name)
    if "occurrence=" in obj_real_name:
        test.warning("Real name must not contain the occurrence property on the actual object (but it is okay for containers): %s" % obj_real_name)

    occurrence = 0
    objects = []
    while True:
        crn = "{occurrence='%s' %s}" % (occurrence, container_real_name[1:-1])
        if not object.exists(crn):
            break
        orn = "{%s=%s %s}" % (container_property_name, crn, obj_real_name[1:-1])
        os = find_all_objects(orn)
        objects.extend(os)
        occurrence += 1
    return objects

def find_all_objects(obj_real_name):
    if "occurrence=" in obj_real_name:
        test.warning("Real name must not contain the occurrence property on the actual object (but it is okay for containers): %s" % obj_real_name)
  
    objects = []
    try:
        one_object = findObject(obj_real_name)
        objects.append(one_object)
  
        # Occurrence of second instance (varies across
        # Squish editions/toolkits):
        i = 0
  
        # If Java second instance has occurrence 1:
        if hasattr(one_object, "class"):
            i = 1
  
        # If Qt or web second instance has occurrence 2:
        elif hasattr(one_object, "metaObject") or hasattr("nextSibling"):
            i = 2

        while True:
            obj_real_name_n = "{occurrence='%s' %s}" % (i, obj_real_name[1:-1])
            one_object = findObject(obj_real_name_n)
            objects.append(one_object)
            i += 1
    except LookupError:
        # No more occurrences found
        pass
    return objects
test.py

Related Information