Waiting for, looking for, finding multiple objects

Last edited on

Overview

The following example demonstrates how to wait for multiple objects. (This typically includes waiting for the object to be "ready" for interaction, i.e. visible, enabled, etc.)

wait_for_any_object():

find_any_object():

JavaScript

import * as names from 'names.js'
function main()
{
    //...

    result = wait_for_any_object([names.Button1, names.Button2]);
    test.log("Object found: %s" % result[1]);
    clickButton(result[0]);

    clickButton(wait_for_any_object([names.Button1, names.Button2]));
}

/**
 * Find enabled, ready-to-use objects, just like waitForObject()
 */
function wait_for_any_object(object_names, retries, retry_delay)
{
    if (retries == null) {
        retries = 20;
    }

    if (retry_delay == null) {
        retry_delay = 1;
    }

    var lookup_errors = {};
    for (var i = 0; i < retries; i++) {
        for (var ii = 0; ii < object_names.length; ii++ ) {
            var n = object_names[ii];
            try {
                // findObject() does not trigger the Object Not Found
                // (ONF) dialog, so try this first to avoid the ONF
                // dialog in some cases (another option is to disable
                // the ONF dialog at Edit > Preferences > Squish >
                // Playback > "Halt Test Execution in case of 'Object
                // not found' issues for inspecting and fixing"):
                findObject(n);
                return [waitForObject(n, 0), n];
            } catch(e) {
                lookup_errors[n] = "" + e;
            }
        }
        snooze(retry_delay);
    }

    var msg = "{";
    for (n in lookup_errors) {
        msg += '"' + n + '": "' + lookup_errors[n] + '", ';
    }
    msg = msg.substr(0, msg.length - 2);
    msg += "}";
    throw "Objects not found: " + msg;
}

/**
 * Find any objects (enabled, disabled, ready-to-use,
 * not-ready-to-use) just like findObject()
 */
function find_any_object(object_names, retries, retry_delay)
{
    if (retries == null) {
        retries = 20;
    }

    if (retry_delay == null) {
        retry_delay = 1;
    }

    var lookup_errors = {};
    for (var i = 0; i < retries; i++) {
        for (var ii = 0; ii < object_names.length; ii++ ) {
            var n = object_names[ii];
            try {
                return [findObject(n), n];
            } catch(e) {
                lookup_errors[n] = "" + e;
            }
        }
        snooze(retry_delay);
    }

    var msg = "{";
    for (n in lookup_errors) {
        msg += '"' + n + '": "' + lookup_errors[n] + '", ';
    }
    msg = msg.substr(0, msg.length - 2);
    msg += "}";
    throw "Objects not found: " + msg;
}
test.js

Python

import names
def main():
    #...

    result = wait_for_any_object([names.Button1, names.Button2])
    test.log("Object found: %s" % result[1])
    clickButton(result[0])

def wait_for_any_object(object_names, retries=20, retry_delay=1):
    """Find enabled, ready-to-use objects, just like waitForObject()"""
    lookup_errors = {}
    for i in range(retries):
        for n in object_names:
            try:
                # findObject() does not trigger the Object Not Found
                # (ONF) dialog, so try this first to avoid the ONF
                # dialog in some cases (another option is to disable
                # the ONF dialog at Edit > Preferences > Squish >
                # Playback > "Halt Test Execution in case of 'Object
                # not found' issues for inspecting and fixing"):
                findObject(n)
                return [waitForObject(n, 0), n]
            except LookupError, e:
                lookup_errors[n] = "%s" % e
        snooze(retry_delay)
    raise LookupError("Objects not found: %s" % lookup_errors)

def find_any_object(object_names, retries=20, retry_delay=1):
    """Find any objects (enabled, disabled, ready-to-use, not-ready-to-use) just like findObject()"""
    lookup_errors = {}
    for i in range(retries):
        for n in object_names:
            try:
                return [findObject(n), n]
            except LookupError, e:
                lookup_errors[n] = "%s" % e
        snooze(retry_delay)
    raise LookupError("Objects not found: %s" % lookup_errors)
test.py