forked from russss/barclayscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
96 lines (79 loc) · 2.72 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fs = require('fs');
// Look for a warning on the page and raise it as an error.
async function check_for_webpage_warning(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.dump_html_to = async(page, filename) => {
const html = await page.content();
fs.writeFileSync(filename, html);
}
exports.dump_screenshot_to = async(page, filename) => {
await page.screenshot({path: filename, fullPage: true});
}
exports.dump_to = async(page, prefix) => {
await exports.dump_screenshot_to(page, prefix+'.png')
await exports.dump_html_to(page, prefix+'.html')
}
exports.dump_screenshot = async(page) => {
await exports.dump_screenshot_to(page, "./error.png");
}
exports.dump_html = async(page) => {
await exports.dump_html_to(page, "./dump.html");
}
// Click element then wait for any subsequent navigation to complete
exports.click = async (page, selector) => {
await click_withnav(page, selector, true)
};
// Click element without waiting for subsequent navigation (ie, clientside UI javascript)
exports.click_nonav = async (page, selector) => {
await click_withnav(page, selector, false)
}
click_withnav = async (page, selector, wait_for_nav) => {
try {
steps = []
if (wait_for_nav) {
await Promise.all([
page.waitForNavigation({timeout: 30000}),
page.$eval(selector, el => { el.click() }),
]);
} else {
await Promise.all([
page.$eval(selector, el => { el.click() }),
]);
}
} catch (err) {
check_for_webpage_warning(page, 'clicking', selector);
await exports.dump_screenshot(page);
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) => {
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);
};
// waits for a selector to become available
exports.wait = async (page, selector) => {
try {
await page.waitForSelector(selector, {timeout: 30000});
} catch (err) {
check_for_webpage_warning(page, 'fetching', selector);
await exports.dump_screenshot(page);
throw new Error(`Couldn't find selector ${selector} on page ${page.url()}. Screenshot saved to error.png`);
}
};
exports.cssEsc = (string) => {
return string.replace(/([\\'"])/g, '\\$1');
};