-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
82 lines (71 loc) · 2.82 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
81
82
const utils = require('@percy/sdk-utils');
// Collect client and environment information
const sdkPkg = require('./package.json');
let webdriverioPkg = null;
try {
webdriverioPkg = require('webdriverio/package.json');
} catch {
try {
// this handles webdriverio 9
const path = require('path');
const webdriverioDir = path.dirname(require.resolve('webdriverio'));
webdriverioPkg = require(`${webdriverioDir}/../package.json`);
} catch {
// next is only fallback if everything else fails just to make sure that version info
// is not the reason why we break
/* istanbul ignore next */
webdriverioPkg = {
name: 'unknown-webdriverio',
version: 'unknown'
};
}
}
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 => ({
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() {
return await utils.isPercyEnabled();
};