-
Notifications
You must be signed in to change notification settings - Fork 0
/
takeScreenshots.js
68 lines (57 loc) · 1.53 KB
/
takeScreenshots.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
const browser = require('./puppeteer/browser.js');
const takeScreenshots = async (url, retries) => {
try {
const page = await browser.getPage();
console.log(`Try number ${retries}`);
console.log('Going to url');
await page.goto(url, {
timeout: retries > 3 ? 45000 : 30000,
waitUntill: retries > 1 ? 'domcontentloaded' : 'load'
});
console.log('Page loaded, waiting 5 seconds for other things to load');
await page.waitFor(5000);
console.log('Taking screenshots...');
const screenshotsPromises = Promise.all([
page.screenshot({
path: __dirname + '/.data/screenshot1.png',
clip: {
x: 0,
y: 0,
width: 700,
height: 2000
}
}),
page.screenshot({
path: __dirname + '/.data/screenshot2.png',
clip: {
x: 0,
y: 2000,
width: 700,
height: 2000
}
}),
page.screenshot({
path: __dirname + '/.data/screenshot3.png',
clip: {
x: 0,
y: 4000,
width: 700,
height: 2000
}
})
]);
const timeout = new Promise((resolve, reject) =>
setTimeout(_ => {
reject(new Error('Too long taking screenshots, aborting..'));
}, 40000)
);
await Promise.race([screenshotsPromises, timeout]);
console.log('Screenshots taken');
await browser.reset();
}
catch(error) {
await browser.reset();
throw error;
}
}
module.exports = takeScreenshots;