Path: blob/master/6-Selenium/phantomjs/examples/run-jasmine.js
164 views
var system = require('system');12/**3* Wait until the test condition is true or a timeout occurs. Useful for waiting4* on a server response or for a ui change (fadeIn, etc.) to occur.5*6* @param testFx javascript condition that evaluates to a boolean,7* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or8* as a callback function.9* @param onReady what to do when testFx condition is fulfilled,10* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or11* as a callback function.12* @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.13*/14function waitFor(testFx, onReady, timeOutMillis) {15var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timeout is 3s16start = new Date().getTime(),17condition = false,18interval = setInterval(function() {19if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {20// If not time-out yet and condition not yet fulfilled21condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code22} else {23if(!condition) {24// If condition still not fulfilled (timeout but condition is 'false')25console.log("'waitFor()' timeout");26phantom.exit(1);27} else {28// Condition fulfilled (timeout and/or condition is 'true')29console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");30typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled31clearInterval(interval); //< Stop this interval32}33}34}, 100); //< repeat check every 100ms35};363738if (system.args.length !== 2) {39console.log('Usage: run-jasmine.js URL');40phantom.exit(1);41}4243var page = require('webpage').create();4445// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")46page.onConsoleMessage = function(msg) {47console.log(msg);48};4950page.open(system.args[1], function(status){51if (status !== "success") {52console.log("Unable to open " + system.args[1]);53phantom.exit(1);54} else {55waitFor(function(){56return page.evaluate(function(){57return document.body.querySelector('.symbolSummary .pending') === null58});59}, function(){60var exitCode = page.evaluate(function(){61try {62console.log('');63console.log(document.body.querySelector('.description').innerText);64var list = document.body.querySelectorAll('.results > #details > .specDetail.failed');65if (list && list.length > 0) {66console.log('');67console.log(list.length + ' test(s) FAILED:');68for (i = 0; i < list.length; ++i) {69var el = list[i],70desc = el.querySelector('.description'),71msg = el.querySelector('.resultMessage.fail');72console.log('');73console.log(desc.innerText);74console.log(msg.innerText);75console.log('');76}77return 1;78} else {79console.log(document.body.querySelector('.alert > .passingAlert.bar').innerText);80return 0;81}82} catch (ex) {83console.log(ex);84return 1;85}86});87phantom.exit(exitCode);88});89}90});919293