-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (135 loc) · 4.91 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { AppiumDriver } = require('./percy/driver/driverWrapper');
const { ProviderResolver } = require('./percy/providers/providerResolver');
const { TimeIt } = require('./percy/util/timing');
const postFailedEvents = require('./percy/util/postFailedEvents');
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,
topScrollviewOffset,
bottomScrollviewOffset,
scrollableId,
sync,
testCase,
labels,
androidScrollAreaPercentage,
scrollSpeed
} = 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;
topScrollviewOffset = name.topScrollviewOffset;
bottomScrollviewOffset = name.bottomScrollviewOffset;
scrollableId = name.scrollableId;
sync = name.sync;
testCase = name.testCase;
labels = name.labels;
androidScrollAreaPercentage = name.androidScrollAreaPercentage;
scrollSpeed = name.scrollSpeed;
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;
await postFailedEvents(e);
}
};
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') {
const percyOnAutomateResponse = await percyOnAutomate(driver, name, options);
return percyOnAutomateResponse?.body?.data;
}
const provider = ProviderResolver.resolve(driver);
// Only added for browserstack sdk.
let thTestCaseExecutionId = options.thTestCaseExecutionId;
const response = await provider.screenshot(name, {
fullscreen,
deviceName,
orientation,
statusBarHeight,
navigationBarHeight,
fullPage,
screenLengths,
ignoreRegionXpaths,
ignoreRegionAccessibilityIds,
ignoreRegionAppiumElements,
customIgnoreRegions,
considerRegionXpaths,
considerRegionAccessibilityIds,
considerRegionAppiumElements,
customConsiderRegions,
scrollableXpath,
topScrollviewOffset,
bottomScrollviewOffset,
scrollableId,
sync,
testCase,
labels,
thTestCaseExecutionId,
androidScrollAreaPercentage,
scrollSpeed
});
log.debug(`[${name}] -> end`);
return response?.body?.data;
} catch (e) {
log.error(`[${name}] failed to take screenshot`);
log.debug(`[${name}] ${e}, \n ${e.stack}`);
await postFailedEvents(e);
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;
};