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 (typeof 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 clearInterval(interval);
47 result.time = (new Date().getTime() - start);
48 result.failed ? failFx(result) : readyFx(result);
50 // See: https://github.com/ariya/phantomjs/issues/12697
53 setTimeout(function () {
54 phantom.exit(result.failed == 0 ? 0 : 2);
59 else if (new Date().getTime() - start >= max_timeout) {
60 result.time = (new Date().getTime() - start);
68 if (result) condition = result.done;
71 }, 500); //< repeat check every N ms
74 function dump_html(file) {
75 // not yet implemented
76 var spawn = require('child_process').spawn,
77 lynx = spawn("lynx", ["-nolist", "-dump", file]);
79 lynx.stdout.on('data', function (data) {
80 console.log('lynx >> ' + data);
83 // lynx.stderr.on('data', function (data) { console.log('stderr: ' + data); });
84 // lynx.on('close', function (code) { console.log('child process exited with code ' + code) });
87 // redirect webkit console.log() output
88 page.onConsoleMessage = function (message) {
89 if (debug >= 2) console.log(message);
93 page.onAlert = function (msg) {
94 console.log("Alert: " + msg);
97 // display HTTP errors
98 page.onResourceError = function (resourceError) {
99 // console.log('phantomjs error code: ' + resourceError.errorCode);
100 console.log(resourceError.errorString);
104 page.open(url, function (status) {
105 if (debug >= 1) console.log("fetch " + url + " with status: " + status);
107 if (status != 'success') {
108 console.log("Failed to fetch page, give up. Network error?");
112 if (debug >= 1) console.log("polling MKWS jasmine test status for " + run_time + " seconds");
115 var exit = wait_for_jasmine(function () {
116 return page.evaluate(function () {
117 if (!window || !window.mkws || !window.mkws.$) {
118 console.log("No window object found");
122 var $ = window.mkws.$;
123 var error_msg = [""];
124 var passing = $(".passingAlert").text() || $(".failingAlert").text();
126 // extract failed tests
127 var list = $('.results > #details > .specDetail.failed');
128 if (list && list.length > 0) {
129 error_msg.push("==> " + list.length + ' test(s) FAILED:');
130 for (i = 0; i < list.length; ++i) {
132 desc = el.querySelector('.description'),
133 msg = el.querySelector('.resultMessage.fail');
134 error_msg.push($(desc).text());
135 error_msg.push($(msg).text());
141 done: $('.symbolSummary .pending').length == 0,
142 html: $("html").html(),
143 duration: $(".duration").text(),
144 error_msg: error_msg,
145 failed: (list.length > 0 || !passing),
152 if (debug < 1) return;
155 console.log("MKWS tests are successfully done in " + result.time / 1000 + " seconds. Hooray!");
156 console.log("jasmine duration: " + result.duration);
157 console.log("jasmine passing: " + result.passing);
161 var error_png = "./mkws-error.png";
162 var error_html = "./mkws-error.html";
164 var html = result.html + "\n\n<!-- mkws: " + JSON.stringify(result.mkws) + " -->\n";
165 var fs = require('fs');
166 fs.write(error_html, html, "wb");
167 dump_html(error_html);
169 console.log("MKWS tests failed after " + result.time / 1000 + " seconds");
170 console.log(result.error_msg.join("\n"));
171 console.log("keep screenshot in '" + error_png + "'");
172 page.render(error_png);
174 console.log("keep html DOM in '" + error_html + "'");
175 // console.log("you may run: lynx -nolist -dump " + error_html);