-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
42 lines (36 loc) · 1.37 KB
/
index.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
const utils = require('@percy/sdk-utils');
// Collect client and environment information
const sdkPkg = require('./package.json');
const puppeteerPkg = require('puppeteer/package.json');
const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`;
const ENV_INFO = `${puppeteerPkg.name}/${puppeteerPkg.version}`;
// Take a DOM snapshot and post it to the snapshot endpoint
async function percySnapshot(page, name, options) {
if (!page) throw new Error('A Puppeteer `page` object is required.');
if (!name) throw new Error('The `name` argument is required.');
if (!(await utils.isPercyEnabled())) return;
let log = utils.logger('puppeteer');
try {
// Inject the DOM serialization script
await page.evaluate(await utils.fetchPercyDOM());
// Serialize and capture the DOM
/* istanbul ignore next: no instrumenting injected code */
let domSnapshot = await page.evaluate((options) => {
/* eslint-disable-next-line no-undef */
return PercyDOM.serialize(options);
}, options);
// Post the DOM to the snapshot endpoint with snapshot options and other info
await utils.postSnapshot({
...options,
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
url: page.url(),
domSnapshot,
name
});
} catch (err) {
log.error(`Could not take DOM snapshot "${name}"`);
log.error(err);
}
}
module.exports = percySnapshot;