BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / phantomjs-prebuilt / lib / phantom / examples / run-qunit.js

run-qunit.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
run-qunit.js 3.1 KiB Download Raw
1"use strict";
2var system = require('system');
3
4/**
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.
7 *
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.
15 */
16function waitFor(testFx, onReady, timeOutMillis) {
17 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timout is 3s
18 start = new Date().getTime(),
19 condition = false,
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
24 } else {
25 if(!condition) {
26 // If condition still not fulfilled (timeout but condition is 'false')
27 console.log("'waitFor()' timeout");
28 phantom.exit(1);
29 } else {
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
34 }
35 }
36 }, 100); //< repeat check every 250ms
37};
38
39
40if (system.args.length !== 2) {
41 console.log('Usage: run-qunit.js URL');
42 phantom.exit(1);
43}
44
45var page = require('webpage').create();
46
47// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
48page.onConsoleMessage = function(msg) {
49 console.log(msg);
50};
51
52page.open(system.args[1], function(status){
53 if (status !== "success") {
54 console.log("Unable to access network");
55 phantom.exit(1);
56 } else {
57 waitFor(function(){
58 return page.evaluate(function(){
59 var el = document.getElementById('qunit-testresult');
60 if (el && el.innerText.match('completed')) {
61 return true;
62 }
63 return false;
64 });
65 }, function(){
66 var failedNum = page.evaluate(function(){
67 var el = document.getElementById('qunit-testresult');
68 console.log(el.innerText);
69 try {
70 return el.getElementsByClassName('failed')[0].innerHTML;
71 } catch (e) { }
72 return 10000;
73 });
74 phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
75 });
76 }
77});