This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnapshot.js
60 lines (50 loc) · 1.54 KB
/
snapshot.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
const pkg = require("./package.json");
const puppeteerPkg = require("puppeteer/package.json");
const {
agentJsFilename,
isAgentRunning,
postSnapshot
} = require("@percy/agent/dist/utils/sdk-utils");
const CLIENT_INFO = `${pkg.name}/${pkg.version}`;
const ENV_INFO = `${puppeteerPkg.name}/${puppeteerPkg.version}`;
module.exports = async function percySnapshot(page, name, options) {
if (!page) {
throw new Error("Puppeteer 'page' object must be provided.");
}
if (!name) {
throw new Error("'name' must be provided.");
}
try {
await page.addScriptTag({
path: agentJsFilename()
});
} catch (err) {
// Certain CSP settings prevent Puppeteer from injecting scripts. See:
// https://github.com/GoogleChrome/puppeteer/issues/2644
console.log(
`[percy] Could not take snapshot named '${name}', maybe due to stringent CSPs. Try page.setBypassCSP(true).`
);
return;
}
if (!(await isAgentRunning())) {
return;
}
let domSnapshot = await page.evaluate(function(name, options) {
let percyAgentClient = new PercyAgent({ handleAgentCommunication: false });
return percyAgentClient.snapshot(name, options);
});
await postDomSnapshot(name, domSnapshot, page.url(), options);
};
async function postDomSnapshot(name, domSnapshot, url, options) {
let postSuccess = await postSnapshot({
name,
url,
domSnapshot,
clientInfo: CLIENT_INFO,
environmentInfo: ENV_INFO,
...options
});
if (!postSuccess) {
console.log(`[percy] Error posting snapshot to agent.`);
}
}