move log buffers into session, start logging implicitely

This commit is contained in:
Bruno Windels 2018-08-07 17:09:43 +02:00
parent 7c91ecab7e
commit 6b843eacfc
2 changed files with 41 additions and 28 deletions

View file

@ -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() {

View file

@ -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);
}