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