1/*jshint evil:true*/ 2 3/* This file is only used by the test suite. 4 * 5 * Origin: https://github.com/ariya/phantomjs/blob/master/examples/run-qunit.js 6 * License: https://github.com/ariya/phantomjs/blob/master/LICENSE.BSD 7 * 8 * Inclusion into Apache products is allowed according to http://www.apache.org/legal/3party.html 9 */ 10 11var system = require('system'); 12 13 14/** 15 * Wait until the test condition is true or a timeout occurs. Useful for waiting 16 * on a server response or for a ui change (fadeIn, etc.) to occur. 17 * 18 * @param testFx javascript condition that evaluates to a boolean, 19 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 20 * as a callback function. 21 * @param onReady what to do when testFx condition is fulfilled, 22 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 23 * as a callback function. 24 * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 25 */ 26function waitFor(testFx, onReady, timeOutMillis) { 27 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timout is 3s 28 start = new Date().getTime(), 29 condition = false, 30 interval = setInterval(function() { 31 if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) { 32 // If not time-out yet and condition not yet fulfilled 33 condition = (typeof(testFx) === 'string' ? eval(testFx) : testFx()); //< defensive code 34 } else { 35 if (!condition) { 36 // If condition still not fulfilled (timeout but condition is 'false') 37 console.log("'waitFor()' timeout"); 38 phantom.exit(1); 39 } else { 40 // Condition fulfilled (timeout and/or condition is 'true') 41 console.log("'waitFor()' finished in " + (new Date().getTime() - start) + 'ms.'); 42 if (typeof(onReady) === 'string') { 43 eval(onReady); 44 } else { 45 onReady(); //< Do what it's supposed to do once the condition is fulfilled 46 } 47 clearInterval(interval); //< Stop this interval 48 } 49 } 50 }, 100); //< repeat check every 250ms 51} 52 53 54if (system.args.length === 1 || system.args.length > 3) { 55 console.log('Usage: phantomjs phantomjs-qunit.js URL'); 56 phantom.exit(1); 57} 58 59var page = new WebPage(); 60 61// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 62page.onConsoleMessage = function(msg) { 63 console.log(msg); 64}; 65 66page.open(system.args[1], function(status) { 67 if (status !== 'success') { 68 console.log('Unable to access network'); 69 phantom.exit(1); 70 } else { 71 waitFor(function() { 72 return page.evaluate(function() { 73 var el = document.getElementById('qunit-testresult'); 74 if (el && el.innerText.match('completed')) { 75 return true; 76 } 77 return false; 78 }); 79 }, function() { 80 var failedNum = page.evaluate(function() { 81 var el = document.getElementById('qunit-testresult'); 82 console.log(el.innerText); 83 try { 84 return el.getElementsByClassName('failed')[0].innerHTML; 85 } catch (e) { } 86 return 10000; 87 }); 88 phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0); 89 }); 90 } 91}); 92