Path: blob/master/6-Selenium/phantomjs/examples/run-jasmine2.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 access network");53phantom.exit();54} else {55waitFor(function(){56return page.evaluate(function(){57return document.body.querySelector('.symbolSummary .pending') === null58});59}, function(){60var exitCode = page.evaluate(function(){61console.log('');6263var el = document.body.querySelector('.banner');64var banner = el.querySelector('.title').innerText + " " +65el.querySelector('.version').innerText + " " +66el.querySelector('.duration').innerText;67console.log(banner);6869var list = document.body.querySelectorAll('.results > .failures > .spec-detail.failed');70if (list && list.length > 0) {71console.log('');72console.log(list.length + ' test(s) FAILED:');73for (i = 0; i < list.length; ++i) {74var el = list[i],75desc = el.querySelector('.description'),76msg = el.querySelector('.messages > .result-message');77console.log('');78console.log(desc.innerText);79console.log(msg.innerText);80console.log('');81}82return 1;83} else {84console.log(document.body.querySelector('.alert > .bar.passed').innerText);85return 0;86}87});88phantom.exit(exitCode);89});90}91});929394