Debian Codegraph @

test/vendor/runner.js

raw
1 /*
2 * QtWebKit-powered headless test runner using PhantomJS
3 *
4 * PhantomJS binaries: http://phantomjs.org/download.html
5 * Requires PhantomJS 1.6+ (1.7+ recommended)
6 *
7 * Run with:
8 * phantomjs runner.js [url-of-your-qunit-testsuite]
9 *
10 * e.g.
11 * phantomjs runner.js http://localhost/qunit/test/index.html
12 */
13
14 /*jshint latedef:false */
15 /*global phantom:false, require:false, console:false, window:false, QUnit:false */
16
17 (function() {
18 'use strict';
19
20 var args = require('system').args;
21
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]');
25 phantom.exit(1);
26 }
27
28 var url = args[1],
29 page = require('webpage').create();
30
31 // Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`)
32 page.onConsoleMessage = function(msg) {
33 console.log(msg);
34 };
35
36 page.onInitialized = function() {
37 page.evaluate(addLogging);
38 };
39
40 page.onCallback = function(message) {
41 var result,
42 failed;
43
44 if (message) {
45 if (message.name === 'QUnit.done') {
46 result = message.data;
47 failed = !result || result.failed;
48
49 phantom.exit(failed ? 1 : 0);
50 }
51 }
52 };
53
54 page.open(url, function(status) {
55 if (status !== 'success') {
56 console.error('Unable to access network: ' + status);
57 phantom.exit(1);
58 } else {
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); });
62 if (qunitMissing) {
63 console.error('The `QUnit` object is not present on this page.');
64 phantom.exit(1);
65 }
66
67 // Do nothing... the callback mechanism will handle everything!
68 }
69 });
70
71 function addLogging() {
72 window.document.addEventListener('DOMContentLoaded', function() {
73 var current_test_assertions = [];
74
75 QUnit.log(function(details) {
76 var response;
77
78 // Ignore passing assertions
79 if (details.result) {
80 return;
81 }
82
83 response = details.message || '';
84
85 if (typeof details.expected !== 'undefined') {
86 if (response) {
87 response += ', ';
88 }
89
90 response += 'expected: ' + details.expected + ', but was: ' + details.actual;
91 if (details.source) {
92 response += "\n" + details.source;
93 }
94 }
95
96 current_test_assertions.push('Failed assertion: ' + response);
97 });
98
99 QUnit.testDone(function(result) {
100 var i,
101 len,
102 name = result.module + ': ' + result.name;
103
104 if (result.failed) {
105 console.log('Test failed: ' + name);
106
107 for (i = 0, len = current_test_assertions.length; i < len; i++) {
108 console.log(' ' + current_test_assertions[i]);
109 }
110 }
111
112 current_test_assertions.length = 0;
113 });
114
115 QUnit.done(function(result) {
116 console.log('Took ' + result.runtime + 'ms to run ' + result.total + ' tests. ' + result.passed + ' passed, ' + result.failed + ' failed.');
117
118 if (typeof window.callPhantom === 'function') {
119 window.callPhantom({
120 'name': 'QUnit.done',
121 'data': result
122 });
123 }
124 });
125 }, false);
126 }
127 })();