-
Notifications
You must be signed in to change notification settings - Fork 16
/
qunit-puppeteer.js
114 lines (91 loc) · 2.97 KB
/
qunit-puppeteer.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
#! /usr/bin/env node
var args = process.argv.slice(2);
if (args.length < 1 || args.length > 2) {
console.log("Usage: node run-qunit-chrome.js <URL> <timeout>");
process.exit(1);
}
const targetURL = args[0];
const timeout = parseInt(args[1] || 300000, 10);
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Attach to browser console log events, and log to node console
await page.on('console', (...params) => {
for (let i = 0; i < params.length; ++i)
console.log(`${params[i]}`);
});
var moduleErrors = [];
var testErrors = [];
var assertionErrors = [];
await page.exposeFunction('harness_moduleDone', context => {
if (context.failed) {
var msg = "Module Failed: " + context.name + "\n" + testErrors.join("\n");
moduleErrors.push(msg);
testErrors = [];
}
});
await page.exposeFunction('harness_testDone', context => {
if (context.failed) {
var msg = " Test Failed: " + context.name + assertionErrors.join(" ");
testErrors.push(msg);
assertionErrors = [];
process.stdout.write("F");
} else {
process.stdout.write(".");
}
});
await page.exposeFunction('harness_log', context => {
if (context.result) { return; } // If success don't log
var msg = "\n Assertion Failed:";
if (context.message) {
msg += " " + context.message;
}
if (context.expected) {
msg += "\n Expected: " + context.expected + ", Actual: " + context.actual;
}
assertionErrors.push(msg);
});
await page.exposeFunction('harness_done', context => {
console.log("\n");
if (moduleErrors.length > 0) {
for (var idx=0; idx<moduleErrors.length; idx++) {
console.error(moduleErrors[idx]+"\n");
}
}
var stats = [
"Time: " + context.runtime + "ms",
"Total: " + context.total,
"Passed: " + context.passed,
"Failed: " + context.failed
];
console.log(stats.join(", "));
browser.close();
if (context.failed > 0){
process.exit(1);
}else{
process.exit();
}
});
await page.goto(targetURL);
await page.evaluate(() => {
QUnit.config.testTimeout = 10000;
// Cannot pass the window.harness_blah methods directly, because they are
// automatically defined as async methods, which QUnit does not support
QUnit.moduleDone((context) => { window.harness_moduleDone(context); });
QUnit.testDone((context) => { window.harness_testDone(context); });
QUnit.log((context) => { window.harness_log(context); });
QUnit.done((context) => { window.harness_done(context); });
console.log("\nRunning: " + JSON.stringify(QUnit.urlParams) + "\n");
});
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
await wait(timeout);
console.error("Tests timed out");
browser.close();
process.exit(124);
})().catch((error) => {
console.error(error);
process.exit(1);
});