-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_qunit.js
183 lines (168 loc) · 6.64 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
/**
* 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;
}
var generateJUnitXML = function(result) {
console.log('<?xml version="1.0"?>');
console.log('<!--\n ' + result.testresult + ' \n-->');
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());
console.log('<testsuite name="QUnit - JavaScript Tests" timestamp="'+ timestamp +'" tests="'+ testCount +'" failures="'+ failures +'" time="'+ duration +'">');
var testList = document.createElement("ol");
testList.innerHTML = result.tests_html;
var testElements = testList.getElementsByTagName('li');
for (var i = 0; i < testElements.length; i++) {
var resultLine = testElements[i].innerText;
var resultRE = /^([ \w-]*: )?(.+) \((\d+), (\d+), (\d+)\)Rerun/;
var resultMatch = resultLine.match(resultRE);
if (resultMatch) {
var moduleName = resultMatch[1] ? resultMatch[1].slice(0,-": ".length) : '';
var testName = resultMatch[2];
var failure = resultMatch[3];
console.log('<testcase name="'+ testName +'" classname="'+ moduleName +'">');
if (failure > 0) {
var failureText = resultLine.replace(resultRE,'');
console.log('<failure message="'+ moduleName +'" type="'+ moduleName +'">');
console.log(unescapeHtml(failureText));
console.log('</failure>');
}
console.log('</testcase>');
}
}
console.log('</testsuite>');
};
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 generateText = function(result) {
console.log(result.testresult);
console.log(result.tests);
}
var onErrorText = function(msg) {
console.log(msg);
}
if (phantom.args.length === 0 || phantom.args.length > 2) {
console.log('Usage: run-qunit.js URL <TYPE>');
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.fn = generateText;
output.onError = onErrorText;
if (output.type == 'junit-xml') {
output.fn = generateJUnitXML;
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
}
});
output.fn(result);
phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
},
output.onError,
60000); /*max. 60s for all tests */
}});