Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple Qunit test runner for PhantomJS

A simple test runner for QUnit

Usage: phantomjs testrunner.js URL_TO_YOUR_TESTFILE accepts http:// and file:// uris

Returns 0 when tests pass and 1 when something didn't go as expected (useful for use with continuous integration systems like CruiseControl).

[10:44][~/dev/Mikrob.chrome master]: phantomjs testrunner.js file://`pwd`/test.html
Running tests
file:///Users/optimor/dev/Mikrob.chrome/test/collection_store_test.js:71 Clearing localStorage
Passed: 14, Failed: 1 Total: 15
[10:44][~/dev/Mikrob.chrome master]: echo $?
1
if (phantom.state.length === 0) {
if(phantom.args.length === 0 || phantom.args.length > 2) {
console.log("Simple QUnit test runner for phantom.js");
console.log('Usage: testrunner.js url_to_test_file.html');
console.log('Accepts: http://example.com/file.html and file://`pwd`/test.html');
phantom.exit();
} else {
phantom.state = "run-qunit";
phantom.open(phantom.args[0]);
}
} else {
console.log("Running tests");
setInterval(function() {
var result_el = document.getElementById('qunit-testresult');
if(typeof result_el !== 'undefined') {
try {
var passed = result_el.getElementsByClassName('passed')[0].innerHTML;
var total = result_el.getElementsByClassName('total')[0].innerHTML;
var failed = result_el.getElementsByClassName('failed')[0].innerHTML;
} catch(e) {
// SHHHHHH
}
console.log('Passed: '+passed + ', Failed: '+ failed + ' Total: '+ total);
if(parseInt(failed,10) > 0) {
phantom.exit(1);
} else {
phantom.exit(0);
}
}
}, 100);
}
Owner

lukaszkorecki commented Oct 25, 2011

I know that this version used to work, I guess that QUnit's HTML template changed, yes?

blakeeb commented Oct 28, 2011

Nope, PhantomJS' API changed quite a bit.

Owner

lukaszkorecki commented Oct 29, 2011

Just published a kind of ad-hoc script that I use to run QUnit tests headlessly with PhantomJS.
Take a look if you're interested: https://github.com/DavidDurman/ghost-qunit-runner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment