element-web/helpers.js

148 lines
3.5 KiB
JavaScript
Raw Normal View History

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
// 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;
}
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 = "";
page.on('request', req => {
const type = req.resourceType();
if (type === 'xhr' || type === 'fetch') {
buffer += `${req.method()} ${req.url()} \n`;
if (req.method() === "POST") {
buffer += " Post data: " + req.postData();
}
}
});
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);
}
// 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);
}
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,
innerText,
2018-07-09 16:35:47 +00:00
newPage,
logConsole,
logXHRRequests,
getOuterHTML,
printElements,
replaceInputText,
waitAndQuerySelector,
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,
}