Skip to content

Instantly share code, notes, and snippets.

@zkessin
Created December 22, 2015 09:04
Show Gist options
  • Save zkessin/5d144dec6634a714ef05 to your computer and use it in GitHub Desktop.
Save zkessin/5d144dec6634a714ef05 to your computer and use it in GitHub Desktop.
"use strict";
var system = require('system');
var page = require('webpage').create();
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 if (condition === true){
// Condition fulfilled (timeout and/or condition is 'true')
console.log("Elm Tests finished in " + (new Date().getTime() - start) + "ms.");
phantom.exit(0);
} else{
phantom.exit(1);
}
}
}, 100); //< repeat check every 250ms
};
if (system.args.length !== 2) {
console.log('Usage: load.js URL');
phantom.exit(1);
}
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(1);
} else {
waitFor(function(){
return page.evaluate(function(){
var el = document.getElementById('elm-check-result');
if (el && el.innerText.match('All tests passed')) {
console.log("OK\n", el.innerText);
return true;
}
else{
console.error("Result ->\n", el.innerText);
return el.innerText;
}
});
}, function(){
var failedNum = page.evaluate(function(){
var el = document.getElementById('elm-check-result');
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