Logging object properties

Last edited on

Overview

For comparing objects it can be useful to iterate over and output all object properties and their respective values. This can be achieved with the help of the object.properties() function:

Python:

def main():
    # startApplication(...)
    log_obj(waitForObject({"type": "MainWindow", "visible": True}));

def log_obj(obj):
    properties = object.properties(obj)
    keys = sorted(properties.keys())
    for key in keys:
        value = properties[key]
        if isNull(value):
            test.log(key + '=null')
        else:
            test.log(key + '=%s' % value)
test.py

JavaScript:

function main()
{
    // startApplication(...);
    log_obj(waitForObject({"type": "MainWindow", "visible": true}));
}

function log_obj(obj)
{
    var properties = object.properties(obj);
    var keys = new Array();
    for (var key in properties) {
        keys.push(key);
    }
    keys.sort();
    for (var i = 0; i < keys.length; i++) {
        var key = keys[i];
        var value = properties[key];
        if (isNull(value)) {
            test.log(key + '=null');
        } else {
            try {
                test.log(key + '=' + value);
            } catch(e) {
                test.log(key + '=<' + typeName(value) + ' object reference>');
            }
        }
    }
}
test.js