-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
62 lines (54 loc) · 2.36 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
const utils = require('@percy/sdk-utils');
// Collect client and environment information
const sdkPkg = require('./package.json');
const webdriverioPkg = require('webdriverio/package.json');
const CLIENT_INFO = `${sdkPkg.name}/${sdkPkg.version}`;
const ENV_INFO = `${webdriverioPkg.name}/${webdriverioPkg.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('The WebdriverIO `browser` object is required.');
if (!name) throw new Error('The `name` argument is required.');
return b.call(async () => {
if (!(await module.exports.isPercyEnabled())) return;
let log = utils.logger('webdriverio');
if (utils.percy?.type === 'automate') {
throw new Error('You are using Percy on Automate session with WebdriverIO. For using WebdriverIO correctly, please use https://github.com/percy/percy-selenium-js/ or https://github.com/percy/percy-appium-js/');
}
try {
// Inject the DOM serialization script
await b.execute(await utils.fetchPercyDOM());
// Serialize and capture the DOM
/* istanbul ignore next: no instrumenting injected code */
let { domSnapshot, url } = await b.execute(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
const response = await module.exports.request({
...options,
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
domSnapshot,
name,
url
});
return response?.body?.data;
} catch (error) {
// Handle errors
log.error(`Could not take DOM snapshot "${name}"`);
log.error(error);
}
});
};
// To mock the test case
module.exports.request = async function request(data) {
return await utils.postSnapshot(data);
};
// jasmine cannot mock individual functions, hence adding isPercyEnabled to the exports object
// also need to define this at the end of the file or else default exports will over-ride this
module.exports.isPercyEnabled = async function isPercyEnabled(driver) {
return await utils.isPercyEnabled();
};