-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
47 lines (41 loc) · 1.62 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
44
45
46
47
// Collect client and environment information
const sdkPkg = require('./package.json');
const protractorPkg = require('protractor/package.json');
const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`;
const ENV_INFO = `${protractorPkg.name}/${protractorPkg.version}`;
// Take a DOM snapshot and post it to the snapshot endpoint
module.exports = function percySnapshot(b, name, options) {
// allow working with or without standalone mode
if (!b || typeof b === 'string') [b, name, options] = [browser, b, name];
if (!b) throw new Error('Protractor\'s `browser` was not found.');
if (!name) throw new Error('The `name` argument is required.');
return b.call(async () => {
let utils = await import('@percy/sdk-utils');
if (!(await utils.isPercyEnabled())) return;
let log = utils.logger('protractor');
try {
// Inject the DOM serialization script
await b.executeScript(await utils.fetchPercyDOM());
// Serialize and capture the DOM
/* istanbul ignore next: no instrumenting injected code */
let { domSnapshot, url } = await b.executeScript(options => ({
/* eslint-disable-next-line no-undef */
domSnapshot: PercyDOM.serialize(options),
url: document.URL
}), options);
// Post the DOM to the snapshot endpoint with snapshot options and other info
await utils.postSnapshot({
...options,
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
domSnapshot,
name,
url
});
} catch (error) {
// Handle errors
log.error(`Could not take DOM snapshot "${name}"`);
log.error(error);
}
});
};