2013-06-19 01:44:20 +08:00
|
|
|
// PhantomJS QUnit Test Runner
|
|
|
|
|
|
|
|
/*globals QUnit phantom*/
|
|
|
|
|
2015-08-25 16:42:19 +08:00
|
|
|
var system = require("system"),
|
2015-08-25 23:36:24 +08:00
|
|
|
args = phantom.args;
|
2015-08-25 16:42:19 +08:00
|
|
|
|
2015-08-25 23:36:24 +08:00
|
|
|
if (args === undefined) {
|
|
|
|
args = system.args;
|
|
|
|
args.shift();
|
|
|
|
}
|
2015-08-25 16:42:19 +08:00
|
|
|
|
2013-06-19 01:44:20 +08:00
|
|
|
if (args.length < 1 || args.length > 2) {
|
|
|
|
console.log("Usage: " + phantom.scriptName + " <URL> <timeout>");
|
|
|
|
phantom.exit(1);
|
|
|
|
}
|
|
|
|
|
2015-08-25 16:42:19 +08:00
|
|
|
var page = require("webpage").create();
|
2013-06-19 01:44:20 +08:00
|
|
|
|
|
|
|
page.onConsoleMessage = function(msg) {
|
2015-08-25 16:42:19 +08:00
|
|
|
if (msg.slice(0, 8) === "WARNING:") { return; }
|
|
|
|
if (msg.slice(0, 6) === "DEBUG:") { return; }
|
2013-06-19 01:44:20 +08:00
|
|
|
|
|
|
|
console.log(msg);
|
|
|
|
};
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
page.onCallback = function (message) {
|
|
|
|
// forward the message to the standard output
|
|
|
|
system.stdout.write(message);
|
|
|
|
};
|
|
|
|
|
2013-06-19 01:44:20 +08:00
|
|
|
page.open(args[0], function(status) {
|
2015-08-25 16:42:19 +08:00
|
|
|
if (status !== "success") {
|
2013-06-19 01:44:20 +08:00
|
|
|
console.error("Unable to access network");
|
|
|
|
phantom.exit(1);
|
|
|
|
} else {
|
|
|
|
page.evaluate(logQUnit);
|
|
|
|
|
2015-08-25 16:42:19 +08:00
|
|
|
var timeout = parseInt(args[1] || 120000, 10),
|
|
|
|
start = Date.now();
|
|
|
|
|
2013-06-19 01:44:20 +08:00
|
|
|
var interval = setInterval(function() {
|
|
|
|
if (Date.now() > start + timeout) {
|
|
|
|
console.error("Tests timed out");
|
|
|
|
phantom.exit(124);
|
|
|
|
} else {
|
|
|
|
var qunitDone = page.evaluate(function() {
|
|
|
|
return window.qunitDone;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (qunitDone) {
|
|
|
|
clearInterval(interval);
|
|
|
|
if (qunitDone.failed > 0) {
|
|
|
|
phantom.exit(1);
|
|
|
|
} else {
|
|
|
|
phantom.exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 16:42:19 +08:00
|
|
|
}, 250);
|
2013-06-19 01:44:20 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function logQUnit() {
|
|
|
|
var moduleErrors = [];
|
|
|
|
var testErrors = [];
|
|
|
|
var assertionErrors = [];
|
|
|
|
|
|
|
|
console.log("\nRunning: " + JSON.stringify(QUnit.urlParams) + "\n");
|
|
|
|
|
|
|
|
QUnit.moduleDone(function(context) {
|
|
|
|
if (context.failed) {
|
|
|
|
var msg = "Module Failed: " + context.name + "\n" + testErrors.join("\n");
|
|
|
|
moduleErrors.push(msg);
|
|
|
|
testErrors = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.testDone(function(context) {
|
|
|
|
if (context.failed) {
|
|
|
|
var msg = " Test Failed: " + context.name + assertionErrors.join(" ");
|
|
|
|
testErrors.push(msg);
|
|
|
|
assertionErrors = [];
|
2015-08-25 16:42:19 +08:00
|
|
|
window.callPhantom("F");
|
2013-06-19 01:44:20 +08:00
|
|
|
} else {
|
2015-08-25 16:42:19 +08:00
|
|
|
window.callPhantom(".");
|
2013-06-19 01:44:20 +08: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 16:42:19 +08:00
|
|
|
console.log("\n");
|
2013-06-19 01:44:20 +08:00
|
|
|
|
|
|
|
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(", "));
|
|
|
|
window.qunitDone = context;
|
|
|
|
});
|
|
|
|
}
|