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