-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
43 lines (37 loc) · 1.43 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
43
const utils = require('@percy/sdk-utils');
// Collect client and environment information
const sdkPkg = require('./package.json');
const playwrightPkg = require('playwright/package.json');
const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`;
const ENV_INFO = `${playwrightPkg.name}/${playwrightPkg.version}`;
// Take a DOM snapshot and post it to the snapshot endpoint
async function percySnapshot(page, name, options) {
if (!page) throw new Error('A Playwright `page` object is required.');
if (!name) throw new Error('The `name` argument is required.');
if (!(await utils.isPercyEnabled())) return;
let log = utils.logger('playwright');
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
const response = await utils.postSnapshot({
...options,
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
url: page.url(),
domSnapshot,
name
});
return response?.body?.data;
} catch (err) {
log.error(`Could not take DOM snapshot "${name}"`);
log.error(err);
}
}
module.exports = percySnapshot;