2 Fetch a mkws/jasmine based page into node.js, evaluate the page and check if test status
3 This should make it possible to run the test on the command line in jenkins. e.g.:
5 phantomjs evaluate.js https://mkws-dev.indexdata.com/jasmine-local-popup.html
8 var page = require('webpage').create(),
9 system = require('system');
11 if (system.args.length === 1) {
12 console.log('Usage: screenshot.js <some URL>');
15 var url = system.args[1];
17 var run_time = 8; // poll up to seconds
18 if (system.args[2] && parseFloat(system.args[2]) > 0) {
19 run_time = parseFloat(system.args[2]);
27 // 0: silent, 1: some infos, 2: display console.log() output
29 if (system.env['DEBUG'] != 'undefined' && parseInt(system.env['DEBUG']) != NaN) {
30 debug = system.env['DEBUG'];
31 if (debug > 0) console.log("reset debug level to: " + debug);
34 /************************/
36 function wait_for_jasmine(checkFx, readyFx, failFx, timeout) {
37 var max_timeout = timeout ? timeout : run_time * 1000,
38 start = new Date().getTime(),
39 result, condition = false;
41 var interval = setInterval(function () {
42 if (debug == 1) console.log(".");
46 // console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
47 result.time = (new Date().getTime() - start);
49 clearInterval(interval);
54 else if (new Date().getTime() - start >= max_timeout) {
55 result.time = (new Date().getTime() - start);
63 if (result) condition = result.mkws.jasmine_done;
66 }, 500); //< repeat check every N ms
69 // redirect webkit console.log() output
70 page.onConsoleMessage = function (message) {
71 if (debug >= 2) console.log(message);
75 page.onAlert = function (msg) {
76 console.log("Alert: " + msg);
80 page.open(url, function (status) {
81 if (debug >= 1) console.log("fetch " + url + " with status: " + status);
83 if (status != 'success') {
84 console.log("Failed to fetch page, give up");
88 if (debug >= 1) console.log("polling MKWS jasmine test status for " + run_time + " seconds");
91 var exit = wait_for_jasmine(function () {
92 return page.evaluate(function () {
93 if (!window || !window.$ || !window.mkws) {
98 html: window.$("html").html(),
99 duration: window.$(".duration").text(),
100 passing: window.$(".passingAlert").text()
107 if (debug < 1) return;
110 console.log("MKWS tests are successfully done in " + result.time / 1000 + " seconds. Hooray!");
111 console.log("jasmine duration: " + result.duration);
112 console.log("jasmine passing: " + result.passing);
116 var error_png = "./mkws-error.png";
117 var error_html = "./mkws-error.html";
119 console.log("MKWS tests failed after " + result.time / 1000 + " seconds");
120 console.log("keep screenshot in '" + error_png + "'");
121 page.render(error_png);
123 console.log("keep html DOM in '" + error_html + "'");
124 var html = result.html + "\n\n<!-- mkws: " + JSON.stringify(result.mkws) + " -->\n";
125 var fs = require('fs');
126 fs.write(error_html, html, "wb");