rewrite test to display better error messages and avoid race conditions, MKWS-135
authorWolfram Schneider <wosch@indexdata.dk>
Fri, 14 Mar 2014 14:21:42 +0000 (14:21 +0000)
committerWolfram Schneider <wosch@indexdata.dk>
Fri, 14 Mar 2014 14:21:42 +0000 (14:21 +0000)
test/phantom/run-jasmine.js [new file with mode: 0644]

diff --git a/test/phantom/run-jasmine.js b/test/phantom/run-jasmine.js
new file mode 100644 (file)
index 0000000..a1f8704
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+    Fetch a mkws/jasmine based page into node.js, evaluate the page and check if test status
+    This should make it possible to run the test on the command line in jenkins.  e.g.:
+
+      phantomjs evaluate.js https://mkws-dev.indexdata.com/jasmine-local-popup.html
+*/
+
+var page = require('webpage').create(),
+    system = require('system');
+
+if (system.args.length === 1) {
+    console.log('Usage: screenshot.js <some URL>');
+    phantom.exit();
+}
+var url = system.args[1];
+
+var run_time = 8; // poll up to seconds
+if (system.args[2] && parseFloat(system.args[2]) > 0) {
+    run_time = parseFloat(system.args[2]);
+}
+
+page.viewportSize = {
+    width: 1200,
+    height: 1000
+};
+
+// 0: silent, 1: some infos,  2: display console.log() output
+var debug = 2;
+if (typeof system.env['DEBUG'] != 'undefined' && parseInt(system.env['DEBUG']) != NaN) {
+    debug = system.env['DEBUG'];
+    if (debug > 0) console.log("reset debug level to: " + debug);
+}
+
+/************************/
+
+function wait_for_jasmine(checkFx, readyFx, failFx, timeout) {
+    var max_timeout = timeout ? timeout : run_time * 1000,
+        start = new Date().getTime(),
+        result, condition = false;
+
+    var interval = setInterval(function () {
+        if (debug == 1) console.log(".");
+
+        // done
+        if (condition) {
+            clearInterval(interval);
+            result.time = (new Date().getTime() - start);
+            result.failed ? failFx(result) : readyFx(result);
+            phantom.exit(result.failed == 0 ? 0 : 2);
+        }
+
+        // timeout
+        else if (new Date().getTime() - start >= max_timeout) {
+            result.time = (new Date().getTime() - start);
+            failFx(result);
+            phantom.exit(1);
+        }
+
+        // checking
+        else {
+            result = checkFx();
+            if (result) condition = result.done;
+        }
+
+    }, 500); //< repeat check every N ms
+};
+
+// redirect webkit console.log() output
+page.onConsoleMessage = function (message) {
+    if (debug >= 2) console.log(message);
+};
+
+// cat webkit alert()
+page.onAlert = function (msg) {
+    console.log("Alert: " + msg);
+};
+
+// display HTTP errors
+page.onResourceError = function (resourceError) {
+    // console.log('phantomjs error code: ' + resourceError.errorCode);
+    console.log(resourceError.errorString);
+    phantom.exit(3);
+};
+
+page.open(url, function (status) {
+    if (debug >= 1) console.log("fetch " + url + " with status: " + status);
+
+    if (status != 'success') {
+        console.log("Failed to fetch page, give up. Network error?");
+        phantom.exit(1);
+    }
+
+    if (debug >= 1) console.log("polling MKWS jasmine test status for " + run_time + " seconds");
+
+
+    var exit = wait_for_jasmine(function () {
+        return page.evaluate(function () {
+            if (!window || !window.$ || !window.mkws) {
+                console.log("No window object found");
+                return false;
+            }
+
+            var $ = window.$;
+            var error_msg = [""];
+            var passing = $(".passingAlert").text() || window.$(".failingAlert").text();
+
+            // extract failed tests
+            var list = $('.results > #details > .specDetail.failed');
+            if (list && list.length > 0) {
+                error_msg.push("==> " + list.length + ' test(s) FAILED:');
+                for (i = 0; i < list.length; ++i) {
+                    var el = list[i],
+                        desc = el.querySelector('.description'),
+                        msg = el.querySelector('.resultMessage.fail');
+                    error_msg.push($(desc).text());
+                    error_msg.push($(msg).text());
+                }
+            }
+
+            return {
+                mkws: window.mkws,
+                done: $('.symbolSummary .pending').length == 0,
+                html: $("html").html(),
+                duration: $(".duration").text(),
+                error_msg: error_msg,
+                failed: list.length,
+                passing: passing
+            };
+        })
+    },
+
+    function (result) {
+        if (debug < 1) return;
+
+        console.log("");
+        console.log("MKWS tests are successfully done in " + result.time / 1000 + " seconds. Hooray!");
+        console.log("jasmine duration: " + result.duration);
+        console.log("jasmine passing: " + result.passing);
+    },
+
+    function (result) {
+        var error_png = "./mkws-error.png";
+        var error_html = "./mkws-error.html";
+
+        console.log("MKWS tests failed after " + result.time / 1000 + " seconds");
+        console.log(result.error_msg.join("\n"));
+        console.log("keep screenshot in '" + error_png + "'");
+        page.render(error_png);
+
+        console.log("keep html DOM in '" + error_html + "'");
+        console.log("you may run: lynx -nolist -dump " + error_html);
+        var html = result.html + "\n\n<!-- mkws: " + JSON.stringify(result.mkws) + " -->\n";
+        var fs = require('fs');
+        fs.write(error_html, html, "wb");
+    }, run_time * 1000);
+});