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 35
/
Copy pathutils.ts
160 lines (138 loc) · 5.15 KB
/
utils.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import {join} from 'path';
import {DESKTOP, PLATFORMS} from './constants';
import {ensureDirSync} from 'fs-extra';
import {
FormatFileDefaults,
FormatFileNameOptions,
GetAddressBarShadowPaddingOptions,
GetAndCreatePathOptions,
GetToolBarShadowPaddingOptions, ScreenshotSize
} from './utils.interfaces';
/**
* Get and create a folder
*/
export function getAndCreatePath(folder: string, options: GetAndCreatePathOptions): string {
const {browserName, deviceName, isMobile, savePerInstance} = options;
const instanceName = (isMobile ? deviceName : `${DESKTOP}_${browserName}`).replace(/ /g, '_');
const subFolder = savePerInstance ? instanceName : '';
const folderName = join(folder, subFolder);
ensureDirSync(folderName);
return folderName;
}
/**
* Format the filename
*/
export function formatFileName(options: FormatFileNameOptions): string {
let defaults: FormatFileDefaults = {
browserName: options.browserName,
browserVersion: options.browserVersion,
deviceName: options.deviceName,
dpr: options.devicePixelRatio,
height: options.isMobile ? options.screenHeight : options.outerHeight,
logName: options.logName,
mobile: (options.isMobile && options.isTestInBrowser) ? options.browserName : options.isMobile ? 'app' : '',
name: options.name,
platformName: options.platformName,
platformVersion: options.platformVersion,
tag: options.tag,
width: options.isMobile ? options.screenWidth : options.outerWidth
};
let fileName = options.formatImageName;
Object.keys(defaults).forEach((value: string) => {
// @ts-ignore
// @TODO: Fix this in a proper way
fileName = fileName.replace(`{${value}}`, defaults[value]);
});
return `${fileName.replace(/ /g, '_')}.png`;
}
/**
* Checks if it is mobile
*/
export function checkIsMobile(platformName: string): boolean {
return checkIsAndroid(platformName) || checkIsIos(platformName);
}
/**
* Checks if the os is Android
*/
export function checkIsAndroid(platformName: string): boolean {
return platformName.toLowerCase() === PLATFORMS.ANDROID;
}
/**
* Checks if the os is IOS
*/
export function checkIsIos(platformName: string): boolean {
return platformName.toLowerCase() === PLATFORMS.IOS;
}
/**
* Checks if the test is executed in a browser
*/
export function checkTestInBrowser(browserName: string): boolean {
return browserName !== '';
}
/**
* Checks if the test is executed in a browser on a mobile phone
*/
export function checkTestInMobileBrowser(platformName: string, browserName: string): boolean {
return checkIsMobile(platformName) && checkTestInBrowser(browserName);
}
/**
* Checks if this is a native webscreenshot on android
*/
export function checkAndroidNativeWebScreenshot(platformName: string, nativeWebscreenshot: boolean): boolean {
return (checkIsAndroid(platformName) && nativeWebscreenshot) || false;
}
/**
* Checks if this is an Android chromedriver screenshot
*/
export function checkAndroidChromeDriverScreenshot(platformName: string, nativeWebScreenshot: boolean): boolean {
return checkIsAndroid(platformName) && !checkAndroidNativeWebScreenshot(platformName, nativeWebScreenshot);
}
/**
* Get the address bar shadow padding. This is only needed for Android native webscreenshot and iOS
*/
export function getAddressBarShadowPadding(options: GetAddressBarShadowPaddingOptions): number {
const {
platformName,
browserName,
nativeWebScreenshot,
addressBarShadowPadding,
addShadowPadding,
} = options;
const isTestInMobileBrowser = checkTestInMobileBrowser(platformName, browserName);
const isAndroidNativeWebScreenshot = checkAndroidNativeWebScreenshot(platformName, nativeWebScreenshot);
const isAndroid = checkIsAndroid(platformName);
const isIos = checkIsIos(platformName);
return isTestInMobileBrowser && ((isAndroidNativeWebScreenshot && isAndroid) || isIos) && addShadowPadding ? addressBarShadowPadding : 0;
}
/**
* Get the tool bar shadow padding. This is only needed for iOS
*/
export function getToolBarShadowPadding(options: GetToolBarShadowPaddingOptions): number {
const {platformName, browserName, toolBarShadowPadding, addShadowPadding} = options;
return checkTestInMobileBrowser(platformName, browserName) && checkIsIos(platformName) && addShadowPadding ? toolBarShadowPadding : 0;
}
/**
* Calculate the data based on the device pixel ratio
*/
export function calculateDprData<T>(data: T, devicePixelRatio: number): T {
// @ts-ignore
// @TODO: need to figure this one out
Object.keys(data).map((key) => data[key] = typeof data[key] === 'number' ? data[key] * devicePixelRatio : data[key]);
return data;
}
/**
* Wait for an amount of milliseconds
*/
export async function waitFor(milliseconds: number): Promise<void> {
/* istanbul ignore next */
return new Promise(resolve => setTimeout(() => resolve(), milliseconds));
}
/**
* Get the size of a screenshot in pixels without the device pixel ratio
*/
export function getScreenshotSize(screenshot: string, devicePixelRation: number = 1): ScreenshotSize {
return {
height: Buffer.from(screenshot, 'base64').readUInt32BE(20) / devicePixelRation,
width: Buffer.from(screenshot, 'base64').readUInt32BE(16) / devicePixelRation,
};
}