blob: 027be401026392e8eda499401720bd0e693fae77 [file] [log] [blame]
Henrique Mendonça095ddb72013-09-20 19:38:03 +02001/*jshint evil:true*/
2
Roger Meier0b267252011-07-29 21:08:04 +00003/* 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
11
12/**
13 * Wait until the test condition is true or a timeout occurs. Useful for waiting
14 * on a server response or for a ui change (fadeIn, etc.) to occur.
15 *
16 * @param testFx javascript condition that evaluates to a boolean,
17 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
18 * as a callback function.
19 * @param onReady what to do when testFx condition is fulfilled,
20 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
21 * as a callback function.
22 * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
23 */
24function waitFor(testFx, onReady, timeOutMillis) {
25 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timout is 3s
26 start = new Date().getTime(),
27 condition = false,
28 interval = setInterval(function() {
29 if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
30 // If not time-out yet and condition not yet fulfilled
31 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
32 } else {
33 if(!condition) {
34 // If condition still not fulfilled (timeout but condition is 'false')
35 console.log("'waitFor()' timeout");
36 phantom.exit(1);
37 } else {
38 // Condition fulfilled (timeout and/or condition is 'true')
39 console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
Henrique Mendonça095ddb72013-09-20 19:38:03 +020040 if (typeof(onReady) === "string") {
41 eval(onReady);
42 } else {
43 onReady(); //< Do what it's supposed to do once the condition is fulfilled
44 }
Roger Meier0b267252011-07-29 21:08:04 +000045 clearInterval(interval); //< Stop this interval
46 }
47 }
48 }, 100); //< repeat check every 250ms
Henrique Mendonça095ddb72013-09-20 19:38:03 +020049}
Roger Meier0b267252011-07-29 21:08:04 +000050
51
52if (phantom.args.length === 0 || phantom.args.length > 2) {
53 console.log('Usage: phantomjs phantomjs-qunit.js URL');
54 phantom.exit(1);
55}
56
57var page = new WebPage();
58
59// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
60page.onConsoleMessage = function(msg) {
61 console.log(msg);
62};
63
64page.open(phantom.args[0], function(status){
65 if (status !== "success") {
66 console.log("Unable to access network");
67 phantom.exit(1);
68 } else {
69 waitFor(function(){
70 return page.evaluate(function(){
71 var el = document.getElementById('qunit-testresult');
72 if (el && el.innerText.match('completed')) {
73 return true;
74 }
75 return false;
76 });
77 }, function(){
78 var failedNum = page.evaluate(function(){
79 var el = document.getElementById('qunit-testresult');
80 console.log(el.innerText);
81 try {
82 return el.getElementsByClassName('failed')[0].innerHTML;
83 } catch (e) { }
84 return 10000;
85 });
86 phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
87 });
88 }
89});