[36] | 1 | /* global require phantom console document setInterval clearInterval */
|
---|
| 2 | /* eslint no-console: 'off'*/
|
---|
| 3 | 'use strict';
|
---|
| 4 | var system = require('system');
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * Wait until the test condition is true or a timeout occurs. Useful for waiting
|
---|
| 8 | * on a server response or for a ui change (fadeIn, etc.) to occur.
|
---|
| 9 | *
|
---|
| 10 | * @param {func} testFx javascript condition that evaluates to a boolean,
|
---|
| 11 | * it can be passed in as a string (e.g.: "1 == 1" or
|
---|
| 12 | * "$('#bar').is(':visible')" or
|
---|
| 13 | * as a callback function.
|
---|
| 14 | * @param {func} onReady what to do when testFx condition is fulfilled,
|
---|
| 15 | * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')"
|
---|
| 16 | * or as a callback function.
|
---|
| 17 | * @param {long} timeOutMillis the max amount of time to wait.
|
---|
| 18 | * If not specified, 3 sec is used.
|
---|
| 19 | */
|
---|
| 20 | function waitFor(testFx, onReady, timeOutMillis) {
|
---|
| 21 | var maxtimeOutMillis = timeOutMillis ? timeOutMillis
|
---|
| 22 | : 3001;
|
---|
| 23 | var start = new Date().getTime();
|
---|
| 24 | var condition = false;
|
---|
| 25 | var interval = setInterval(function() {
|
---|
| 26 | if ( (new Date().getTime() - start < maxtimeOutMillis)
|
---|
| 27 | && !condition ) {
|
---|
| 28 | // If not time-out yet and condition not yet fulfilled
|
---|
| 29 | condition = (typeof(testFx) === 'string'
|
---|
| 30 | ? eval(testFx)
|
---|
| 31 | : testFx()); // < defensive code
|
---|
| 32 | } else {
|
---|
| 33 | if (!condition) {
|
---|
| 34 | // If condition still not fulfilled
|
---|
| 35 | // (timeout but condition is 'false')
|
---|
| 36 | // console.log("'waitFor()' timeout");
|
---|
| 37 | phantom.exit(1);
|
---|
| 38 | } else {
|
---|
| 39 | // Condition fulfilled (timeout and/or condition is 'true')
|
---|
| 40 | // console.log("'waitFor()' finished in " +
|
---|
| 41 | // (new Date().getTime() - start) + "ms.");
|
---|
| 42 | // < Do what it's supposed to do once
|
---|
| 43 | // the condition is fulfilled
|
---|
| 44 | typeof(onReady) === 'string' ? eval(onReady) : onReady();
|
---|
| 45 | clearInterval(interval); // < Stop this interval
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }, 100); // < repeat check every 100ms
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | if (system.args.length !== 2) {
|
---|
| 53 | console.log('Usage: run-jasmine3.js URL');
|
---|
| 54 | phantom.exit(1);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | var page = require('webpage').create();
|
---|
| 58 |
|
---|
| 59 | // Route "console.log()" calls from within the Page context
|
---|
| 60 | // to the main Phantom context (i.e. current "this")
|
---|
| 61 | page.onConsoleMessage = function(msg) {
|
---|
| 62 | console.log(msg);
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | page.open(system.args[1], function(status) {
|
---|
| 66 | if (status !== 'success') {
|
---|
| 67 | console.log('Unable to access network');
|
---|
| 68 | phantom.exit();
|
---|
| 69 | } else {
|
---|
| 70 | waitFor(function() {
|
---|
| 71 | return page.evaluate(function() {
|
---|
| 72 | return (document.body
|
---|
| 73 | .querySelector('.jasmine-symbol-summary ' +
|
---|
| 74 | '.jasmine-pending') === null &&
|
---|
| 75 | document.body
|
---|
| 76 | .querySelector('.jasmine-duration') !== null);
|
---|
| 77 | });
|
---|
| 78 | }, function() {
|
---|
| 79 | var exitCode = page.evaluate(function() {
|
---|
| 80 | console.log('');
|
---|
| 81 |
|
---|
| 82 | var title = 'Jasmine';
|
---|
| 83 | var version = document.body.querySelector('.jasmine-version')
|
---|
| 84 | .innerText;
|
---|
| 85 | var duration = document.body.querySelector('.jasmine-duration')
|
---|
| 86 | .innerText;
|
---|
| 87 | var banner = title + ' ' + version + ' ' + duration;
|
---|
| 88 | console.log(banner);
|
---|
| 89 |
|
---|
| 90 | var list = document.body
|
---|
| 91 | .querySelectorAll('.jasmine-results > ' +
|
---|
| 92 | '.jasmine-failures > ' +
|
---|
| 93 | '.jasmine-spec-detail' +
|
---|
| 94 | '.jasmine-failed');
|
---|
| 95 | if (list && list.length > 0) {
|
---|
| 96 | console.log('');
|
---|
| 97 | console.log(list.length + ' test(s) FAILED:');
|
---|
| 98 | for (var i = 0; i < list.length; ++i) {
|
---|
| 99 | var el = list[i];
|
---|
| 100 | var desc = el.querySelector('.jasmine-description');
|
---|
| 101 | var msg = el.querySelector('.jasmine-messages > ' +
|
---|
| 102 | '.jasmine-result-message');
|
---|
| 103 | console.log('');
|
---|
| 104 | console.log(desc.innerText);
|
---|
| 105 | console.log(msg.innerText);
|
---|
| 106 | console.log('');
|
---|
| 107 | }
|
---|
| 108 | return 1;
|
---|
| 109 | } else {
|
---|
| 110 | console.log(document.body
|
---|
| 111 | .querySelector('.jasmine-alert > ' +
|
---|
| 112 | '.jasmine-bar.' +
|
---|
| 113 | 'jasmine-passed,' +
|
---|
| 114 | '.jasmine-alert > ' +
|
---|
| 115 | '.jasmine-bar.' +
|
---|
| 116 | 'jasmine-skipped')
|
---|
| 117 | .innerText);
|
---|
| 118 | return 0;
|
---|
| 119 | }
|
---|
| 120 | });
|
---|
| 121 | phantom.exit(exitCode);
|
---|
| 122 | });
|
---|
| 123 | }
|
---|
| 124 | });
|
---|