-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
61 lines (46 loc) · 1.62 KB
/
lib.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
const path = require('path');
const puppeteer = require('puppeteer');
exports.capture = async function(destination, host, pages) {
const sessionToken = (process.argv[2]||'').trim();
if (!sessionToken) {
console.log('Warning: no session token provided');
}
console.log('Capturing %s to %s', host, destination);
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.on('pageerror', function(err) {
console.log('Page error: %s', err.toString());
});
page.on('error', function(err) {
console.log('Error: %s', err.toString());
});
await page.setViewport({
width: 1366,
height: 768
});
await page.setExtraHTTPHeaders({
Authorization: 'Token '+sessionToken
});
for (const pageKey of Object.keys(pages)) {
const pageSpec = typeof pages[pageKey] == 'string' ? { path: pages[pageKey] } : pages[pageKey];
const pagePath = pageSpec.path;
const pageFilename = `${pageKey}.png`;
console.log('\tRendering %s to %s', pagePath, pageFilename);
try {
await page.goto(path.join(host, pagePath), {
waitUntil: 'networkidle'
});
if (pageSpec.waitFor) {
await page.waitFor(pageSpec.waitFor);
}
await page.screenshot({
path: path.join(destination, pageFilename),
fullPage: true
});
} catch (err) {
console.log('\tFailed to capture page: %s', err.toString());
}
}
await browser.close();
console.log('done');7
};