Skip to content

Instantly share code, notes, and snippets.

@garth
Created October 9, 2013 14:01
Show Gist options
  • Save garth/6901801 to your computer and use it in GitHub Desktop.
Save garth/6901801 to your computer and use it in GitHub Desktop.
A PhantomJS test runner for qunit that includes a built in web server to host the JavaScript files and auto generate test.html This is especially useful when working in complex environment where running up a full web server during your automated build process might be difficult, since PhantomJS hosts and executes the tests without any external d…
var system = require('system');
var fs = require('fs');
if (system.args.length !== 2) {
console.log('Usage: runner.js website-base-path');
phantom.exit(1);
}
var basePath = system.args[1];
var outputFile = basePath + '/test.html';
var port = 3123; // port for test web server
// generate the test html
var html = '<!DOCTYPE html>\n';
html += '<html>\n';
html += '<head>\n';
html += ' <title>Tests</title>\n';
html += ' <link rel="stylesheet" href="/qunit.css">\n';
html += ' <script src="/vendor.js"></script>\n';
html += ' <script src="/app.js"></script>\n';
html += ' <script src="/qunit.js"></script>\n';
var files = fs.list(basePath + '/tests');
for (var i = 0; i < files.length; i++) {
if (files[i].match(/\.js/)) {
console.log('loading tests from: ' + files[i]);
html += ' <script src="/tests/' + files[i] + '"></script>\n';
}
}
html += '</head>\n';
html += '<body>\n';
html += ' <div id="qunit"></div>\n';
html += ' <div id="qunit-fixture"></div>\n';
html += '</body>\n';
html += '</html>\n';
//comment out this line to disable persisting the test.html
fs.write(outputFile, html, 'w');
// start a web server for serving test files
var service = require('webserver').create()
.listen('127.0.0.1:' + port, function (request, response) {
// serve the html page on /test
if (request.url === '/test.html') {
response.headers = { "Content-Type" : "text/html" };
response.statusCode = 200;
response.write(html);
response.close();
return;
}
// serve all js files
if (request.url.match(/\.js/)) {
var fileName = basePath + request.url;
var content = fs.exists(fileName) ? fs.read(fileName) : null;
if (content) {
response.headers = { "Content-Type" : "application/javascript" };
response.statusCode = 200;
response.write(content);
response.close();
return;
}
}
// send zero content for css
if (request.url.match(/\.css/)) {
response.headers = { "Content-Type" : "text/css" };
response.statusCode = 200;
response.close();
return;
}
// give 404 for all other requests
console.log('not found: ' + request.url);
response.statusCode = 404;
response.headers = { "Content-Type" : "text/plain" };
response.write('file not found: ' + request.url);
response.close();
});
/**
* 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 Timout 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')
console.log("'waitFor()' timeout");
phantom.exit(1);
}
else {
// Condition fulfilled (timeout and/or condition is 'true')
//console.log("Execution took " + (new Date().getTime() - start) + "ms.");
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 250ms
};
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.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.open('http://127.0.0.1:' + port + '/test.html', function(status){
if (status !== "success") {
console.log("Unable to access network");
phantom.exit(1);
} else {
waitFor(function(){
return page.evaluate(function(){
var el = document.getElementById('qunit-testresult');
if (el && el.innerText.match('completed')) {
return true;
}
return false;
});
}, function(){
var failedNum = page.evaluate(function(){
var el = document.getElementById('qunit-testresult');
console.log(el.innerText);
try {
return el.getElementsByClassName('failed')[0].innerHTML;
} catch (e) { }
return 10000;
});
phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment