2017-12-19 02:59:41 -05:00
|
|
|
// Chrome QUnit Test Runner
|
|
|
|
// Author: David Taylor
|
|
|
|
// Requires chrome-launcher and chrome-remote-interface from npm
|
|
|
|
// An up-to-date version of chrome is also required
|
2013-06-18 13:44:20 -04:00
|
|
|
|
2017-12-19 02:59:41 -05:00
|
|
|
/* globals Promise */
|
2013-06-18 13:44:20 -04:00
|
|
|
|
2017-12-19 02:59:41 -05:00
|
|
|
var args = process.argv.slice(2);
|
2015-08-25 04:42:19 -04:00
|
|
|
|
2018-04-23 00:41:29 -04:00
|
|
|
if (args.length < 1 || args.length > 3) {
|
|
|
|
console.log("Usage: node run-qunit.js <URL> <timeout> <result_file>");
|
2017-12-19 02:59:41 -05:00
|
|
|
process.exit(1);
|
2013-06-18 13:44:20 -04:00
|
|
|
}
|
|
|
|
|
2017-12-19 02:59:41 -05:00
|
|
|
const chromeLauncher = require('chrome-launcher');
|
|
|
|
const CDP = require('chrome-remote-interface');
|
2013-06-18 13:44:20 -04:00
|
|
|
|
2018-04-23 00:41:29 -04:00
|
|
|
const QUNIT_RESULT = args[2];
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
if (QUNIT_RESULT) {
|
|
|
|
(async () => {
|
|
|
|
await fs.stat(QUNIT_RESULT, (err, stats) => {
|
|
|
|
if (stats && stats.isFile()) fs.unlink(QUNIT_RESULT);
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runAllTests() {
|
2017-12-19 02:59:41 -05:00
|
|
|
|
|
|
|
function launchChrome() {
|
2018-04-11 22:10:45 -04:00
|
|
|
const options = {
|
2017-12-19 02:59:41 -05:00
|
|
|
chromeFlags: [
|
|
|
|
'--disable-gpu',
|
|
|
|
'--headless',
|
|
|
|
'--no-sandbox'
|
|
|
|
]
|
2018-04-23 00:41:29 -04:00
|
|
|
};
|
2018-04-11 22:10:45 -04:00
|
|
|
|
|
|
|
if (process.env.REMOTE_DEBUG) {
|
|
|
|
options.port = 9222;
|
|
|
|
}
|
|
|
|
|
|
|
|
return chromeLauncher.launch(options);
|
2013-06-18 13:44:20 -04:00
|
|
|
}
|
|
|
|
|
2018-04-23 00:41:29 -04:00
|
|
|
let chrome = await launchChrome();
|
|
|
|
let protocol = await CDP({ port: chrome.port});
|
|
|
|
|
|
|
|
const {Page, Runtime} = protocol;
|
|
|
|
|
|
|
|
await Promise.all([Page.enable(), Runtime.enable()]);
|
|
|
|
|
|
|
|
Runtime.consoleAPICalled((response) => {
|
|
|
|
const message = response['args'][0].value;
|
|
|
|
|
|
|
|
// If it's a simple test result, write without newline
|
|
|
|
if(message === "." || message === "F"){
|
|
|
|
process.stdout.write(message);
|
2018-05-23 23:02:20 -04:00
|
|
|
} else if (message && message.startsWith("AUTOSPEC:")) {
|
2018-04-23 00:41:29 -04:00
|
|
|
fs.appendFileSync(QUNIT_RESULT, `${message.slice(10)}\n`);
|
|
|
|
} else {
|
|
|
|
console.log(message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("navigate to " + args[0]);
|
|
|
|
Page.navigate({url: args[0]});
|
|
|
|
|
|
|
|
Page.loadEventFired(async () => {
|
|
|
|
|
|
|
|
await Runtime.evaluate({ expression: `(${qunit_script})()`});
|
|
|
|
|
|
|
|
const timeout = parseInt(args[1] || 300000, 10);
|
|
|
|
var start = Date.now();
|
|
|
|
|
|
|
|
var interval;
|
|
|
|
|
|
|
|
let runTests = async function() {
|
|
|
|
if (Date.now() > start + timeout) {
|
|
|
|
console.error("Tests timed out");
|
|
|
|
protocol.close();
|
|
|
|
chrome.kill();
|
|
|
|
process.exit(124);
|
|
|
|
}
|
|
|
|
|
|
|
|
let numFails = await Runtime.evaluate({expression: `(${check_script})()`});
|
|
|
|
|
|
|
|
if (numFails && numFails.result && numFails.result.type !== 'undefined') {
|
|
|
|
clearInterval(interval);
|
|
|
|
protocol.close();
|
|
|
|
chrome.kill();
|
|
|
|
|
|
|
|
if (numFails.result.value > 0) {
|
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
interval = setInterval(runTests, 250);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
runAllTests();
|
|
|
|
} catch(e) {
|
|
|
|
console.log("Failed to run tests: " + e);
|
|
|
|
process.exit(1);
|
2017-12-19 02:59:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following functions are converted to strings
|
|
|
|
// And then sent to chrome to be evalaluated
|
2013-06-18 13:44:20 -04:00
|
|
|
function logQUnit() {
|
|
|
|
var moduleErrors = [];
|
|
|
|
var testErrors = [];
|
|
|
|
var assertionErrors = [];
|
|
|
|
|
|
|
|
console.log("\nRunning: " + JSON.stringify(QUnit.urlParams) + "\n");
|
|
|
|
|
2016-11-16 05:02:16 -05:00
|
|
|
QUnit.config.testTimeout = 10000;
|
|
|
|
|
2013-06-18 13:44:20 -04:00
|
|
|
QUnit.moduleDone(function(context) {
|
|
|
|
if (context.failed) {
|
|
|
|
var msg = "Module Failed: " + context.name + "\n" + testErrors.join("\n");
|
|
|
|
moduleErrors.push(msg);
|
|
|
|
testErrors = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-23 00:41:29 -04:00
|
|
|
let durations = {};
|
|
|
|
|
2013-06-18 13:44:20 -04:00
|
|
|
QUnit.testDone(function(context) {
|
2018-04-23 00:41:29 -04:00
|
|
|
|
|
|
|
durations[context.module + "::" + context.name] = context.runtime;
|
|
|
|
|
2013-06-18 13:44:20 -04:00
|
|
|
if (context.failed) {
|
|
|
|
var msg = " Test Failed: " + context.name + assertionErrors.join(" ");
|
2018-04-23 00:41:29 -04:00
|
|
|
|
|
|
|
/* QUNIT_RESULT */
|
|
|
|
|
2013-06-18 13:44:20 -04:00
|
|
|
testErrors.push(msg);
|
|
|
|
assertionErrors = [];
|
2017-12-19 02:59:41 -05:00
|
|
|
console.log("F");
|
2013-06-18 13:44:20 -04:00
|
|
|
} else {
|
2017-12-19 02:59:41 -05:00
|
|
|
console.log(".");
|
2013-06-18 13:44:20 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.log(function(context) {
|
|
|
|
if (context.result) { return; }
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.done(function(context) {
|
2015-08-25 04:42:19 -04:00
|
|
|
console.log("\n");
|
2013-06-18 13:44:20 -04:00
|
|
|
|
|
|
|
if (moduleErrors.length > 0) {
|
|
|
|
for (var idx=0; idx<moduleErrors.length; idx++) {
|
|
|
|
console.error(moduleErrors[idx]+"\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 00:41:29 -04:00
|
|
|
console.log("Slowest tests");
|
|
|
|
console.log("----------------------------------------------");
|
|
|
|
let ary = Object.keys(durations).map((key) => ({ 'key': key, 'value': durations[key] }))
|
|
|
|
ary.sort((p1, p2) => (p2.value - p1.value));
|
|
|
|
ary.slice(0, 30).forEach(pair => {
|
|
|
|
console.log(pair.key + ": " + pair.value + "ms");
|
|
|
|
});
|
|
|
|
|
2013-06-18 13:44:20 -04:00
|
|
|
var stats = [
|
|
|
|
"Time: " + context.runtime + "ms",
|
|
|
|
"Total: " + context.total,
|
|
|
|
"Passed: " + context.passed,
|
|
|
|
"Failed: " + context.failed
|
|
|
|
];
|
|
|
|
console.log(stats.join(", "));
|
2018-04-23 00:41:29 -04:00
|
|
|
|
|
|
|
|
2013-06-18 13:44:20 -04:00
|
|
|
window.qunitDone = context;
|
|
|
|
});
|
|
|
|
}
|
2018-04-23 00:41:29 -04:00
|
|
|
let qunit_script = logQUnit.toString();
|
|
|
|
|
|
|
|
if (QUNIT_RESULT) {
|
|
|
|
qunit_script = qunit_script.replace("/* QUNIT_RESULT */", "console.log(`AUTOSPEC: ${context.module}:::${context.testId}:::${context.name}`);");
|
|
|
|
|
|
|
|
}
|
2017-12-19 02:59:41 -05:00
|
|
|
|
|
|
|
function check() {
|
|
|
|
if(window.qunitDone){
|
|
|
|
return window.qunitDone.failed;
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 00:41:29 -04:00
|
|
|
|
2017-12-19 02:59:41 -05:00
|
|
|
const check_script = check.toString();
|