Testing or waiting for an expected property value

Last edited on

Overview

There are cases where you want to wait for an object to be visible and one of its properties

to have an expected value.

To make this easy to do for testers one can introduce a helper function (waitForPropertyValue() in the examples below).

JavaScript

function main()
{
    // Register squish_dir/examples/qt/addressbook
    // for this example code:
    startApplication("addressbook");
    
    // This will fail, unless you create a new
    // address book and add a single entry to it,
    // but it demonstrates how to use this
    // function:
    
    if (!waitForPropertyValue({"type": "QTableWidget", "visible": "1"}, "rowCount", 1, 20000)) {
        test.fail("Property did not have the expected value");
    } else {
        test.pass("Property had the expected value");
    }
}

/**
 * Needed because Javascript string representation of dictonaries is [Object object] and can not be used for waitFor
**/
function objToString (obj) {
    var values = [];
    for (var p in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, p)) {
            values.push(`"${p}":"${obj[p]}"`);
        }
    }
    return `\{${values.join(", ")}\}`;
}

/**
 * Waits for property value of an already existing object
**/
function waitForPropertyValue(objectName, propertyName, expectedValue, timeoutInMilliseconds)
{
    var condition = `findObject(${objToString(objectName)}).${propertyName}.toString()==expectedValue.toString()`;
    return waitFor(condition, timeoutInMilliseconds);
}

Python

def main():
    # Register squish_dir/examples/qt/addressbook
    # for this example code:
    startApplication("addressbook")

    # This will fail, unless you create a new
    # address book and add a single entry to it,
    # but it demonstrates how to use this
    # function:
    if not waitForPropertyValue({"type": "QTableWidget", "visible": "1"}, "rowCount", 1, 20000):
        test.fail("Property did not have the expected value")
    else:
        test.passes("Property had the expected value")


def waitForPropertyValue(objectName, propertyName, expectedValue, timeoutInMilliseconds=20000):
    """Waits for property value of an already existing object"""

    condition = 'str(findObject({}).{})==str(expectedValue)'.format(objectName,propertyName)
    return waitFor(condition, timeoutInMilliseconds)