2
* QtWebKit-powered headless test runner using PhantomJS
4
* PhantomJS binaries: http://phantomjs.org/download.html
5
* Requires PhantomJS 1.6+ (1.7+ recommended)
8
* phantomjs runner.js [url-of-your-qunit-testsuite]
11
* phantomjs runner.js http://localhost/qunit/test/index.html
14
/*jshint latedef:false */
15
/*global phantom:false, require:false, console:false, window:false, QUnit:false */
20
var args = require('system').args;
22
// arg[0]: scriptName, args[1...]: arguments
23
if (args.length !== 2) {
24
console.error('Usage:\n phantomjs runner.js [url-of-your-qunit-testsuite]');
29
page = require('webpage').create();
31
// Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`)
32
page.onConsoleMessage = function(msg) {
36
page.onInitialized = function() {
37
page.evaluate(addLogging);
40
page.onCallback = function(message) {
45
if (message.name === 'QUnit.done') {
46
result = message.data;
47
failed = !result || result.failed;
49
phantom.exit(failed ? 1 : 0);
54
page.open(url, function(status) {
55
if (status !== 'success') {
56
console.error('Unable to access network: ' + status);
59
// Cannot do this verification with the 'DOMContentLoaded' handler because it
60
// will be too late to attach it if a page does not have any script tags.
61
var qunitMissing = page.evaluate(function() { return (typeof QUnit === 'undefined' || !QUnit); });
63
console.error('The `QUnit` object is not present on this page.');
67
// Do nothing... the callback mechanism will handle everything!
71
function addLogging() {
72
window.document.addEventListener('DOMContentLoaded', function() {
73
var current_test_assertions = [];
75
QUnit.log(function(details) {
78
// Ignore passing assertions
83
response = details.message || '';
85
if (typeof details.expected !== 'undefined') {
90
response += 'expected: ' + details.expected + ', but was: ' + details.actual;
92
response += "\n" + details.source;
96
current_test_assertions.push('Failed assertion: ' + response);
99
QUnit.testDone(function(result) {
102
name = result.module + ': ' + result.name;
105
console.log('Test failed: ' + name);
107
for (i = 0, len = current_test_assertions.length; i < len; i++) {
108
console.log(' ' + current_test_assertions[i]);
112
current_test_assertions.length = 0;
115
QUnit.done(function(result) {
116
console.log('Took ' + result.runtime + 'ms to run ' + result.total + ' tests. ' + result.passed + ' passed, ' + result.failed + ' failed.');
118
if (typeof window.callPhantom === 'function') {
120
'name': 'QUnit.done',