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'); 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 { module.exports = class RiotSession {
constructor(browser, page, username, riotserver) { constructor(browser, page, username, riotserver) {
this.browser = browser; this.browser = browser;
this.page = page; this.page = page;
this.riotserver = riotserver; this.riotserver = riotserver;
this.username = username; 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) { static async create(username, puppeteerOptions, riotserver) {
@ -48,16 +69,12 @@ module.exports = class RiotSession {
return await text_handle.jsonValue(); return await text_handle.jsonValue();
} }
logConsole() { consoleLogs() {
let buffer = ""; return this.consoleLog.buffer;
this.page.on('console', msg => {
buffer += msg.text() + '\n';
});
return {
logs() {
return buffer;
}
} }
networkLogs() {
return this.networkLog.buffer;
} }
logXHRRequests() { logXHRRequests() {

View file

@ -25,9 +25,7 @@ const acceptServerNoticesInviteAndConsent = require('./src/tests/server-notices-
const homeserver = 'http://localhost:8008'; const homeserver = 'http://localhost:8008';
const riotserver = 'http://localhost:5000'; const riotserver = 'http://localhost:5000';
let consoleLogs = null; let sessions = [];
let xhrLogs = null;
let globalPage = null;
async function runTests() { async function runTests() {
console.log("running tests ..."); console.log("running tests ...");
@ -39,9 +37,7 @@ async function runTests() {
} }
const alice = await RiotSession.create("alice", options, riotserver); const alice = await RiotSession.create("alice", options, riotserver);
sessions.push(alice);
consoleLogs = alice.logConsole();
xhrLogs = alice.logXHRRequests();
process.stdout.write(`* signing up as ${alice.username} ... `); process.stdout.write(`* signing up as ${alice.username} ... `);
await signup(alice, alice.username, 'testtest'); await signup(alice, alice.username, 'testtest');
@ -65,19 +61,19 @@ function onSuccess() {
} }
async function onFailure(err) { async function onFailure(err) {
let documentHtml = "no page";
if (globalPage) {
documentHtml = await globalPage.content();
}
console.log('failure: ', err); console.log('failure: ', err);
console.log('console.log output:'); for(var i = 0; i < sessions.length; ++i) {
console.log(consoleLogs.logs()); const session = sessions[i];
console.log('XHR requests:'); documentHtml = await session.page.content();
console.log(xhrLogs.logs()); console.log(`---------------- START OF ${session.username} LOGS ----------------`);
console.log('document html:'); 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(documentHtml);
console.log(`---------------- END OF ${session.username} LOGS ----------------`);
}
process.exit(-1); process.exit(-1);
} }