2018-07-09 15:06:17 +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
|
|
|
|
|
|
|
|
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,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// puppeteer helpers
|
|
|
|
|
2018-07-20 16:51:25 +00:00
|
|
|
// TODO: rename to queryAndInnertext?
|
2018-07-09 16:35:47 +00:00
|
|
|
async function tryGetInnertext(page, selector) {
|
2018-07-09 15:06:17 +00:00
|
|
|
const field = await page.$(selector);
|
|
|
|
if (field != null) {
|
|
|
|
const text_handle = await field.getProperty('innerText');
|
|
|
|
return await text_handle.jsonValue();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:51:25 +00:00
|
|
|
async function innerText(page, field) {
|
|
|
|
const text_handle = await field.getProperty('innerText');
|
|
|
|
return await text_handle.jsonValue();
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
async function newPage() {
|
2018-07-09 15:06:17 +00:00
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.setViewport({
|
|
|
|
width: 1280,
|
|
|
|
height: 800
|
|
|
|
});
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
function logConsole(page) {
|
2018-07-09 15:06:17 +00:00
|
|
|
let buffer = "";
|
|
|
|
page.on('console', msg => {
|
|
|
|
buffer += msg.text() + '\n';
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
logs() {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
function logXHRRequests(page) {
|
2018-07-09 15:06:17 +00:00
|
|
|
let buffer = "";
|
2018-07-27 16:58:37 +00:00
|
|
|
page.on('requestfinished', async (req) => {
|
2018-07-09 15:06:17 +00:00
|
|
|
const type = req.resourceType();
|
2018-07-27 16:58:37 +00:00
|
|
|
const response = await req.response();
|
|
|
|
//if (type === 'xhr' || type === 'fetch') {
|
|
|
|
buffer += `${type} ${response.status()} ${req.method()} ${req.url()} \n`;
|
2018-07-27 16:43:11 +00:00
|
|
|
// if (req.method() === "POST") {
|
|
|
|
// buffer += " Post data: " + req.postData();
|
|
|
|
// }
|
2018-07-27 16:58:37 +00:00
|
|
|
//}
|
2018-07-09 15:06:17 +00:00
|
|
|
});
|
|
|
|
return {
|
|
|
|
logs() {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
async function getOuterHTML(element_handle) {
|
2018-07-09 15:06:17 +00:00
|
|
|
const html_handle = await element_handle.getProperty('outerHTML');
|
|
|
|
return await html_handle.jsonValue();
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
async function printElements(label, elements) {
|
|
|
|
console.log(label, await Promise.all(elements.map(getOuterHTML)));
|
2018-07-09 15:06:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
async function replaceInputText(input, text) {
|
2018-07-09 15:06:17 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:51:25 +00:00
|
|
|
// TODO: rename to waitAndQuery(Single)?
|
2018-07-09 16:35:47 +00:00
|
|
|
async function waitAndQuerySelector(page, selector, timeout = 500) {
|
2018-07-09 16:21:05 +00:00
|
|
|
await page.waitForSelector(selector, {visible: true, timeout});
|
|
|
|
return await page.$(selector);
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:51:25 +00:00
|
|
|
async function waitAndQueryAll(page, selector, timeout = 500) {
|
|
|
|
await page.waitForSelector(selector, {visible: true, timeout});
|
|
|
|
return await page.$$(selector);
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:26:47 +00:00
|
|
|
function waitForNewPage(timeout = 500) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const timeoutHandle = setTimeout(() => {
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
browser.once('targetcreated', callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:06:17 +00:00
|
|
|
// other helpers
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
function randomInt(max) {
|
2018-07-09 15:06:17 +00:00
|
|
|
return Math.ceil(Math.random()*max);
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:47 +00:00
|
|
|
function riotUrl(path) {
|
2018-07-09 15:06:17 +00:00
|
|
|
return riotserver + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function delay(ms) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-07-09 16:35:47 +00:00
|
|
|
tryGetInnertext,
|
2018-07-20 16:51:25 +00:00
|
|
|
innerText,
|
2018-07-09 16:35:47 +00:00
|
|
|
newPage,
|
|
|
|
logConsole,
|
|
|
|
logXHRRequests,
|
|
|
|
getOuterHTML,
|
|
|
|
printElements,
|
|
|
|
replaceInputText,
|
|
|
|
waitAndQuerySelector,
|
2018-07-20 16:51:25 +00:00
|
|
|
waitAndQueryAll,
|
2018-07-10 17:26:47 +00:00
|
|
|
waitForNewPage,
|
2018-07-09 16:35:47 +00:00
|
|
|
randomInt,
|
|
|
|
riotUrl,
|
2018-07-09 15:06:17 +00:00
|
|
|
delay,
|
|
|
|
}
|