-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
80 lines (72 loc) · 2.93 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const utils = require('@percy/sdk-utils');
const { Utils } = require('./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}`;
const log = utils.logger('playwright');
// Take a DOM snapshot and post it to the snapshot endpoint
const percySnapshot = async function(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;
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);
}
};
// Takes Playwright screenshot with Automate
const percyScreenshot = async function(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;
if (Utils.projectType() !== 'automate') {
throw new Error('Invalid function call - percyScreenshot(). Please use percySnapshot() function for taking screenshot. percyScreenshot() should be used only while using Percy with Automate. For more information on usage of PercySnapshot(), refer doc for your language https://www.browserstack.com/docs/percy/integrate/overview');
}
try {
const sessionDetails = await Utils.sessionDetails(page);
const sessionId = sessionDetails.hashed_id;
const pageGuid = page._guid;
const frameGuid = page._mainFrame._guid;
const data = {
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
sessionId: sessionId,
pageGuid: pageGuid,
frameGuid: frameGuid,
framework: 'playwright',
snapshotName: name,
options
};
const response = await Utils.captureAutomateScreenshot(data);
return response?.body?.data;
} catch (err) {
log.error(`Could not take percy screenshot "${name}"`);
log.error(err);
}
};
module.exports = percySnapshot;
module.exports.percySnapshot = percySnapshot;
module.exports.percyScreenshot = percyScreenshot;
module.exports.CLIENT_INFO = CLIENT_INFO;
module.exports.ENV_INFO = ENV_INFO;