Skip to content

Instantly share code, notes, and snippets.

@jzaefferer
Created August 15, 2011 20:15
Show Gist options
  • Save jzaefferer/75dd5affcd1e7f106517 to your computer and use it in GitHub Desktop.
Save jzaefferer/75dd5affcd1e7f106517 to your computer and use it in GitHub Desktop.
var url = phantom.args[0];
var page = new WebPage();
//Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
console.log("load started", arguments)
};
page.onLoadFinished = function() {
console.log("load finished", arguments)
};
page.open(url, function(status){
if (status !== "success") {
console.log("Unable to access network: " + status);
phantom.exit(1);
} else {
page.evaluate(addLogging);
var interval = setInterval(function() {
if (finished()) {
clearInterval(interval);
onfinishedTests();
}
}, 500);
}
});
function finished() {
return page.evaluate(function(){
return !!window.qunitDone;
});
};
function onfinishedTests() {
var output = page.evaluate(function() {
return JSON.stringify(window.qunitDone);
});
phantom.exit(JSON.parse(output).failed > 0 ? 1 : 0);
};
function addLogging() {
var current_test_assertions = [];
var module;
QUnit.moduleStart = function(context) {
module = context.name;
};
QUnit.testDone = function(result) {
var name = module + ': ' + result.name;
var i;
if (result.failed) {
console.log('\u001B[31m✖ ' + name);
for (i = 0; i < current_test_assertions.length; i++) {
console.log(' ' + current_test_assertions[i]);
}
console.log('\u001B[39m');
}
current_test_assertions = [];
};
QUnit.log = function(details) {
var response;
if (details.result) {
return;
}
response = details.message || '';
if (typeof details.expected !== 'undefined') {
if (response) {
response += ', ';
}
response += 'expected: ' + details.expected + ', but was: ' + details.actual;
}
current_test_assertions.push('Failed assertion: ' + response);
};
QUnit.done = function(result){
console.log('Took ' + result.runtime + 'ms to run ' + result.total + ' tests. \u001B[32m✔ ' + result.passed + '\u001B[39m \u001B[31m✖ ' + result.failed + '\u001B[39m ');
window.qunitDone = result;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment