+// 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(".");
+
+ // success
+ if (condition) {
+ // console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
+ result.time = (new Date().getTime() - start);
+ readyFx(result);
+ clearInterval(interval);
+ phantom.exit(0);
+ }
+
+ // 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.mkws.jasmine_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);
+};
+