Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1 | /*jshint evil:true*/ |
| 2 | |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 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 | |
Nobuaki Sukegawa | 2c4edd8 | 2015-05-09 19:04:46 +0900 | [diff] [blame] | 11 | var system = require('system'); |
| 12 | |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 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 | */ |
| 26 | function 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 Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 31 | if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) { |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 32 | // If not time-out yet and condition not yet fulfilled |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 33 | condition = (typeof(testFx) === 'string' ? eval(testFx) : testFx()); //< defensive code |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 34 | } else { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 35 | if (!condition) { |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 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') |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 41 | console.log("'waitFor()' finished in " + (new Date().getTime() - start) + 'ms.'); |
| 42 | if (typeof(onReady) === 'string') { |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 43 | eval(onReady); |
| 44 | } else { |
| 45 | onReady(); //< Do what it's supposed to do once the condition is fulfilled |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 46 | } |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 47 | clearInterval(interval); //< Stop this interval |
| 48 | } |
| 49 | } |
| 50 | }, 100); //< repeat check every 250ms |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 51 | } |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 52 | |
| 53 | |
Nobuaki Sukegawa | 2c4edd8 | 2015-05-09 19:04:46 +0900 | [diff] [blame] | 54 | if (system.args.length === 1 || system.args.length > 3) { |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 55 | console.log('Usage: phantomjs phantomjs-qunit.js URL'); |
| 56 | phantom.exit(1); |
| 57 | } |
| 58 | |
| 59 | var page = new WebPage(); |
| 60 | |
| 61 | // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") |
| 62 | page.onConsoleMessage = function(msg) { |
| 63 | console.log(msg); |
| 64 | }; |
| 65 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 66 | page.open(system.args[1], function(status) { |
| 67 | if (status !== 'success') { |
| 68 | console.log('Unable to access network'); |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 69 | phantom.exit(1); |
| 70 | } else { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 71 | waitFor(function() { |
| 72 | return page.evaluate(function() { |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 73 | var el = document.getElementById('qunit-testresult'); |
| 74 | if (el && el.innerText.match('completed')) { |
| 75 | return true; |
| 76 | } |
| 77 | return false; |
| 78 | }); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 79 | }, function() { |
| 80 | var failedNum = page.evaluate(function() { |
Roger Meier | 0b26725 | 2011-07-29 21:08:04 +0000 | [diff] [blame] | 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 | }); |