This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.js
64 lines (54 loc) · 2.04 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Look for a warning on the page and raise it as an error.
async function raiseWarning(page, action, selector) {
const warning = await page.$('.notification--warning');
if (!warning) {
return
}
const warningText = await page.evaluate((el) => { return el.textContent }, warning);
throw `Barclays Error: "${warningText.trim()}" (while ${action} ${selector})`;
}
exports.screenshot = async(page, filename) => {
await page.screenshot({path: filename, fullPage: true});
}
// Click a link and wait for the navigation state to go to idle.
exports.click = async (page, selector) => {
try {
await Promise.all([
page.waitForNavigation({timeout: 30000}),
// Executing el.click() within the page context with $eval means we can
// click invisible links, which simplifies things.
page.$eval(selector, el => { el.click() }),
]);
} catch (err) {
raiseWarning(page, 'clicking', selector);
await exports.screenshot(page, 'error.png');
throw `Error when clicking ${selector} on URL ${page.url()}: ${err}`;
}
};
exports.fillField = async (page, key, value) => {
await page.click(key);
await page.type(key, value);
}
exports.fillFields = async (page, form) => {
// Disappointingly, you can't type into multiple fields simultaneously.
for (let key of Object.keys(form)) {
await exports.fillField(page, key, form[key]);
}
};
exports.getAttribute = (page, element, attribute) => {
return page.evaluate((el, attr) => { return el.getAttribute(attr) }, element, attribute);
};
// Wait for a selector to become visible, and issue a nice error if it doesn't.
exports.wait = async (page, selector) => {
try {
await page.waitForSelector(selector, {timeout: 30000});
} catch (err) {
raiseWarning(page, 'fetching', selector);
let screenshotFile = './error.png';
await exports.screenshot(page, screenshotFile);
throw `Couldn't find selector "${selector}" on page ${page.url()}. Screenshot saved to ${screenshotFile}.`;
}
};
exports.cssEsc = (string) => {
return string.replace(/([\\'"])/g, '\\$1');
};