forked from 4teamwork/ftw-buildouts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-qunit.js
322 lines (270 loc) · 10.6 KB
/
run-qunit.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* PhantomJS: QUnit test runner from https://github.com/ariya/phantomjs/blob/1.2/examples/run-qunit.js
*
* Wait until the test condition is true or a timeout occurs. Useful for waiting
* on a server response or for a ui change (fadeIn, etc.) to occur.
*
* @param testFx javascript condition that evaluates to a boolean,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param onReady what to do when testFx condition is fulfilled,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
*/
function waitFor(testFx, onReady, onError, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
onError("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 100); //< repeat check every 100ms
};
function ISODateString(d) {
function pad(n){
return n<10 ? '0'+n : n
}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
function unescapeHtml(str) {
str = str.replace(/</g,"<")
str = str.replace(/>/g,">");
str = str.replace(/&/g,"&");
str = str.replace(/"/g,""");
return str;
}
function filterChildrenByType(node, typeName) {
var nodes = new Array();
for (var i=0; i < node.childNodes.length; i++) {
var sub = node.childNodes[i];
if (typeof(sub.tagName) == 'undefined') {
continue;
} else if (sub.tagName.toLowerCase() != typeName.toLowerCase()) {
continue;
}
nodes.push(sub);
}
return nodes;
}
function getChildByClass(node, classname) {
for (var i=0; i < node.childNodes.length; i++) {
var sub = node.childNodes[i];
if (typeof(sub.getAttribute('class')) == 'undefined') {
continue;
} else if (sub.getAttribute('class').indexOf(classname) > -1) {
return sub;
}
}
return null;
}
function formatTestresultTable(test) {
var table = test.getElementsByTagName('table')[0];
var trs = table.getElementsByTagName('tr');
var tabletext = [];
for (var r=0; r < trs.length; r++) {
var tds = trs[r].childNodes;
var rowtext = [];
for (var d=0; d < tds.length; d++) {
var td = tds[d];
rowtext.push(unescapeHtml(td.innerText));
}
tabletext.push(rowtext.join(' '));
}
return tabletext;
}
function JUnitXMLReportWriter(suitename, result) {
this.start_testsuite = function(failures, total, timestamp, duration) {
console.log(
'<testsuite name="' + suitename + '" ' +
'failures="' + failures + '" ' +
'tests="' + total + '" ' +
'errors="0" ' +
'timestamp="' + timestamp + '" ' +
'time="' + duration + '">');
};
this.end_testsuite = function(failures, total, timestamp, duration) {
console.log('</testsuite>');
};
this.start_testcase = function(modulename, testname, has_failures) {
var classname = suitename + '.' + modulename;
console.log(
' <testcase classname="' + classname + '" ' +
'name="' + testname + '">');
};
this.testcase_failure = function(message, type, details) {
message = unescapeHtml(message);
type = unescapeHtml(type);
console.log(
' <failure ' +
'message="' + unescapeHtml(message) +'" ' +
'type="' + type + '">');
console.log(details.join('\n'));
console.log('</failure>')
};
this.end_testcase = function(modulename, testname, has_failures) {
console.log(' </testcase>');
};
return this;
}
function ConsoleReportWriter(suitename, result) {
this.start_testsuite = function(failures, total, timestamp, duration) {
console.log('Running '.concat(suitename));
console.log('');
};
this.end_testsuite = function(failures, total, timestamp, duration) {
console.log(
'Total: '.concat(total).concat(', ').concat(
failures).concat(' failures, in ').concat(
duration).concat(' seconds.'));
console.log('');
};
this.start_testcase = function(modulename, testname, has_failures) {
if (has_failures) {
console.log(' ' + modulename + ': ' + testname);
}
};
this.testcase_failure = function(message, type, details) {
console.log(' ' + message);
console.log(' ' + details.join('\n '));
};
this.end_testcase = function(modulename, testname, has_failures) {
if (has_failures) {
console.log('');
}
};
return this;
}
function generate_report(suitename, result, writertype) {
var writer = writertype(suitename, result);
var summaryArr = result.testresult.split("\n",3);
var durationLine = summaryArr[0];
var countLine = summaryArr[1];
var duration = durationLine.match(
/Tests completed in (\d+) milliseconds./)[1] / 1000;
var countMatch = countLine.match(
/(\d+) tests of (\d+) passed, (\d+) failed./);
var testCount = countMatch[2];
var failures = countMatch[3];
var timestamp = ISODateString(new Date());
writer.start_testsuite(failures, testCount, timestamp, duration);
var testList = document.createElement("ol");
testList.innerHTML = result.tests_html;
var testcase_nodes = filterChildrenByType(testList, 'li');
for (var i=0; i < testcase_nodes.length; i++) {
var testcase = testcase_nodes[i];
var modulename = filterChildrenByType(
filterChildrenByType(testcase, 'strong')[0], 'span')[0].innerText;
var testname = filterChildrenByType(
filterChildrenByType(testcase, 'strong')[0], 'span')[1].innerText;
var has_failures = testcase.getAttribute('class') == 'fail';
writer.start_testcase(modulename, testname, has_failures);
var tests = filterChildrenByType(
filterChildrenByType(testcase, 'ol')[0], 'li');
for (var j=0; j < tests.length; j++) {
var test = tests[j];
if (test.getAttribute('class') == 'pass') {
continue;
}
var message = getChildByClass(test, 'test-message').innerText;
var details = formatTestresultTable(test);
writer.testcase_failure(message, message, details);
}
writer.end_testcase(modulename, testname, has_failures);
}
writer.end_testsuite(failures, testCount, timestamp, duration);
}
var onErrorXML = function(msg) {
var testName = 'QUnit Timeout';
var moduleName = "QUnit ";
var timestamp = ISODateString(new Date());
console.log('<?xml version="1.0"?>');
console.log('<!--\n ' + msg + ' \n-->');
console.log('<testsuite name="QUnit - JavaScript Tests" timestamp="'+ timestamp +'" tests="1" failures="1">');
console.log('<testcase name="'+ testName +'" classname="'+ moduleName +'">');
console.log('<failure message="'+ moduleName +'" type="'+ moduleName +'">');
console.log(msg);
console.log('</failure>');
console.log('</testcase>');
console.log('</testsuite>');
}
var onErrorText = function(msg) {
console.log(msg);
}
if (phantom.args.length === 0 || phantom.args.length > 3) {
console.log('Usage: run-qunit.js URL <TYPE> <SUITENAME>');
console.log('TYPE: either text or junit-xml');
phantom.exit();
}
var page = new WebPage();
var output = new Object();
output.type = phantom.args[1] || 'text';
output.suitename = phantom.args[2] || 'QUnit';
output.writer = ConsoleReportWriter;
output.onError = onErrorText;
if (output.type == 'xml') {
output.writer = JUnitXMLReportWriter;
output.onError = onErrorXML;
}
// 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);
};
var firstCall;
page.open(phantom.args[0], function(status){
//page.loads gets called multiple times when iframes are dynamically added, only allow 1st call to continue
if (firstCall !== undefined) {
return;
}
firstCall = false;
if (status !== "success") {
console.log("Unable to access network");
phantom.exit();
} else {
waitFor(function(){
return page.evaluate(function(){
var el = document.getElementById('qunit-testresult');
if (el && el.innerText.match('completed')) {
return true;
}
return false;
});
}, function(){
var failedNum = page.evaluate(function(){
var el = document.getElementById('qunit-testresult');
try {
return el.getElementsByClassName('failed')[0].innerHTML;
} catch (e) { }
return 10000;
});
var result = page.evaluate(function(){
return {
testresult: document.getElementById('qunit-testresult').innerText,
testresult_html: document.getElementById('qunit-testresult').innerHTML,
tests: document.getElementById('qunit-tests').innerText,
tests_html: document.getElementById('qunit-tests').innerHTML
}
});
generate_report(output.suitename, result, output.writer);
phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
},
output.onError,
60000); /*max. 60s for all tests */
}});