Skip to content

Instantly share code, notes, and snippets.

@uzikilon
Created January 15, 2013 18:51
Show Gist options
  • Save uzikilon/4540969 to your computer and use it in GitHub Desktop.
Save uzikilon/4540969 to your computer and use it in GitHub Desktop.
Jasmine Phantom Runner
/*global require:false,console:false,phantom:false */
/*jshint maxstatements:13,maxcomplexity:3 */
var system = require('system');
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log(msg);
};
if (system.args.length != 2) {
console.log('Usage: phantomjs phantomjs-test-runner.js URL');
phantom.exit(1);
}
var JasmineUtil = {
maxtimeSec: 20,
isDone: function () {
return page.evaluate(function(){
return document.querySelector('.symbolSummary') !== null &&
document.querySelector('.symbolSummary .pending') === null;
});
},
parseResults: function () {
var exitCode = page.evaluate(function () {
var list = document.querySelectorAll('.results > #details > .specDetail.failed');
if (list && list.length > 0) {
console.log('');
console.log(list.length + ' test(s) FAILED:');
for( var i=0; i<list.length; i++ ) {
var el = list[i],
desc = el.querySelector('.description').innerText,
res = el.querySelector('.resultMessage.fail').innerText;
console.log('');
console.log(desc);
console.log(res);
console.log('');
}
return 1;
}
else {
console.log(document.querySelector('.alert > .passingAlert.bar').innerText);
return 0;
}
});
phantom.exit(exitCode);
},
waitAndRun: function (callback) {
var self = this;
var start = new Date().getTime();
var intervalId = setInterval(function () {
var millisPassed = new Date().getTime() - start;
if ( self.isDone() ) {
clearInterval(intervalId);
callback();
return ;
}
else if (millisPassed >= (self.maxtimeSec * 1000)) {
console.log('Script timed out after ' + self.maxtimeSec + ' seconds');
phantom.exit(1);
}
}, 100);
}
};
var url = system.args[1];
var statusCode = null;
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to open URI: ' + url);
phantom.exit(1);
}
else {
if(statusCode && statusCode.toString().substr(0,1) > 3) {
console.log('Unable to open ' + url);
console.log('Returned with status code - ' + statusCode);
phantom.exit(1);
return ;
}
page.evaluate(function () {
console.log('Running test suite: ' + document.title);
console.log('Loaded ' + window.location.href);
});
JasmineUtil.waitAndRun(JasmineUtil.parseResults);
}
});
page.onResourceReceived = function (resource) {
if (resource.url == url) {
statusCode = resource.status;
}
};
@alpengeist
Copy link

Thank you for this script! You saved my day. I almost wrecked my nerves while trying to get the asynchronous behavior under control.

@scottwarren
Copy link

This didn't work for Jasmine 2.0.0 for me, the selectors were wrong, so I updated them:

https://gist.github.com/scottwarren/8107125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment