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