rewrite test to display better error messages and avoid race conditions, MKWS-135
[mkws-moved-to-github.git] / test / phantom / run-jasmine.js
1 /*
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.:
4
5       phantomjs evaluate.js https://mkws-dev.indexdata.com/jasmine-local-popup.html
6 */
7
8 var page = require('webpage').create(),
9     system = require('system');
10
11 if (system.args.length === 1) {
12     console.log('Usage: screenshot.js <some URL>');
13     phantom.exit();
14 }
15 var url = system.args[1];
16
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]);
20 }
21
22 page.viewportSize = {
23     width: 1200,
24     height: 1000
25 };
26
27 // 0: silent, 1: some infos,  2: display console.log() output
28 var debug = 2;
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);
32 }
33
34 /************************/
35
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;
40
41     var interval = setInterval(function () {
42         if (debug == 1) console.log(".");
43
44         // done
45         if (condition) {
46             clearInterval(interval);
47             result.time = (new Date().getTime() - start);
48             result.failed ? failFx(result) : readyFx(result);
49             phantom.exit(result.failed == 0 ? 0 : 2);
50         }
51
52         // timeout
53         else if (new Date().getTime() - start >= max_timeout) {
54             result.time = (new Date().getTime() - start);
55             failFx(result);
56             phantom.exit(1);
57         }
58
59         // checking
60         else {
61             result = checkFx();
62             if (result) condition = result.done;
63         }
64
65     }, 500); //< repeat check every N ms
66 };
67
68 // redirect webkit console.log() output
69 page.onConsoleMessage = function (message) {
70     if (debug >= 2) console.log(message);
71 };
72
73 // cat webkit alert()
74 page.onAlert = function (msg) {
75     console.log("Alert: " + msg);
76 };
77
78 // display HTTP errors
79 page.onResourceError = function (resourceError) {
80     // console.log('phantomjs error code: ' + resourceError.errorCode);
81     console.log(resourceError.errorString);
82     phantom.exit(3);
83 };
84
85 page.open(url, function (status) {
86     if (debug >= 1) console.log("fetch " + url + " with status: " + status);
87
88     if (status != 'success') {
89         console.log("Failed to fetch page, give up. Network error?");
90         phantom.exit(1);
91     }
92
93     if (debug >= 1) console.log("polling MKWS jasmine test status for " + run_time + " seconds");
94
95
96     var exit = wait_for_jasmine(function () {
97         return page.evaluate(function () {
98             if (!window || !window.$ || !window.mkws) {
99                 console.log("No window object found");
100                 return false;
101             }
102
103             var $ = window.$;
104             var error_msg = [""];
105             var passing = $(".passingAlert").text() || window.$(".failingAlert").text();
106
107             // extract failed tests
108             var list = $('.results > #details > .specDetail.failed');
109             if (list && list.length > 0) {
110                 error_msg.push("==> " + list.length + ' test(s) FAILED:');
111                 for (i = 0; i < list.length; ++i) {
112                     var el = list[i],
113                         desc = el.querySelector('.description'),
114                         msg = el.querySelector('.resultMessage.fail');
115                     error_msg.push($(desc).text());
116                     error_msg.push($(msg).text());
117                 }
118             }
119
120             return {
121                 mkws: window.mkws,
122                 done: $('.symbolSummary .pending').length == 0,
123                 html: $("html").html(),
124                 duration: $(".duration").text(),
125                 error_msg: error_msg,
126                 failed: list.length,
127                 passing: passing
128             };
129         })
130     },
131
132     function (result) {
133         if (debug < 1) return;
134
135         console.log("");
136         console.log("MKWS tests are successfully done in " + result.time / 1000 + " seconds. Hooray!");
137         console.log("jasmine duration: " + result.duration);
138         console.log("jasmine passing: " + result.passing);
139     },
140
141     function (result) {
142         var error_png = "./mkws-error.png";
143         var error_html = "./mkws-error.html";
144
145         console.log("MKWS tests failed after " + result.time / 1000 + " seconds");
146         console.log(result.error_msg.join("\n"));
147         console.log("keep screenshot in '" + error_png + "'");
148         page.render(error_png);
149
150         console.log("keep html DOM in '" + error_html + "'");
151         console.log("you may run: lynx -nolist -dump " + error_html);
152         var html = result.html + "\n\n<!-- mkws: " + JSON.stringify(result.mkws) + " -->\n";
153         var fs = require('fs');
154         fs.write(error_html, html, "wb");
155     }, run_time * 1000);
156 });