extract and display number of tests, and duration
[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 page.open(url, function (status) {
24     console.log("fetch " + url + " with status: " + status);
25     console.log("polling MKWS test status...");
26
27     var r;
28     for (var i = 1; i <= run_time; i++) {
29         setTimeout(function () {
30             var result = page.evaluate(function (s) {
31                 // return document.querySelector(s).innerText;
32                 return {
33                     mkws: window.mkws,
34                     duration: window.$(".duration").text(),
35                     passing: window.$(".passingAlert").text()
36                 };
37             }, 'title');
38
39             console.log(".");
40             if (result.mkws.jasmine_done) {
41                 console.log("MKWS tests are successfully done. Hooray!");
42                 console.log("duration: " + result.duration);
43                 console.log("passing: " + result.passing);
44                 phantom.exit(0);
45             }
46             r = result;
47         }, i * 1000);
48     }
49
50
51     setTimeout(function () {
52         var error_png = "./mkws-error.png";
53         console.log("MKWS tests failed after " + run_time + " seconds");
54         console.log("keep screenshot in '" + error_png + "'");
55
56         page.render(error_png);
57         phantom.exit(1);
58     }, (run_time + 1) * 1000);
59 });