2 var system = require('system');
5 * Wait until the test condition is true or a timeout occurs. Useful for waiting
6 * on a server response or for a ui change (fadeIn, etc.) to occur.
8 * @param testFx javascript condition that evaluates to a boolean,
9 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
10 * as a callback function.
11 * @param onReady what to do when testFx condition is fulfilled,
12 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
13 * as a callback function.
14 * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
16 function waitFor(testFx, onReady, timeOutMillis) {
17 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timeout is 3s
18 start = new Date().getTime(),
20 interval = setInterval(function() {
21 if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
22 // If not time-out yet and condition not yet fulfilled
23 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
26 // If condition still not fulfilled (timeout but condition is 'false')
27 console.log("'waitFor()' timeout");
30 // Condition fulfilled (timeout and/or condition is 'true')
31 console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
32 typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
33 clearInterval(interval); //< Stop this interval
36 }, 100); //< repeat check every 100ms
40 if (system.args.length !== 2) {
41 console.log('Usage: run-jasmine2.js URL');
45 var page = require('webpage').create();
47 // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
48 page.onConsoleMessage = function(msg) {
52 page.open(system.args[1], function(status){
53 if (status !== "success") {
54 console.log("Unable to access network");
58 return page.evaluate(function(){
59 return (document.body.querySelector('.symbolSummary .pending') === null &&
60 document.body.querySelector('.duration') !== null);
63 var exitCode = page.evaluate(function(){
66 var title = 'Jasmine';
67 var version = document.body.querySelector('.version').innerText;
68 var duration = document.body.querySelector('.duration').innerText;
69 var banner = title + ' ' + version + ' ' + duration;
72 var list = document.body.querySelectorAll('.results > .failures > .spec-detail.failed');
73 if (list && list.length > 0) {
75 console.log(list.length + ' test(s) FAILED:');
76 for (i = 0; i < list.length; ++i) {
78 desc = el.querySelector('.description'),
79 msg = el.querySelector('.messages > .result-message');
81 console.log(desc.innerText);
82 console.log(msg.innerText);
87 console.log(document.body.querySelector('.alert > .bar.passed,.alert > .bar.skipped').innerText);
91 phantom.exit(exitCode);