-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (107 loc) · 3.96 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const { AppiumDriver } = require('./percy/driver/driverWrapper');
const { ProviderResolver } = require('./percy/providers/providerResolver');
const { TimeIt } = require('./percy/util/timing');
const percyOnAutomate = require('./percy/percyOnAutomate');
const log = require('./percy/util/log');
const utils = require('@percy/sdk-utils');
module.exports = async function percyScreenshot(driver, name, options = {}) {
let {
fullscreen,
deviceName,
orientation,
statusBarHeight,
navigationBarHeight,
fullPage,
screenLengths,
ignoreRegionXpaths,
ignoreRegionAccessibilityIds,
ignoreRegionAppiumElements,
customIgnoreRegions,
considerRegionXpaths,
considerRegionAccessibilityIds,
considerRegionAppiumElements,
customConsiderRegions,
scrollableXpath,
scrollableId
} = options;
// allow working with or without standalone mode for wdio
if (!driver || typeof driver === 'string') {
// Unable to test this as couldnt define `browser` from test mjs file that would be
// accessible here
/* istanbul ignore if */
if (name) {
fullscreen = name.fullscreen;
deviceName = name.deviceName;
orientation = name.orientation;
statusBarHeight = name.statusBarHeight;
navigationBarHeight = name.navigationBarHeight;
fullPage = name.fullPage;
screenLengths = name.screenLengths;
ignoreRegionXpaths = name.ignoreRegionXpaths;
ignoreRegionAccessibilityIds = name.ignoreRegionAccessibilityIds;
ignoreRegionAppiumElements = name.ignoreRegionAppiumElements;
customIgnoreRegions = name.customIgnoreRegions;
considerRegionXpaths = name.considerRegionXpaths;
considerRegionAccessibilityIds = name.considerRegionAccessibilityIds;
considerRegionAppiumElements = name.considerRegionAppiumElements;
customConsiderRegions = name.customConsiderRegions;
scrollableXpath = name.scrollableXpath;
scrollableId = name.scrollableId;
options = name;
}
try {
// browser is defined in wdio context
// eslint-disable-next-line no-undef
[driver, name] = [browser, driver];
} catch (e) { // ReferenceError: browser is not defined.
driver = undefined;
}
};
if (!driver) throw new Error('The WebdriverIO `browser` object or wd `driver` object is required.');
if (!name) throw new Error('The `name` argument is required.');
log.debug(`[${name}] -> begin`);
driver = new AppiumDriver(driver);
if (!await module.exports.isPercyEnabled(driver)) {
log.info(`[${name}] percy is disabled for session ${driver.sessionId} -> end`);
return;
};
return TimeIt.run('percyScreenshot', async () => {
try {
if (utils.percy?.type === 'automate') {
return await percyOnAutomate(driver, name, options);
}
const provider = ProviderResolver.resolve(driver);
const response = await provider.screenshot(name, {
fullscreen,
deviceName,
orientation,
statusBarHeight,
navigationBarHeight,
fullPage,
screenLengths,
ignoreRegionXpaths,
ignoreRegionAccessibilityIds,
ignoreRegionAppiumElements,
customIgnoreRegions,
considerRegionXpaths,
considerRegionAccessibilityIds,
considerRegionAppiumElements,
customConsiderRegions,
scrollableXpath,
scrollableId
});
log.debug(`[${name}] -> end`);
return response;
} catch (e) {
log.error(`[${name}] failed to take screenshot`);
log.debug(`[${name}] ${e}, \n ${e.stack}`);
if (!(await driver.getPercyOptions()).ignoreErrors) throw e;
}
});
};
// 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) {
if (!(await utils.isPercyEnabled())) return false;
return (await driver.getPercyOptions()).enabled;
};