(function (){ 'use strict'; var url, page, timeout, args = require('system').args; if (_AN_Read_length('length', args) < 2 || _AN_Read_length('length', args) > 3) { console.error('Usage:\n phantomjs runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]'); phantom.exit(1); } url = args[1]; page = require('webpage').create(); if (args[2] !== undefined) { timeout = parseInt(args[2], 10); } page.onConsoleMessage = function (msg){ console.log(msg); } ; page.onInitialized = function (){ page.evaluate(addLogging); } ; page.onCallback = function (message){ var result, failed; if (message) { if (message.name === 'QUnit.done') { result = message.data; failed = !result || !result.total || result.failed; if (!result.total) { console.error('No tests were executed. Are you loading tests asynchronously?'); } phantom.exit(failed? 1: 0); } } } ; _AN_Call_open('open', page, url, function (status){ if (status !== 'success') { console.error('Unable to access network: ' + status); phantom.exit(1); } else { var qunitMissing = page.evaluate(function (){ return (typeof QUnit === 'undefined' || !QUnit); } ); if (qunitMissing) { console.error('The `QUnit` object is not present on this page.'); phantom.exit(1); } if (typeof timeout === 'number') { _AN_Call_settimeout('setTimeout', window, function (){ console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...'); phantom.exit(1); } , timeout * 1000); } } } ); function addLogging(){ window.document.addEventListener('DOMContentLoaded', function (){ var currentTestAssertions = [] ; QUnit.log(function (details){ var response; if (details.result) { return ; } response = details.message || ''; if (typeof details.expected !== 'undefined') { if (response) { response += ', '; } response += 'expected: ' + details.expected + ', but was: ' + details.actual; } if (details.source) { response += "\n" + details.source; } currentTestAssertions.push('Failed assertion: ' + response); } ); QUnit.testDone(function (result){ var i, len, name = ''; if (result.module) { name += result.module + ': '; } name += result.name; if (result.failed) { console.log('\n' + 'Test failed: ' + name); for (i = 0, len = _AN_Read_length('length', currentTestAssertions); i < len; i++ ){ console.log(' ' + currentTestAssertions[i]); } } currentTestAssertions.length = 0; } ); QUnit.done(function (result){ console.log('\n' + 'Took ' + result.runtime + 'ms to run ' + result.total + ' tests. ' + result.passed + ' passed, ' + result.failed + ' failed.'); if (typeof window.callPhantom === 'function') { window.callPhantom({ 'name': 'QUnit.done', 'data': result} ); } } ); } , false ); } } )();