-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_qunit.js
97 lines (89 loc) · 2.72 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
var fs = require("fs"), webpage = require("webpage"), system = require("system"), make_test_script = system.args[0], make_test_name = system.args[1] || "test", make_test_timeout = parseInt(system.args[2] || "3000"), make_test_interval = parseInt(system.args[3] || "100");
function make_test_urls(name) {
function listAbsolutePaths() {
if (fs.isFile(name)) {
return [ name ];
} else {
return fs.list(name).map(function(path) {
return name + fs.separator + path;
});
}
}
function isFileHTML(path) {
return fs.isFile(path) && path.match(/^.+[.]html$/);
}
return listAbsolutePaths(name).filter(isFileHTML);
}
function make_test_run(make_test_poll, urls) {
var timeleft = make_test_timeout, url, page;
if (urls === undefined) {
urls = make_test_urls(fs.workingDirectory + fs.separator + make_test_name);
}
function make_test_wait() {
timeleft = timeleft - make_test_interval;
var state = make_test_poll(page);
switch (state) {
case "wait":
if (timeleft > 0) {
setTimeout(make_test_wait, make_test_interval);
} else {
console.log("timeout: " + url);
phantom.exit(2);
}
break;
case "pass":
console.log("pass: " + url);
if (urls.length > 0) {
make_test_loop();
} else {
phantom.exit(0);
}
break;
case "fail":
console.log("fail: " + url);
phantom.exit(1);
break;
default:
console.log("invalid state: " + state + " in " + url);
phantom.exit(4);
}
}
function make_test_loaded(status) {
if (status === "success") {
setTimeout(make_test_wait, make_test_interval);
} else {
console.log("failed to open: " + url);
phantom.exit(3);
}
}
function make_test_loop() {
url = urls.shift();
page = webpage.create();
page.open(url, make_test_loaded);
}
if (urls) {
make_test_loop();
} else {
phantom.exit(0);
}
}
function qunit_result() {
var result = document.getElementById("qunit-testresult");
if (result) {
return result.innerText;
} else {
return "Not a Qunit test page";
}
}
function qunit_poll(page) {
var result = page.evaluate(qunit_result);
if (result.match(/^Running/)) {
return "wait";
} else if (result.match(/, 0 failed[.]$/m)) {
return "pass";
} else {
console.log(result);
return "fail";
}
}
make_test_run(qunit_poll);