-
Notifications
You must be signed in to change notification settings - Fork 29
/
runner-xml.js
132 lines (107 loc) · 3.97 KB
/
runner-xml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* global phantom:false, require:false, console:false, window:false, QUnit:false */
(function () {
'use strict';
var url, page, timeout,
args = require('system').args;
// arg[0]: scriptName, args[1...]: arguments
if (args.length < 2) {
console.error('Usage:\n phantomjs [phantom arguments] runner-xml.js [url-of-your-qunit-testsuite] [timeout-in-seconds] [page-properties]');
exit(1);
}
url = args[1];
if (args[2] !== undefined) {
timeout = parseInt(args[2], 10);
}
page = require('webpage').create();
if (args[3] !== undefined) {
try {
var pageProperties = JSON.parse(args[3]);
if (pageProperties) {
for (var prop in pageProperties) {
if (pageProperties.hasOwnProperty(prop)) {
page[prop] = pageProperties[prop];
}
}
}
} catch (e) {
console.error('Error parsing "' + args[3] + '": ' + e);
}
}
// Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`)
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?');
}
exit(failed ? 1 : 0);
}
}
};
page.open(url, function (status) {
if (status !== 'success') {
console.error('Unable to access network: ' + status);
exit(1);
} else {
// Cannot do this verification with the 'DOMContentLoaded' handler because it
// will be too late to attach it if a page does not have any script tags.
var qunitMissing = page.evaluate(function () {
return (typeof QUnit === 'undefined' || !QUnit);
});
if (qunitMissing) {
console.error('The `QUnit` object is not present on this page.');
exit(1);
}
// Set a default timeout value if the user does not provide one
if (typeof timeout === 'undefined') {
timeout = 5;
}
if (page.injectJs('./node_modules/qunit-reporter-junit/qunit-reporter-junit.js') === false) {
console.error('Could not inject `qunit-reporter-junit.js`.');
exit(1);
}
// Override default
page.evaluate(function () {
QUnit.jUnitReport = function (report) {
console.log(report.xml);
};
});
// Set a timeout on the test running, otherwise tests with async problems will hang forever
setTimeout(function () {
console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...');
exit(1);
}, timeout * 1000);
// Do nothing... the callback mechanism will handle everything!
}
});
function addLogging() {
window.document.addEventListener('DOMContentLoaded', function () {
QUnit.done(function (result) {
if (typeof window.callPhantom === 'function') {
window.callPhantom({
'name': 'QUnit.done',
'data': result
});
}
});
}, false);
}
function exit(code) {
if (page) {
page.close();
}
setTimeout(function () {
phantom.exit(code);
}, 0);
}
})();