52e044dfbed7c68aa4ce988575a64f76457d0691
[mkws-moved-to-github.git] / test / phantom / evaluate.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
30
31 /************************/
32
33 function wait_for_jasmine(checkFx, readyFx, failFx, timeout) {
34     var max_timeout = timeout ? timeout : run_time * 1000,
35         start = new Date().getTime(),
36         result, condition = false;
37
38     var interval = setInterval(function () {
39         if (debug == 1) console.log(".");
40
41         // success
42         if (condition) {
43             // console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
44             result.time = (new Date().getTime() - start);
45             readyFx(result);
46             clearInterval(interval);
47             phantom.exit(0);
48         }
49
50         // timeout
51         else if (new Date().getTime() - start >= max_timeout) {
52             result.time = (new Date().getTime() - start);
53             failFx(result);
54             phantom.exit(1);
55         }
56
57         // checking
58         else {
59             result = checkFx();
60             if (result) condition = result.mkws.jasmine_done;
61         }
62
63     }, 500); //< repeat check every N ms
64 };
65
66 // redirect webkit console.log() output
67 page.onConsoleMessage = function (message) {
68     if (debug >= 2) console.log(message);
69 };
70
71 // cat webkit alert()
72 page.onAlert = function (msg) {
73     console.log("Alert: " + msg);
74 };
75
76
77 page.open(url, function (status) {
78     if (debug >= 1) console.log("fetch " + url + " with status: " + status);
79
80     if (status != 'success') {
81         console.log("Failed to fetch page, give up");
82         phantom.exit(1);
83     }
84
85     if (debug >= 1) console.log("polling MKWS jasmine test status for " + run_time + " seconds");
86
87
88     var exit = wait_for_jasmine(function () {
89         return page.evaluate(function () {
90             if (!window || !window.$ || !window.mkws) {
91                 return false;
92             } else {
93                 return {
94                     mkws: window.mkws,
95                     html: window.$("html").html(),
96                     duration: window.$(".duration").text(),
97                     passing: window.$(".passingAlert").text()
98                 };
99             }
100         })
101     },
102
103     function (result) {
104         if (debug < 1) return;
105
106         console.log("MKWS tests are successfully done in " + result.time / 1000 + " seconds. Hooray!");
107         console.log("jasmine duration: " + result.duration);
108         console.log("jasmine passing: " + result.passing);
109     },
110
111     function (result) {
112         var error_png = "./mkws-error.png";
113         var error_html = "./mkws-error.html";
114
115         console.log("MKWS tests failed after " + result.time / 1000 + " seconds");
116         console.log("keep screenshot in '" + error_png + "'");
117         page.render(error_png);
118
119         console.log("keep html DOM in '" + error_html + "'");
120         var html = result.html + "\n\n<!-- mkws: " + JSON.stringify(result.mkws) + " -->\n";
121         var fs = require('fs');
122         fs.write(error_html, html, "wb");
123     }, run_time * 1000);
124 });