2019-03-20 13:32:05 +08:00
|
|
|
/*eslint no-console: ["error", { allow: ["log", "error"] }] */
|
|
|
|
|
2017-12-19 15:59:41 +08: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-19 01:44:20 +08:00
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let args = process.argv.slice(2);
|
2015-08-25 16:42:19 +08:00
|
|
|
|
2018-04-23 12:41:29 +08:00
|
|
|
if (args.length < 1 || args.length > 3) {
|
|
|
|
console.log("Usage: node run-qunit.js <URL> <timeout> <result_file>");
|
2017-12-19 15:59:41 +08:00
|
|
|
process.exit(1);
|
2013-06-19 01:44:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-27 14:40:55 +08:00
|
|
|
const chromeLauncher = require("chrome-launcher");
|
|
|
|
const CDP = require("chrome-remote-interface");
|
2013-06-19 01:44:20 +08:00
|
|
|
|
2018-04-23 12:41:29 +08:00
|
|
|
const QUNIT_RESULT = args[2];
|
2018-11-27 14:40:55 +08:00
|
|
|
const fs = require("fs");
|
2018-04-23 12:41:29 +08:00
|
|
|
|
|
|
|
if (QUNIT_RESULT) {
|
|
|
|
(async () => {
|
|
|
|
await fs.stat(QUNIT_RESULT, (err, stats) => {
|
2020-09-23 18:52:11 +08:00
|
|
|
if (stats && stats.isFile()) {
|
|
|
|
fs.unlink(QUNIT_RESULT, (unlinkErr) => {
|
2019-03-20 13:32:05 +08:00
|
|
|
if (unlinkErr) {
|
|
|
|
console.log("Error deleting " + QUNIT_RESULT + " " + unlinkErr);
|
|
|
|
}
|
|
|
|
});
|
2020-09-23 18:52:11 +08:00
|
|
|
}
|
2018-04-23 12:41:29 +08:00
|
|
|
});
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runAllTests() {
|
2017-12-19 15:59:41 +08:00
|
|
|
function launchChrome() {
|
2018-04-12 10:10:45 +08:00
|
|
|
const options = {
|
2019-07-25 17:38:05 +08:00
|
|
|
chromeFlags: [
|
|
|
|
"--disable-gpu",
|
|
|
|
"--headless",
|
|
|
|
"--no-sandbox",
|
|
|
|
"--disable-dev-shm-usage",
|
|
|
|
"--mute-audio",
|
2020-09-23 18:52:11 +08:00
|
|
|
"--window-size=1440,900",
|
2021-09-28 23:32:56 +08:00
|
|
|
"--enable-precise-memory-info",
|
2020-09-23 18:52:11 +08:00
|
|
|
],
|
2018-04-23 12:41:29 +08:00
|
|
|
};
|
2018-04-12 10:10:45 +08:00
|
|
|
|
|
|
|
if (process.env.REMOTE_DEBUG) {
|
|
|
|
options.port = 9222;
|
|
|
|
}
|
|
|
|
|
|
|
|
return chromeLauncher.launch(options);
|
2013-06-19 01:44:20 +08:00
|
|
|
}
|
|
|
|
|
2018-04-23 12:41:29 +08:00
|
|
|
let chrome = await launchChrome();
|
2019-12-12 21:24:58 +08:00
|
|
|
|
|
|
|
let protocol = null;
|
|
|
|
let connectAttempts = 0;
|
|
|
|
while (!protocol) {
|
|
|
|
// Workaround for intermittent CI error caused by
|
|
|
|
// https://github.com/GoogleChrome/chrome-launcher/issues/145
|
|
|
|
try {
|
2022-02-02 23:56:03 +08:00
|
|
|
protocol = await CDP({ port: chrome.port, host: "127.0.0.1" });
|
2019-12-12 21:24:58 +08:00
|
|
|
} catch (e) {
|
2019-12-12 22:06:06 +08:00
|
|
|
if (e.message === "No inspectable targets" && connectAttempts < 50) {
|
2019-12-12 21:24:58 +08:00
|
|
|
connectAttempts++;
|
|
|
|
console.log(
|
|
|
|
"Unable to establish connection to chrome target - trying again..."
|
|
|
|
);
|
2020-09-23 18:52:11 +08:00
|
|
|
// eslint-disable-next-line
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
2019-12-12 21:24:58 +08:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2021-06-15 23:27:15 +08:00
|
|
|
const { Inspector, Page, Runtime, Log } = protocol;
|
2021-06-18 01:09:29 +08:00
|
|
|
// eslint-disable-next-line
|
|
|
|
await Promise.all([
|
|
|
|
Inspector.enable(),
|
|
|
|
Page.enable(),
|
|
|
|
Runtime.enable(),
|
|
|
|
Log.enable(),
|
|
|
|
]);
|
2021-06-15 23:27:15 +08:00
|
|
|
|
|
|
|
// Documentation https://chromedevtools.github.io/devtools-protocol/tot/Log/#type-LogEntry
|
|
|
|
Log.entryAdded(({ entry }) => {
|
|
|
|
let message = `${new Date(entry.timestamp).toISOString()} - (type: ${
|
|
|
|
entry.source
|
|
|
|
}/${entry.level}) message: ${entry.text}`;
|
|
|
|
if (entry.url) {
|
|
|
|
message += `, url: ${entry.url}`;
|
|
|
|
}
|
|
|
|
console.log(message);
|
|
|
|
});
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
Inspector.targetCrashed((entry) => {
|
2019-05-17 00:00:57 +08:00
|
|
|
console.log("Chrome target crashed:");
|
|
|
|
console.log(entry);
|
|
|
|
});
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
Runtime.exceptionThrown((exceptionInfo) => {
|
2019-05-16 14:48:08 +08:00
|
|
|
console.log(exceptionInfo.exceptionDetails.exception.description);
|
|
|
|
});
|
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
Runtime.consoleAPICalled((response) => {
|
2018-11-27 14:40:55 +08:00
|
|
|
const message = response["args"][0].value;
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2020-08-06 01:43:14 +08:00
|
|
|
// Not finished yet, don't add a newline
|
2022-03-11 03:43:17 +08:00
|
|
|
if (message?.startsWith?.("↪")) {
|
2018-04-23 12:41:29 +08:00
|
|
|
process.stdout.write(message);
|
2022-03-11 03:43:17 +08:00
|
|
|
} else if (message?.startsWith?.("AUTOSPEC:")) {
|
2018-04-23 12:41:29 +08:00
|
|
|
fs.appendFileSync(QUNIT_RESULT, `${message.slice(10)}\n`);
|
|
|
|
} else {
|
2022-03-11 03:43:17 +08:00
|
|
|
console.log(...response["args"].map((m) => m.value));
|
2018-04-23 12:41:29 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-11 02:32:20 +08:00
|
|
|
let url = args[0] + "&qunit_disable_auto_start=1";
|
2021-06-18 01:09:29 +08:00
|
|
|
|
|
|
|
const apiKey = process.env.ADMIN_API_KEY;
|
|
|
|
const apiUsername = process.env.ADMIN_API_USERNAME;
|
|
|
|
if (apiKey && apiUsername) {
|
|
|
|
const { Fetch } = protocol;
|
|
|
|
await Fetch.enable();
|
|
|
|
const urlObj = new URL(url);
|
|
|
|
Fetch.requestPaused((data) => {
|
|
|
|
const requestURL = new URL(data.request.url);
|
2022-04-03 03:15:38 +08:00
|
|
|
if (requestURL.hostname !== urlObj.hostname) {
|
2021-06-18 01:09:29 +08:00
|
|
|
Fetch.continueRequest({
|
|
|
|
requestId: data.requestId,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Fetch.continueRequest({
|
|
|
|
requestId: data.requestId,
|
|
|
|
headers: [
|
|
|
|
{ name: "Api-Key", value: apiKey },
|
|
|
|
{ name: "Api-Username", value: apiUsername },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-02 02:30:33 +08:00
|
|
|
console.log("navigate to", url);
|
2021-03-11 02:32:20 +08:00
|
|
|
Page.navigate({ url });
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2019-05-20 22:35:23 +08:00
|
|
|
Page.loadEventFired(async () => {
|
2020-08-06 04:27:06 +08:00
|
|
|
let qff = process.env.QUNIT_FAIL_FAST;
|
2019-05-20 22:35:23 +08:00
|
|
|
await Runtime.evaluate({
|
2020-08-06 01:43:14 +08:00
|
|
|
expression:
|
|
|
|
`const QUNIT_FAIL_FAST = ` +
|
2020-08-06 04:27:06 +08:00
|
|
|
(qff === "1" || qff === "true").toString() +
|
2020-09-23 18:52:11 +08:00
|
|
|
";",
|
2020-08-06 01:43:14 +08:00
|
|
|
});
|
|
|
|
await Runtime.evaluate({
|
2020-09-23 18:52:11 +08:00
|
|
|
expression: `(${qunit_script})();`,
|
2019-05-20 22:35:23 +08:00
|
|
|
});
|
2020-04-02 13:01:38 +08:00
|
|
|
|
2022-07-18 02:48:36 +08:00
|
|
|
if (args[0].includes("report_requests=1")) {
|
2020-04-02 13:01:38 +08:00
|
|
|
await Runtime.evaluate({
|
2020-09-23 18:52:11 +08:00
|
|
|
expression: "QUnit.config.logAllRequests = true",
|
2020-04-02 13:01:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:41:29 +08:00
|
|
|
const timeout = parseInt(args[1] || 300000, 10);
|
2021-01-27 19:39:20 +08:00
|
|
|
let start = Date.now();
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let interval;
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
let runTests = async function () {
|
2018-04-23 12:41:29 +08:00
|
|
|
if (Date.now() > start + timeout) {
|
2020-08-07 21:32:57 +08:00
|
|
|
console.error("\n\nTests timed out\n");
|
2018-04-23 12:41:29 +08:00
|
|
|
protocol.close();
|
|
|
|
chrome.kill();
|
|
|
|
process.exit(124);
|
|
|
|
}
|
|
|
|
|
2018-11-27 14:40:55 +08:00
|
|
|
let numFails = await Runtime.evaluate({
|
2020-09-23 18:52:11 +08:00
|
|
|
expression: `(${check_script})()`,
|
2018-11-27 14:40:55 +08:00
|
|
|
});
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2018-11-27 14:40:55 +08:00
|
|
|
if (numFails && numFails.result && numFails.result.type !== "undefined") {
|
2018-04-23 12:41:29 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
protocol.close();
|
|
|
|
chrome.kill();
|
|
|
|
|
|
|
|
if (numFails.result.value > 0) {
|
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
interval = setInterval(runTests, 250);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
runAllTests().catch((e) => {
|
2018-04-23 12:41:29 +08:00
|
|
|
console.log("Failed to run tests: " + e);
|
|
|
|
process.exit(1);
|
2019-12-12 00:50:31 +08:00
|
|
|
});
|
2017-12-19 15:59:41 +08:00
|
|
|
|
|
|
|
// The following functions are converted to strings
|
2021-05-21 09:43:47 +08:00
|
|
|
// And then sent to chrome to be evaluated
|
2013-06-19 01:44:20 +08:00
|
|
|
function logQUnit() {
|
2022-04-03 03:15:38 +08:00
|
|
|
const QUnit = window.QUnit;
|
2020-08-06 01:43:14 +08:00
|
|
|
let testErrors = [];
|
|
|
|
let assertionErrors = [];
|
2013-06-19 01:44:20 +08:00
|
|
|
|
|
|
|
console.log("\nRunning: " + JSON.stringify(QUnit.urlParams) + "\n");
|
|
|
|
|
2016-11-16 18:02:16 +08:00
|
|
|
QUnit.config.testTimeout = 10000;
|
|
|
|
|
2018-04-23 12:41:29 +08:00
|
|
|
let durations = {};
|
|
|
|
|
2020-08-06 01:43:14 +08:00
|
|
|
let inTest = false;
|
2020-09-23 18:52:11 +08:00
|
|
|
QUnit.testStart(function (context) {
|
2020-08-06 01:43:14 +08:00
|
|
|
console.log("↪ " + context.module + "::" + context.name);
|
|
|
|
inTest = true;
|
2019-06-12 05:14:42 +08:00
|
|
|
});
|
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
QUnit.testDone(function (context) {
|
2018-04-23 12:41:29 +08:00
|
|
|
durations[context.module + "::" + context.name] = context.runtime;
|
|
|
|
|
2013-06-19 01:44:20 +08:00
|
|
|
if (context.failed) {
|
2020-08-06 01:43:14 +08:00
|
|
|
const msg =
|
|
|
|
" Test Failed: " +
|
|
|
|
context.name +
|
|
|
|
assertionErrors.join(" ") +
|
|
|
|
"\n" +
|
|
|
|
context.source;
|
2013-06-19 01:44:20 +08:00
|
|
|
testErrors.push(msg);
|
|
|
|
assertionErrors = [];
|
2020-08-06 01:43:14 +08:00
|
|
|
|
|
|
|
// Pass QUNIT_FAIL_FAST on the command line to quit after the first failure
|
2020-09-23 18:52:11 +08:00
|
|
|
// eslint-disable-next-line
|
2020-08-06 01:43:14 +08:00
|
|
|
if (QUNIT_FAIL_FAST) {
|
|
|
|
QUnit.config.queue.length = 0;
|
|
|
|
}
|
|
|
|
if (inTest) {
|
|
|
|
console.log(" [✘]");
|
|
|
|
}
|
2013-06-19 01:44:20 +08:00
|
|
|
} else {
|
2020-08-06 01:43:14 +08:00
|
|
|
if (inTest) {
|
|
|
|
console.log(" [✔]");
|
|
|
|
}
|
2013-06-19 01:44:20 +08:00
|
|
|
}
|
2020-08-06 01:43:14 +08:00
|
|
|
inTest = false;
|
2013-06-19 01:44:20 +08:00
|
|
|
});
|
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
QUnit.log(function (context) {
|
2018-11-27 14:40:55 +08:00
|
|
|
if (context.result) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-19 01:44:20 +08:00
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let msg = "\n Assertion Failed:";
|
2013-06-19 01:44:20 +08:00
|
|
|
if (context.message) {
|
|
|
|
msg += " " + context.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context.expected) {
|
2018-11-27 14:40:55 +08:00
|
|
|
msg +=
|
|
|
|
"\n Expected: " + context.expected + ", Actual: " + context.actual;
|
2013-06-19 01:44:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assertionErrors.push(msg);
|
|
|
|
});
|
|
|
|
|
2020-09-23 18:52:11 +08:00
|
|
|
QUnit.done(function (context) {
|
2015-08-25 16:42:19 +08:00
|
|
|
console.log("\n");
|
2013-06-19 01:44:20 +08:00
|
|
|
|
2018-04-23 12:41:29 +08:00
|
|
|
console.log("Slowest tests");
|
|
|
|
console.log("----------------------------------------------");
|
2020-09-23 18:52:11 +08:00
|
|
|
let ary = Object.keys(durations).map((key) => ({
|
2022-02-02 23:56:03 +08:00
|
|
|
key,
|
2020-09-23 18:52:11 +08:00
|
|
|
value: durations[key],
|
2018-11-27 14:40:55 +08:00
|
|
|
}));
|
|
|
|
ary.sort((p1, p2) => p2.value - p1.value);
|
2020-09-23 18:52:11 +08:00
|
|
|
ary.slice(0, 30).forEach((pair) => {
|
2018-04-23 12:41:29 +08:00
|
|
|
console.log(pair.key + ": " + pair.value + "ms");
|
|
|
|
});
|
|
|
|
|
2020-10-31 00:29:45 +08:00
|
|
|
console.log("\n");
|
|
|
|
|
|
|
|
if (testErrors.length) {
|
|
|
|
console.log("Test Errors");
|
|
|
|
console.log("----------------------------------------------");
|
|
|
|
testErrors.forEach((e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
console.log("\n");
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let stats = [
|
2013-06-19 01:44:20 +08:00
|
|
|
"Time: " + context.runtime + "ms",
|
|
|
|
"Total: " + context.total,
|
|
|
|
"Passed: " + context.passed,
|
2020-09-23 18:52:11 +08:00
|
|
|
"Failed: " + context.failed,
|
2013-06-19 01:44:20 +08:00
|
|
|
];
|
|
|
|
console.log(stats.join(", "));
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2020-08-06 00:20:28 +08:00
|
|
|
if (context.failed) {
|
|
|
|
console.log("\nUse this filter to run in the same order:");
|
2020-08-06 01:43:14 +08:00
|
|
|
console.log(
|
2020-08-06 04:27:06 +08:00
|
|
|
"QUNIT_FAIL_FAST=1 QUNIT_SEED=" +
|
2020-08-06 01:43:14 +08:00
|
|
|
QUnit.config.seed +
|
|
|
|
" rake qunit:test\n"
|
|
|
|
);
|
2020-08-06 00:20:28 +08:00
|
|
|
console.log("If you have a web environment running, you can visit:");
|
|
|
|
console.log(
|
|
|
|
"http://localhost:3000/qunit?hidepassed&seed=" +
|
|
|
|
QUnit.config.seed +
|
|
|
|
"\n\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-19 01:44:20 +08:00
|
|
|
window.qunitDone = context;
|
|
|
|
});
|
2022-04-03 03:15:38 +08:00
|
|
|
|
2021-03-11 02:32:20 +08:00
|
|
|
QUnit.start();
|
2013-06-19 01:44:20 +08:00
|
|
|
}
|
2018-04-23 12:41:29 +08:00
|
|
|
let qunit_script = logQUnit.toString();
|
|
|
|
|
|
|
|
if (QUNIT_RESULT) {
|
2018-11-27 14:40:55 +08:00
|
|
|
qunit_script = qunit_script.replace(
|
|
|
|
"/* QUNIT_RESULT */",
|
|
|
|
"console.log(`AUTOSPEC: ${context.module}:::${context.testId}:::${context.name}`);"
|
|
|
|
);
|
2018-04-23 12:41:29 +08:00
|
|
|
}
|
2017-12-19 15:59:41 +08:00
|
|
|
|
|
|
|
function check() {
|
2018-11-27 14:40:55 +08:00
|
|
|
if (window.qunitDone) {
|
2017-12-19 15:59:41 +08:00
|
|
|
return window.qunitDone.failed;
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 12:41:29 +08:00
|
|
|
|
2017-12-19 15:59:41 +08:00
|
|
|
const check_script = check.toString();
|