b84c0c20f5c7f876374baf306751dda52f66d271
[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 page.viewportSize = {
18     width: 1200,
19     height: 1000
20 };
21
22 var run_time = 8; // poll up to seconds
23 if (system.args[2] && parseFloat(system.args[2]) > 0){
24     run_time = parseFloat(system.args[2] );
25 }
26
27 /************************/
28
29 function wait_for_jasmine(checkFx, readyFx, failFx, timeout) {
30     var max_timeout = timeout ? timeout : run_time * 1000,
31         start = new Date().getTime(),
32         result,
33         condition = false;
34
35     var interval = setInterval(function() {
36         console.log(".");
37
38         // success
39         if (condition) {
40             // console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
41             result.time = (new Date().getTime() - start);
42             readyFx(result);
43             clearInterval(interval);
44             phantom.exit(0);
45         }
46
47         // timeout
48         else if ( new Date().getTime() - start >= max_timeout ) {
49             result.time = (new Date().getTime() - start);
50             failFx(result);
51             phantom.exit(1);
52         }
53
54         // checking
55         else {
56             result = checkFx();
57             if (result)
58                 condition = result.mkws.jasmine_done;
59         }
60
61     }, 500); //< repeat check every N ms
62 };
63
64
65
66 page.open(url, function (status) {
67     console.log("fetch " + url + " with status: " + status);
68     if (status != 'success') {
69         console.log("Failed to fetch page, give up");
70         phantom.exit(1);
71     }
72
73     console.log("polling MKWS jasmine test status for " + run_time + " seconds");
74
75     var exit = wait_for_jasmine(function () {
76         return page.evaluate(function () {
77             if (!window || !window.$ || !window.mkws) {
78                 return false;
79             } else {
80                 return {
81                     mkws: window.mkws,
82                     html: window.$("html").html(),
83                     duration: window.$(".duration").text(),
84                     passing: window.$(".passingAlert").text()
85                 };
86             }
87         })},
88
89         function(result) {
90             console.log("MKWS tests are successfully done in " + result.time/1000 + " seconds. Hooray!");
91             console.log("jasmine duration: " + result.duration);
92             console.log("jasmine passing: " + result.passing);
93             console.log("mkws: " + result.mkws_js);
94         },
95
96         function (result) {
97             var error_png = "./mkws-error.png";
98             var error_html = "./mkws-error.html";
99
100             console.log("MKWS tests failed after " + result.time/1000 + " seconds");
101             console.log("keep screenshot in '" + error_png + "'");
102             page.render(error_png);
103
104             console.log("keep html DOM in '" + error_html + "'");
105             var html = result.html + "\n\n<!-- mkws: " + JSON.stringify(result.mkws) + " -->\n";
106             var fs = require('fs');
107             fs.write(error_html, html, "wb");
108         },
109         run_time * 1000);
110 });