2018-08-07 14:45:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-08-14 10:53:16 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2018-08-07 14:45:34 +00:00
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const puppeteer = require('puppeteer');
|
2018-09-12 16:36:02 +00:00
|
|
|
const Logger = require('./logger');
|
|
|
|
const LogBuffer = require('./logbuffer');
|
2018-09-12 16:38:42 +00:00
|
|
|
const {delay} = require('./util');
|
2018-08-07 15:16:27 +00:00
|
|
|
|
2018-08-07 14:45:34 +00:00
|
|
|
module.exports = class RiotSession {
|
2018-09-11 16:28:50 +00:00
|
|
|
constructor(browser, page, username, riotserver, hsUrl) {
|
2018-08-14 10:53:16 +00:00
|
|
|
this.browser = browser;
|
|
|
|
this.page = page;
|
2018-09-11 16:28:50 +00:00
|
|
|
this.hsUrl = hsUrl;
|
2018-08-14 10:53:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-09-11 16:28:50 +00:00
|
|
|
static async create(username, puppeteerOptions, riotserver, hsUrl) {
|
2018-08-14 10:53:16 +00:00
|
|
|
const browser = await puppeteer.launch(puppeteerOptions);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.setViewport({
|
|
|
|
width: 1280,
|
|
|
|
height: 800
|
|
|
|
});
|
2018-09-11 16:28:50 +00:00
|
|
|
return new RiotSession(browser, page, username, riotserver, hsUrl);
|
2018-08-14 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async printElements(label, elements) {
|
|
|
|
console.log(label, await Promise.all(elements.map(getOuterHTML)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
query(selector) {
|
|
|
|
return this.page.$(selector);
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:27:30 +00:00
|
|
|
waitAndQuery(selector, timeout = 5000) {
|
2018-08-14 10:53:16 +00:00
|
|
|
return this.page.waitForSelector(selector, {visible: true, timeout});
|
|
|
|
}
|
|
|
|
|
|
|
|
queryAll(selector) {
|
|
|
|
return this.page.$$(selector);
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:27:30 +00:00
|
|
|
async waitAndQueryAll(selector, timeout = 5000) {
|
2018-08-14 10:53:16 +00:00
|
|
|
await this.waitAndQuery(selector, timeout);
|
|
|
|
return await this.queryAll(selector);
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:31:15 +00:00
|
|
|
waitForReload(timeout = 10000) {
|
2018-09-11 16:28:50 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const timeoutHandle = setTimeout(() => {
|
|
|
|
this.browser.removeEventListener('domcontentloaded', callback);
|
|
|
|
reject(new Error(`timeout of ${timeout}ms for waitForReload elapsed`));
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
const callback = async () => {
|
|
|
|
clearTimeout(timeoutHandle);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.page.once('domcontentloaded', callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:27:30 +00:00
|
|
|
waitForNewPage(timeout = 5000) {
|
2018-08-14 10:53:16 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const timeoutHandle = setTimeout(() => {
|
2018-08-23 08:03:37 +00:00
|
|
|
this.browser.removeListener('targetcreated', callback);
|
2018-08-14 10:53:16 +00:00
|
|
|
reject(new Error(`timeout of ${timeout}ms for waitForNewPage elapsed`));
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
const callback = async (target) => {
|
2018-08-23 08:03:37 +00:00
|
|
|
if (target.type() !== 'page') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.browser.removeListener('targetcreated', callback);
|
2018-08-14 10:53:16 +00:00
|
|
|
clearTimeout(timeoutHandle);
|
|
|
|
const page = await target.page();
|
|
|
|
resolve(page);
|
|
|
|
};
|
|
|
|
|
2018-08-23 08:03:37 +00:00
|
|
|
this.browser.on('targetcreated', callback);
|
2018-08-14 10:53:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-14 12:45:40 +00:00
|
|
|
/** wait for a /sync request started after this call that gets a 200 response */
|
|
|
|
async waitForNextSuccessfulSync() {
|
|
|
|
const syncUrls = [];
|
|
|
|
function onRequest(request) {
|
|
|
|
if (request.url().indexOf("/sync") !== -1) {
|
|
|
|
syncUrls.push(request.url());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.page.on('request', onRequest);
|
|
|
|
|
|
|
|
await this.page.waitForResponse((response) => {
|
|
|
|
return syncUrls.includes(response.request().url()) && response.status() === 200;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.page.removeListener('request', onRequest);
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:53:16 +00:00
|
|
|
goto(url) {
|
|
|
|
return this.page.goto(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
url(path) {
|
|
|
|
return this.riotserver + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
delay(ms) {
|
2018-09-12 16:38:42 +00:00
|
|
|
return delay(ms);
|
2018-08-14 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 10:44:01 +00:00
|
|
|
async setOffline(enabled) {
|
|
|
|
const description = enabled ? "offline" : "back online";
|
|
|
|
this.log.step(`goes ${description}`);
|
|
|
|
await this.page.setOfflineMode(enabled);
|
|
|
|
this.log.done();
|
|
|
|
}
|
|
|
|
|
2018-08-14 10:53:16 +00:00
|
|
|
close() {
|
|
|
|
return this.browser.close();
|
|
|
|
}
|
2019-04-16 08:30:14 +00:00
|
|
|
|
|
|
|
async poll(callback, timeout) {
|
|
|
|
const INTERVAL = 100;
|
|
|
|
let waited = 0;
|
|
|
|
while(waited < timeout) {
|
|
|
|
await this.delay(INTERVAL);
|
|
|
|
waited += INTERVAL;
|
|
|
|
if (callback()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-07 14:45:34 +00:00
|
|
|
}
|