blob: c1d7a5bb8015e31de672ea6cfc188d8e20ef829e [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
Nobuaki Sukegawa2c4edd82015-05-09 19:04:46 +090011var system = require('system');
12
Roger Meier0b267252011-07-29 21:08:04 +000013
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() {
Kazuki Matsudab909a382016-02-13 19:36:09 +090031 if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
Roger Meier0b267252011-07-29 21:08:04 +000032 // If not time-out yet and condition not yet fulfilled
Kazuki Matsudab909a382016-02-13 19:36:09 +090033 condition = (typeof(testFx) === 'string' ? eval(testFx) : testFx()); //< defensive code
Roger Meier0b267252011-07-29 21:08:04 +000034 } else {
Kazuki Matsudab909a382016-02-13 19:36:09 +090035 if (!condition) {
Roger Meier0b267252011-07-29 21:08:04 +000036 // 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')
Kazuki Matsudab909a382016-02-13 19:36:09 +090041 console.log("'waitFor()' finished in " + (new Date().getTime() - start) + 'ms.');
42 if (typeof(onReady) === 'string') {
Henrique Mendonça095ddb72013-09-20 19:38:03 +020043 eval(onReady);
44 } else {
45 onReady(); //< Do what it's supposed to do once the condition is fulfilled
Roger Meier08562732015-06-14 22:30:22 +020046 }
Roger Meier0b267252011-07-29 21:08:04 +000047 clearInterval(interval); //< Stop this interval
48 }
49 }
50 }, 100); //< repeat check every 250ms
Henrique Mendonça095ddb72013-09-20 19:38:03 +020051}
Roger Meier0b267252011-07-29 21:08:04 +000052
53
Nobuaki Sukegawa2c4edd82015-05-09 19:04:46 +090054if (system.args.length === 1 || system.args.length > 3) {
Roger Meier0b267252011-07-29 21:08:04 +000055 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
Kazuki Matsudab909a382016-02-13 19:36:09 +090066page.open(system.args[1], function(status) {
67 if (status !== 'success') {
68 console.log('Unable to access network');
Roger Meier0b267252011-07-29 21:08:04 +000069 phantom.exit(1);
70 } else {
Kazuki Matsudab909a382016-02-13 19:36:09 +090071 waitFor(function() {
72 return page.evaluate(function() {
Roger Meier0b267252011-07-29 21:08:04 +000073 var el = document.getElementById('qunit-testresult');
74 if (el && el.innerText.match('completed')) {
75 return true;
76 }
77 return false;
78 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090079 }, function() {
80 var failedNum = page.evaluate(function() {
Roger Meier0b267252011-07-29 21:08:04 +000081 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});