This repository has been archived by the owner on Dec 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
beforeScreenshot.ts
73 lines (66 loc) · 2.52 KB
/
beforeScreenshot.ts
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
import hideScrollBars from '../clientSideScripts/hideScrollbars';
import setCustomCss from '../clientSideScripts/setCustomCss';
import {CUSTOM_CSS_ID} from './constants';
import {checkIsMobile, getAddressBarShadowPadding, getToolBarShadowPadding} from './utils';
import getEnrichedInstanceData from '../methods/instanceData';
import {BeforeScreenshotOptions, BeforeScreenshotResult} from './beforeScreenshot.interface';
import {Executor} from '../methods/methods.interface';
import hideRemoveElements from '../clientSideScripts/hideRemoveElements';
import {yellow} from "chalk";
/**
* Methods that need to be executed before a screenshot will be taken
*/
export default async function beforeScreenshot(
executor: Executor,
options: BeforeScreenshotOptions,
addShadowPadding: boolean = false
): Promise<BeforeScreenshotResult> {
const {browserName, nativeWebScreenshot, platformName} = options.instanceData;
const {
addressBarShadowPadding,
disableCSSAnimation,
hideElements,
noScrollBars,
removeElements,
toolBarShadowPadding,
} = options;
const addressBarPadding = getAddressBarShadowPadding({
platformName,
browserName,
nativeWebScreenshot,
addressBarShadowPadding,
addShadowPadding,
});
const toolBarPadding = getToolBarShadowPadding({platformName, browserName, toolBarShadowPadding, addShadowPadding});
// Hide the scrollbars
if(noScrollBars) {
await executor(hideScrollBars, noScrollBars);
}
// Hide and or Remove elements
if (hideElements.length > 0 || removeElements.length > 0) {
try {
await executor(hideRemoveElements, {hide: hideElements, remove: removeElements}, true);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}
// Set some custom css
if (disableCSSAnimation || checkIsMobile(platformName)) {
await executor(setCustomCss, {addressBarPadding, disableCSSAnimation, id: CUSTOM_CSS_ID, toolBarPadding});
}
// Get all the needed instance data
const instanceOptions = {
addressBarShadowPadding: options.addressBarShadowPadding,
toolBarShadowPadding: options.toolBarShadowPadding,
...(options.instanceData),
};
return getEnrichedInstanceData(executor, instanceOptions, addShadowPadding);
}