Blog

Running doh tests under PhantomJS for Continuous Integration

We tend to use doh quite a bit on our dojo projects. One common requirement is to be able run those tests headless for CI purposes. When thinking about headless testing of apps, PhantomJS naturally springs to mind. Looking around, we didn’t find very much in the way of examples for this, but this blog entry got us 99% of the way there.

Using phantom, we can fire up the virtual browser to invoke the doh test runner page, and then periodically check the log output section to see if it is finished. We can then use some simple regexes to find out if there were errors or failures, and exit the phantom process abnormally if so.

We end up with the following script, adapted from the script in the original blog entry referenced above:

/**
 * Adapted from original script at
 * http://markabasler.blogspot.co.uk/2012/11/running-dojo-doh-tests-headlessly-with.html
 */

var page = require('webpage').create(),
    url, text;

// must have URL to hit
url = phantom.args[0];

if (typeof url === "undefined") {
    console.log("Error: arg[0] must be a fully qualified URL.\nCommand format is: 'phantomjs phantomDohRunner.js \"URL\"");
    console.log("For Example:\nphantomjs phantomDohRunner.js \"http://HOST/tester/dojo/util/doh/runner.html?test=dojo/tests/_base/array\"\n");
    phantom.exit(-1);
}

console.log("Running doh tests with URL: " + url);

page.open(url, function () {

    setInterval(function() {
        var log = page.evaluate(function() {
                return document.getElementById("logBody").innerHTML;
            }), errors, failures;

        if (/TEST SUMMARY/.test(log)) {
            console.log("Tests have finished");
            errors = new Number(/(\d+) errors/.exec(log)[1]);
            failures = new Number(/(\d+) failures/.exec(log)[1]);
            if (errors > 0) {
                console.log(errors + " errors");
            }
            if (failures > 0) {
                console.log(failures + " failures");
            }
            if (errors > 0 || failures > 0) {
                console.log(log);
                phantom.exit(-1);
            } else {
                console.log("All tests passed");
            }
            phantom.exit();
        }
    }, 500);

});
Simon Godden 26/09/2013