bring indentation in line with other front-end projects
This commit is contained in:
parent
2c983f8cee
commit
377a20fffa
22 changed files with 583 additions and 560 deletions
304
src/session.js
304
src/session.js
|
@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
|
|||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
@ -17,180 +17,180 @@ 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class Logger {
|
||||
constructor(username) {
|
||||
this.indent = 0;
|
||||
this.username = username;
|
||||
}
|
||||
constructor(username) {
|
||||
this.indent = 0;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
startGroup(description) {
|
||||
const indent = " ".repeat(this.indent * 2);
|
||||
console.log(`${indent} * ${this.username} ${description}:`);
|
||||
this.indent += 1;
|
||||
}
|
||||
startGroup(description) {
|
||||
const indent = " ".repeat(this.indent * 2);
|
||||
console.log(`${indent} * ${this.username} ${description}:`);
|
||||
this.indent += 1;
|
||||
}
|
||||
|
||||
endGroup() {
|
||||
this.indent -= 1;
|
||||
}
|
||||
endGroup() {
|
||||
this.indent -= 1;
|
||||
}
|
||||
|
||||
step(description) {
|
||||
const indent = " ".repeat(this.indent * 2);
|
||||
process.stdout.write(`${indent} * ${this.username} ${description} ... `);
|
||||
}
|
||||
step(description) {
|
||||
const indent = " ".repeat(this.indent * 2);
|
||||
process.stdout.write(`${indent} * ${this.username} ${description} ... `);
|
||||
}
|
||||
|
||||
done(status = "done") {
|
||||
process.stdout.write(status + "\n");
|
||||
}
|
||||
done(status = "done") {
|
||||
process.stdout.write(status + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
this.log = new Logger(this.username);
|
||||
}
|
||||
|
||||
static async create(username, puppeteerOptions, riotserver) {
|
||||
const browser = await puppeteer.launch(puppeteerOptions);
|
||||
const page = await browser.newPage();
|
||||
await page.setViewport({
|
||||
width: 1280,
|
||||
height: 800
|
||||
});
|
||||
return new RiotSession(browser, page, username, riotserver);
|
||||
}
|
||||
|
||||
async tryGetInnertext(selector) {
|
||||
const field = await this.page.$(selector);
|
||||
if (field != null) {
|
||||
const text_handle = await field.getProperty('innerText');
|
||||
return await text_handle.jsonValue();
|
||||
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);
|
||||
this.log = new Logger(this.username);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async getElementProperty(handle, property) {
|
||||
const propHandle = await handle.getProperty(property);
|
||||
return await propHandle.jsonValue();
|
||||
}
|
||||
|
||||
innerText(field) {
|
||||
return this.getElementProperty(field, 'innerText');
|
||||
}
|
||||
|
||||
getOuterHTML(element_handle) {
|
||||
return this.getElementProperty(field, 'outerHTML');
|
||||
}
|
||||
|
||||
consoleLogs() {
|
||||
return this.consoleLog.buffer;
|
||||
}
|
||||
|
||||
networkLogs() {
|
||||
return this.networkLog.buffer;
|
||||
}
|
||||
|
||||
logXHRRequests() {
|
||||
let buffer = "";
|
||||
this.page.on('requestfinished', async (req) => {
|
||||
const type = req.resourceType();
|
||||
const response = await req.response();
|
||||
//if (type === 'xhr' || type === 'fetch') {
|
||||
buffer += `${type} ${response.status()} ${req.method()} ${req.url()} \n`;
|
||||
// if (req.method() === "POST") {
|
||||
// buffer += " Post data: " + req.postData();
|
||||
// }
|
||||
//}
|
||||
});
|
||||
return {
|
||||
logs() {
|
||||
return buffer;
|
||||
}
|
||||
static async create(username, puppeteerOptions, riotserver) {
|
||||
const browser = await puppeteer.launch(puppeteerOptions);
|
||||
const page = await browser.newPage();
|
||||
await page.setViewport({
|
||||
width: 1280,
|
||||
height: 800
|
||||
});
|
||||
return new RiotSession(browser, page, username, riotserver);
|
||||
}
|
||||
}
|
||||
|
||||
async printElements(label, elements) {
|
||||
console.log(label, await Promise.all(elements.map(getOuterHTML)));
|
||||
}
|
||||
async tryGetInnertext(selector) {
|
||||
const field = await this.page.$(selector);
|
||||
if (field != null) {
|
||||
const text_handle = await field.getProperty('innerText');
|
||||
return await text_handle.jsonValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async replaceInputText(input, text) {
|
||||
// click 3 times to select all text
|
||||
await input.click({clickCount: 3});
|
||||
// then remove it with backspace
|
||||
await input.press('Backspace');
|
||||
// and type the new text
|
||||
await input.type(text);
|
||||
}
|
||||
async getElementProperty(handle, property) {
|
||||
const propHandle = await handle.getProperty(property);
|
||||
return await propHandle.jsonValue();
|
||||
}
|
||||
|
||||
query(selector) {
|
||||
return this.page.$(selector);
|
||||
}
|
||||
|
||||
waitAndQuery(selector, timeout = 500) {
|
||||
return this.page.waitForSelector(selector, {visible: true, timeout});
|
||||
}
|
||||
innerText(field) {
|
||||
return this.getElementProperty(field, 'innerText');
|
||||
}
|
||||
|
||||
queryAll(selector) {
|
||||
return this.page.$$(selector);
|
||||
}
|
||||
getOuterHTML(element_handle) {
|
||||
return this.getElementProperty(field, 'outerHTML');
|
||||
}
|
||||
|
||||
async waitAndQueryAll(selector, timeout = 500) {
|
||||
await this.waitAndQuery(selector, timeout);
|
||||
return await this.queryAll(selector);
|
||||
}
|
||||
consoleLogs() {
|
||||
return this.consoleLog.buffer;
|
||||
}
|
||||
|
||||
waitForNewPage(timeout = 500) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeoutHandle = setTimeout(() => {
|
||||
this.browser.removeEventListener('targetcreated', callback);
|
||||
reject(new Error(`timeout of ${timeout}ms for waitForNewPage elapsed`));
|
||||
}, timeout);
|
||||
networkLogs() {
|
||||
return this.networkLog.buffer;
|
||||
}
|
||||
|
||||
const callback = async (target) => {
|
||||
clearTimeout(timeoutHandle);
|
||||
const page = await target.page();
|
||||
resolve(page);
|
||||
};
|
||||
logXHRRequests() {
|
||||
let buffer = "";
|
||||
this.page.on('requestfinished', async (req) => {
|
||||
const type = req.resourceType();
|
||||
const response = await req.response();
|
||||
//if (type === 'xhr' || type === 'fetch') {
|
||||
buffer += `${type} ${response.status()} ${req.method()} ${req.url()} \n`;
|
||||
// if (req.method() === "POST") {
|
||||
// buffer += " Post data: " + req.postData();
|
||||
// }
|
||||
//}
|
||||
});
|
||||
return {
|
||||
logs() {
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.browser.once('targetcreated', callback);
|
||||
});
|
||||
}
|
||||
async printElements(label, elements) {
|
||||
console.log(label, await Promise.all(elements.map(getOuterHTML)));
|
||||
}
|
||||
|
||||
goto(url) {
|
||||
return this.page.goto(url);
|
||||
}
|
||||
async replaceInputText(input, text) {
|
||||
// click 3 times to select all text
|
||||
await input.click({clickCount: 3});
|
||||
// then remove it with backspace
|
||||
await input.press('Backspace');
|
||||
// and type the new text
|
||||
await input.type(text);
|
||||
}
|
||||
|
||||
url(path) {
|
||||
return this.riotserver + path;
|
||||
}
|
||||
query(selector) {
|
||||
return this.page.$(selector);
|
||||
}
|
||||
|
||||
delay(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
waitAndQuery(selector, timeout = 500) {
|
||||
return this.page.waitForSelector(selector, {visible: true, timeout});
|
||||
}
|
||||
|
||||
close() {
|
||||
return this.browser.close();
|
||||
}
|
||||
queryAll(selector) {
|
||||
return this.page.$$(selector);
|
||||
}
|
||||
|
||||
async waitAndQueryAll(selector, timeout = 500) {
|
||||
await this.waitAndQuery(selector, timeout);
|
||||
return await this.queryAll(selector);
|
||||
}
|
||||
|
||||
waitForNewPage(timeout = 500) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeoutHandle = setTimeout(() => {
|
||||
this.browser.removeEventListener('targetcreated', callback);
|
||||
reject(new Error(`timeout of ${timeout}ms for waitForNewPage elapsed`));
|
||||
}, timeout);
|
||||
|
||||
const callback = async (target) => {
|
||||
clearTimeout(timeoutHandle);
|
||||
const page = await target.page();
|
||||
resolve(page);
|
||||
};
|
||||
|
||||
this.browser.once('targetcreated', callback);
|
||||
});
|
||||
}
|
||||
|
||||
goto(url) {
|
||||
return this.page.goto(url);
|
||||
}
|
||||
|
||||
url(path) {
|
||||
return this.riotserver + path;
|
||||
}
|
||||
|
||||
delay(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
close() {
|
||||
return this.browser.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue