Skip to content

Instantly share code, notes, and snippets.

@kir
Last active March 17, 2018 13:53
Show Gist options
  • Save kir/5e81c351cf26f3539d207e67b76eee9d to your computer and use it in GitHub Desktop.
Save kir/5e81c351cf26f3539d207e67b76eee9d to your computer and use it in GitHub Desktop.
qunit-puppeteer-runner (was created as a replacement for qunit-phantomJs runner)
#! /usr/bin/env node
// Based on https://github.com/davidtaylorhq/qunit-puppeteer package by David Taylor
// But compatible with NodeJS 6.x
// Usage:
// node puppeteer_qunit_runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]
//
const puppeteer = require('puppeteer');
const args = process.argv;
// arg[0]: node, arg[1] scriptName, args[2...]: arguments
if (args.length < 3) {
console.error('Usage:\n node puppeteer_qunit_runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]');
process.exit(1);
}
const url = args[2];
const timeout = args[3] !== undefined ? parseInt(args[3])*1000 : 60000;
console.log("URL: " + url + "; timeout=" + timeout);
puppeteer.launch({
args: ['--allow-file-access-from-files']
}).then(function( browser ){
browser.newPage().then(function( page ) {
page.on('console', function (args) {
console.log(args._text);
});
var moduleErrors = [];
var testErrors = [];
var assertionErrors = [];
page.exposeFunction('harness_moduleDone', context => {
if (context.failed) {
var msg = "Module Failed: " + context.name + "\n" + testErrors.join("\n");
moduleErrors.push(msg);
testErrors = [];
}
});
page.exposeFunction('harness_testDone', context => {
if (context.failed) {
var msg = " Test Failed: " + context.name + assertionErrors.join(" ");
testErrors.push(msg);
assertionErrors = [];
}
});
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);
});
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();
}
});
page.goto(url).then(function() {
page.evaluate(() => {
// 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); });
});
});
setTimeout(function () {
console.error("Tests timed out");
browser.close();
process.exit(124);
}, timeout);
}, () => {
console.error("Trouble with puppeteer.newPage");
});
}, () => {
console.error("Trouble with puppeteer.launch");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment