From 71bd808e4f800511bfc8eef475ee3431b75089bb Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 1 Jun 2017 00:09:39 +0200 Subject: [PATCH] Send everything through the test global function --- TestSelenium.js | 62 ++++++++++++++++++++++---------------- www/assert/main.js | 8 ++++- www/common/test.js | 74 +++++++++++++++++++++++++--------------------- 3 files changed, 85 insertions(+), 59 deletions(-) diff --git a/TestSelenium.js b/TestSelenium.js index e4e9b8a4b..562fb29c1 100644 --- a/TestSelenium.js +++ b/TestSelenium.js @@ -22,43 +22,55 @@ if (process.env.SAUCE_USERNAME !== undefined) { driver = new WebDriver.Builder().withCapabilities({ browserName: "chrome" }).build(); } -var SC_GET_LOGS = "return (window.__CRYPTPAD_TEST__) ? window.__CRYPTPAD_TEST__.getLogs() : '[]'"; +var SC_GET_DATA = "return (window.__CRYPTPAD_TEST__) ? window.__CRYPTPAD_TEST__.getData() : '[]'"; +var failed = false; var nt = nThen; [ '/assert/#?test=test', '/auth/#?test=test' ].forEach(function (path) { + if (failed) { return; } var url = 'http://localhost:3000' + path; - nt = nThen(function (waitFor) { + nt = nt(function (waitFor) { + var done = waitFor(); + console.log('\n\n-----TEST ' + url + ' -----'); driver.get(url); - var done = false; - var logMore = function (cb) { - if (done) { return; } - driver.executeScript(SC_GET_LOGS).then(waitFor(function (logs) { - JSON.parse(logs).forEach(function (l) { console.log('>' + l); }); - if (cb) { cb(); } else { setTimeout(logMore, 50); } + var waitTo = setTimeout(function () { + console.log("no report in 10 seconds, timing out"); + failed = true; + }, 10000); + var logMore = function () { + driver.executeScript(SC_GET_DATA).then(waitFor(function (dataS) { + var data = JSON.parse(dataS); + data.forEach(function (d) { + if (d.type !== 'log') { return; } + console.log('>' + d.val); + }); + data.forEach(function (d) { + if (d.type !== 'report') { return; } + console.log('RESULT: ' + d.val); + if (d.val !== 'passed') { + if (d.error) { + console.log(d.error.message); + console.log(d.error.stack); + } + failed = true; + } + clearTimeout(waitTo); + console.log('-----END TEST ' + url + ' -----'); + done(); + done = undefined; + }); + if (done) { setTimeout(logMore, 50); } })); }; logMore(); - driver.wait(WebDriver.until.elementLocated(WebDriver.By.className("report")), 5000); - var report = driver.wait(WebDriver.until.elementLocated(WebDriver.By.className("report")), 5000); - report.getAttribute("class").then(waitFor(function (cls) { - report.getText().then(waitFor(function (text) { - logMore(function () { done = true; }); - console.log("\n-----\n" + url + ' ' + text + "\n-----"); - if (!cls) { - throw new Error("cls is null"); - } else if (cls.indexOf("failure") !== -1) { - throw new Error("cls contains the word failure"); - } else if (cls.indexOf("success") === -1) { - throw new Error("cls does not contain the word success"); - } - })); - })); }).nThen; }); -nt(function () { - driver.quit(); +nt(function (waitFor) { + driver.quit().then(waitFor(function () { + if (failed) { process.exit(100); } + })); }); \ No newline at end of file diff --git a/www/assert/main.js b/www/assert/main.js index 560d10fd8..66ac5eb7b 100644 --- a/www/assert/main.js +++ b/www/assert/main.js @@ -5,7 +5,7 @@ define([ 'json.sortify', '/common/cryptpad-common.js', '/common/test.js' -], function ($, Hyperjson, TextPatcher, Sortify, Cryptpad) { +], function ($, Hyperjson, TextPatcher, Sortify, Cryptpad, Test) { window.Hyperjson = Hyperjson; window.TextPatcher = TextPatcher; window.Sortify = Sortify; @@ -268,6 +268,12 @@ The test returned: var $report = $('.report'); $report.addClass(failed?'failure':'success'); + + if (failed) { + Test.failed(); + } else { + Test.passed(); + } }); }); diff --git a/www/common/test.js b/www/common/test.js index cc5bc2844..b56237689 100644 --- a/www/common/test.js +++ b/www/common/test.js @@ -1,39 +1,13 @@ define([], function () { var out = function () { }; - out.passed = out; - var mkReport = function (list, pass) { - var rpt = document.createElement('div'); - rpt.textContent = JSON.stringify(list); - rpt.setAttribute('class', 'report ' + (pass ? 'success' : 'failure')); - rpt.setAttribute('style', 'display:none;'); - document.body.appendChild(rpt); - }; + out.passed = out.failed = out; if (window.location.hash.indexOf("?test=test") > -1) { - window.onerror = function (msg, url, lineNo, columnNo, e) { - mkReport([ - msg, - url, - lineNo, - columnNo, - e ? e.message : null, - e ? e.stack : null - ]); - }; - require.onError = function (e) { - mkReport([ - e ? e.message : null, - e ? e.stack : null - ]); - }; - out = function (f) { f(); }; - out.passed = function () { mkReport("Test Passed", true); }; - var cpt = window.__CRYPTPAD_TEST__ = { - logs: [], - getLogs: function () { - var logs = JSON.stringify(cpt.logs); - cpt.logs = []; - return logs; + data: [], + getData: function () { + var data = JSON.stringify(cpt.data); + cpt.data = []; + return data; } }; @@ -52,13 +26,47 @@ define([], function () { } var out = [s]; try { throw new Error(); } catch (e) { out.push(e.stack.split('\n')[3]); } - window.__CRYPTPAD_TEST__.logs.push(out); + cpt.data.push({ type: 'log', val: out.join('') }); }; window.console._error = window.console.error; window.console._log = window.console.log; window.console.error = function (e) { window.console._error(e); doLog(e); }; window.console.log = function (l) { window.console._log(l); doLog(l); }; + + window.onerror = function (msg, url, lineNo, columnNo, e) { + cpt.data.push({ + type: 'report', + val: 'failed', + error: { + message: e ? e.message : msg, + stack: e ? e.stack : (url + ":" + lineNo) + } + }); + }; + require.onError = function (e) { + cpt.data.push({ + type: 'report', + val: 'failed', + error: { message: e.message, stack: e.stack } + }); + }; + out = function (f) { f(); }; + out.passed = function () { + cpt.data.push({ + type: 'report', + val: 'passed' + }); + }; + out.failed = function (reason) { + var e; + try { throw new Error(reason); } catch (err) { e = err; } + cpt.data.push({ + type: 'report', + val: 'failed', + error: { message: e.message, stack: e.stack } + }); + }; } return out; }); \ No newline at end of file