1 // Simple phantom.js integration script
2 // Adapted from Modernizr
4 function waitFor(testFx, onReady, timeOutMillis) {
5 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5001 //< Default Max Timout is 5s
6 , start = new Date().getTime()
8 , interval = setInterval(function () {
9 if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
10 // If not time-out yet and condition not yet fulfilled
11 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()) //< defensive code
14 // If condition still not fulfilled (timeout but condition is 'false')
15 console.log("'waitFor()' timeout")
18 // Condition fulfilled (timeout and/or condition is 'true')
19 typeof(onReady) === "string" ? eval(onReady) : onReady() //< Do what it's supposed to do once the condition is fulfilled
20 clearInterval(interval) //< Stop this interval
23 }, 100) //< repeat check every 100ms
27 if (phantom.args.length === 0 || phantom.args.length > 2) {
28 console.log('Usage: phantom.js URL')
32 var page = new WebPage()
34 // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
35 page.onConsoleMessage = function(msg) {
39 page.open(phantom.args[0], function(status){
40 if (status !== "success") {
41 console.log("Unable to access network")
45 return page.evaluate(function(){
46 var el = document.getElementById('qunit-testresult')
47 if (el && el.innerText.match('completed')) {
53 var failedNum = page.evaluate(function(){
54 var el = document.getElementById('qunit-testresult')
56 return el.getElementsByClassName('failed')[0].innerHTML
60 phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0)