Skip to content

Instantly share code, notes, and snippets.

@prasann
Created April 4, 2014 11:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasann/9972777 to your computer and use it in GitHub Desktop.
Save prasann/9972777 to your computer and use it in GitHub Desktop.
A Jasmine 2.0 spec runner and prints output in the console. The code is almost the same as it comes along with phantomjs example. Only that the console reporter is been modified to adapt to Jasmine 2.0 style
var system = require('system'),isDebugMode = false;
/**
* Wait until the test condition is true or a timeout occurs. Useful for waiting
* on a server response or for a ui change (fadeIn, etc.) to occur.
*
* @param testFx javascript condition that evaluates to a boolean,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param onReady what to do when testFx condition is fulfilled,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
* as a callback function.
* @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
*/
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timeout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 100); //< repeat check every 100ms
};
if (!(system.args.length == 2 || system.args.length == 3)) {
console.log('Usage: run-jasmine.js URL [--debug]');
phantom.exit(1);
}
if(system.args[2] == '--debug'){
isDebugMode = true;
}
var page = require('webpage').create();
// 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.open(system.args[1], function(status){
if (status !== "success") {
console.log("Unable to access network");
phantom.exit();
} else {
waitFor(function(){
return page.evaluate(function(){
// If no .symbolSummary or pending is present then, we are not finished loading
return document.body.querySelector('.symbolSummary .pending') === null
});
}, function(){
var exitCode = page.evaluate(function(isDebugMode){
console.log('');
// Load jasmine version info
var jasmineVersion = document.body.querySelector('div.banner > span.title').innerText + " "+
document.body.querySelector('div.banner > span.version').innerText
var banner = document.body.querySelector('div.banner > span.title').innerText;
banner += " " + document.body.querySelector('div.banner > span.version').innerText;
banner += " " + document.body.querySelector('div.banner > span.duration').innerText;
console.log(jasmineVersion);
console.log("");
console.log("Test Summary");
// Load passing tests
var testSummary = document.body.querySelectorAll('ul.symbol-summary > li');
var testSummaryOut = "";
var isPassed = true;
for(var i = 0; i < testSummary.length; i ++)
{
if(testSummary[i].classList.contains('passed'))
{
testSummaryOut += ".";
}
else
{
testSummaryOut += "F";
isPassed = false;
}
}
console.log(testSummaryOut);
if(isPassed)
{
console.log("----------------------------------------------------------------------");
console.log("Total: " + document.body.querySelector("div.html-reporter > div.alert > span.passed").innerText);
console.log("----------------------------------------------------------------------");
// Get results
var specDetails = document.body.querySelectorAll('div.results > div.summary > ul');
if(isDebugMode){
for(var i = 0; i < specDetails.length; i ++)
{
//Print the spec name
console.log("Describe : " + specDetails[i].querySelector('li.suite-detail').innerText);
console.log("");
// Print out specs
var specs = specDetails[i].querySelectorAll('ul.specs');
for(var j = 0; j < specs.length; j++)
{
console.log(" it: " + specs[j].innerText);
}
}
}
}
else
{
console.log("----------------------------------------------------------------------");
console.log("Total: " + document.body.querySelector("div.html-reporter > div.alert > span.failed").innerText);
console.log("----------------------------------------------------------------------");
console.log(document.body.querySelector("div.html-reporter > div.alert > span.failure-list").innerText);
// Get results
var specDetails = document.body.querySelectorAll('div.results > div.failures > div.spec-detail');
// Print each fail
for(var i = 0; i < specDetails.length; i ++)
{
console.log('Failed test: ' + (i+1));
// Print what the fail is
console.log(specDetails[i].querySelector('div.description').innerText);
console.log("");
// Print error
console.log("Error:");
console.log(specDetails[i].querySelector('div.messages > div.result-message').innerText);
if(isDebugMode){
console.log("");
console.log("Stack-trace:");
console.log(specDetails[i].querySelector('div.messages > div.stack-trace').innerText);
}
}
}
console.log("");
console.log("Duration : " + document.body.querySelector('div.banner > span.duration').innerText);
},isDebugMode);
phantom.exit(exitCode);
});
}
});
@vignesh02
Copy link

Hi Prasana,

I have used your run-jasmine-2.0.js to run my Jasmine testing. When I tried to integrated with Jenkins its not generating the xml output. Do you have sample code with you

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