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;
    }
    test.pass("Object found");
    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 occ = 0;
    while (true) {
        containerRealName.occurrence=occ;
        if (!object.exists(containerRealName)) {
            // Handle all Squish editions, keep looking for occurrence='2'
            // all the time:
            if (occ > 2) {
                continue;
            }
            test.log("Container not found, stopping search: " + containerRealName);
            return null;
        }
        objectRealName.container=containerRealName;
        test.log("Looking for: " + objectRealName);
        if (object.exists(objectRealName)) {
            try {
                return waitForObject(objectRealName, 1);
            } catch(e) {
                test.log("Object not ready: " + objectRealName);
            }
        }
        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:
        container_real_name["occurrence"] = occurrence
        if not object.exists(container_real_name):
            break
        obj_real_name[container_property_name]=container_real_name

        os = find_all_objects(obj_real_name)
        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['occurrence']=i
            one_object = findObject(obj_real_name)
            objects.append(one_object)
            i += 1
    except LookupError:
        # No more occurrences found
        pass
    return objects
test.py