From 6b843eacfcf76be987273e63100e11b446a7102b Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 7 Aug 2018 17:09:43 +0200 Subject: [PATCH] move log buffers into session, start logging implicitely --- src/session.js | 37 +++++++++++++++++++++++++++---------- start.js | 32 ++++++++++++++------------------ 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/session.js b/src/session.js index 51bad4a3c9..f8f41b20b8 100644 --- a/src/session.js +++ b/src/session.js @@ -16,12 +16,33 @@ limitations under the License. const puppeteer = require('puppeteer'); +class LogBuffer { + constructor(page, eventName, eventMapper, reduceAsync=false, initialValue = "") { + this.buffer = initialValue; + page.on(eventName, (arg) => { + const result = eventMapper(arg); + if (reduceAsync) { + result.then((r) => this.buffer += r); + } + else { + this.buffer += result; + } + }); + } +} + module.exports = class RiotSession { constructor(browser, page, username, riotserver) { this.browser = browser; this.page = page; this.riotserver = riotserver; this.username = username; + this.consoleLog = new LogBuffer(page, "console", (msg) => `${msg.text()}\n`); + this.networkLog = new LogBuffer(page, "requestfinished", async (req) => { + const type = req.resourceType(); + const response = await req.response(); + return `${type} ${response.status()} ${req.method()} ${req.url()} \n`; + }, true); } static async create(username, puppeteerOptions, riotserver) { @@ -48,16 +69,12 @@ module.exports = class RiotSession { return await text_handle.jsonValue(); } - logConsole() { - let buffer = ""; - this.page.on('console', msg => { - buffer += msg.text() + '\n'; - }); - return { - logs() { - return buffer; - } - } + consoleLogs() { + return this.consoleLog.buffer; + } + + networkLogs() { + return this.networkLog.buffer; } logXHRRequests() { diff --git a/start.js b/start.js index c1aecb65aa..0aa2cb9364 100644 --- a/start.js +++ b/start.js @@ -25,9 +25,7 @@ const acceptServerNoticesInviteAndConsent = require('./src/tests/server-notices- const homeserver = 'http://localhost:8008'; const riotserver = 'http://localhost:5000'; -let consoleLogs = null; -let xhrLogs = null; -let globalPage = null; +let sessions = []; async function runTests() { console.log("running tests ..."); @@ -39,9 +37,7 @@ async function runTests() { } const alice = await RiotSession.create("alice", options, riotserver); - - consoleLogs = alice.logConsole(); - xhrLogs = alice.logXHRRequests(); + sessions.push(alice); process.stdout.write(`* signing up as ${alice.username} ... `); await signup(alice, alice.username, 'testtest'); @@ -65,19 +61,19 @@ function onSuccess() { } async function onFailure(err) { - - let documentHtml = "no page"; - if (globalPage) { - documentHtml = await globalPage.content(); - } - console.log('failure: ', err); - console.log('console.log output:'); - console.log(consoleLogs.logs()); - console.log('XHR requests:'); - console.log(xhrLogs.logs()); - console.log('document html:'); - console.log(documentHtml); + for(var i = 0; i < sessions.length; ++i) { + const session = sessions[i]; + documentHtml = await session.page.content(); + console.log(`---------------- START OF ${session.username} LOGS ----------------`); + console.log('---------------- console.log output:'); + console.log(session.consoleLogs()); + console.log('---------------- network requests:'); + console.log(session.networkLogs()); + console.log('---------------- document html:'); + console.log(documentHtml); + console.log(`---------------- END OF ${session.username} LOGS ----------------`); + } process.exit(-1); }