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