forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphantom.js
More file actions
139 lines (120 loc) · 3.06 KB
/
phantom.js
File metadata and controls
139 lines (120 loc) · 3.06 KB
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
/*
* Qt+WebKit powered headless test runner using Phantomjs
*
* Phantomjs installation: http://code.google.com/p/phantomjs/wiki/BuildInstructions
*
* Run with:
* phantomjs runner.js [url-of-your-qunit-testsuite]
*
* E.g.
* phantomjs runner.js http://localhost/qunit/test
*/
var url = phantom.args[0];
var page = require('webpage').create();
// 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.open(url, function(status){
if (status !== "success") {
console.log("Unable to access network: " + status);
phantom.exit(1);
} else {
// page.evaluate(addLogging);
var interval = setInterval(function() {
if (finished()) {
clearInterval(interval);
onfinishedTests();
}
}, 500);
}
});
page.onResourceReceived = function(data) {
page.evaluate(function(addLogging) {
// Only add setZeroTimeout to the window object, and hide everything
// else in a closure.
(function() {
var timeouts = [];
var messageName = "zero-timeout-message";
// Like setTimeout, but only takes a function argument. There's
// no time argument (always zero) and no arguments (you have to
// use a closure).
function setZeroTimeout(fn) {
timeouts.push(fn);
window.postMessage(messageName, "*");
}
function handleMessage(event) {
if (event.source == window && event.data == messageName) {
event.stopPropagation();
if (timeouts.length > 0) {
var fn = timeouts.shift();
fn();
}
}
}
window.addEventListener("message", handleMessage, true);
// Add the one thing we want added to the window object.
window.setZeroTimeout = setZeroTimeout;
})();
setZeroTimeout(function() {
if(window.QUnit && !window.QUnit.__logging) {
console.log('Adding logging');
addLogging();
window.QUnit.__logging = true;
}
});
}, addLogging);
}
function finished() {
return page.evaluate(function(){
return !!window.qunitDone;
});
}
function onfinishedTests() {
var success = page.evaluate(function() {
return window.qunitSuccess;
});
phantom.exit(success ? 0 : 1);
}
function addLogging() {
var print = function(msg) {
console.log(msg);
};
QUnit.begin(function() {
print("Starting ...");
});
QUnit.log(function(o){
var result = o.result,
message = o.message || 'okay';
// Testdox layout
if(result) {
print(' [x] ' + message);
} else {
print(' [ ] ' + message);
if(o.expected) {
print(' Actual: ' + o.actual);
print(' Expected: ' + o.expected);
}
}
});
QUnit.testStart(function(o){
print(' ' + o.name);
});
QUnit.moduleStart(function(o){
print("\n" + o.name);
});
QUnit.done(function(o) {
if(o.failed > 0) {
print("\n" + 'FAILURES!');
print('Tests: ' + o.total
+ ', Passed: ' + o.passed,
+ ', Failures: ' + o.failed);
} else {
print("\n" + 'SUCESS!');
print('Tests: ' + o.total);
}
print('Took: ' + o.runtime + 'ms');
window.qunitDone = true;
window.qunitSuccess = o.failed === 0;
});
}