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
/
screenshots.ts
414 lines (366 loc) · 15.5 KB
/
screenshots.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// @ts-ignore
import {yellow} from 'chalk';
import scrollToPosition from '../clientSideScripts/scrollToPosition';
import getDocumentScrollHeight from '../clientSideScripts/getDocumentScrollHeight';
import getAndroidStatusAddressToolBarHeight from '../clientSideScripts/getAndroidStatusAddressToolBarHeight';
import getIosStatusAddressToolBarHeight from '../clientSideScripts/getIosStatusAddressToolBarHeight';
import {OFFSETS} from '../helpers/constants';
import {calculateDprData, getScreenshotSize, waitFor} from '../helpers/utils';
import {Executor, TakeScreenShot} from './methods.interface';
import {
FullPageScreenshotOptions,
FullPageScreenshotNativeMobileOptions,
FullPageScreenshotDataOptions,
FullPageScreenshotsData,
} from './screenshots.interfaces';
import {StatusAddressToolBarHeight} from '../clientSideScripts/statusAddressToolBarHeight.interfaces';
import hideRemoveElements from '../clientSideScripts/hideRemoveElements';
import hideScrollBars from '../clientSideScripts/hideScrollbars';
/**
* Take a full page screenshots for desktop / iOS / Android
*/
export async function getBase64FullPageScreenshotsData(
takeScreenshot: TakeScreenShot,
executor: Executor,
options: FullPageScreenshotDataOptions,
): Promise<FullPageScreenshotsData> {
const {
addressBarShadowPadding,
devicePixelRatio,
fullPageScrollTimeout,
hideAfterFirstScroll,
innerHeight,
isAndroid,
isAndroidNativeWebScreenshot,
isAndroidChromeDriverScreenshot,
isHybridApp,
isIos,
toolBarShadowPadding,
} = options;
const desktopOptions = {
devicePixelRatio,
fullPageScrollTimeout,
hideAfterFirstScroll,
innerHeight,
};
const nativeMobileOptions = {
...desktopOptions,
addressBarShadowPadding,
toolBarShadowPadding,
};
if (isAndroid && isAndroidNativeWebScreenshot) {
// Create a fullpage screenshot for Android when native screenshot (so including status, address and toolbar) is created
const statusAddressBarHeight = (
<StatusAddressToolBarHeight>(await executor(getAndroidStatusAddressToolBarHeight, OFFSETS.ANDROID, isHybridApp))
).statusAddressBar.height;
const androidNativeMobileOptions = {...nativeMobileOptions, statusAddressBarHeight};
return getFullPageScreenshotsDataNativeMobile(takeScreenshot, executor, androidNativeMobileOptions);
} else if (isAndroid && isAndroidChromeDriverScreenshot) {
const chromeDriverOptions = {devicePixelRatio, fullPageScrollTimeout, hideAfterFirstScroll, innerHeight};
// Create a fullpage screenshot for Android when the ChromeDriver provides the screenshots
return getFullPageScreenshotsDataAndroidChromeDriver(takeScreenshot, executor, chromeDriverOptions);
} else if (isIos) {
// Create a fullpage screenshot for iOS. iOS screenshots will hold the status, address and toolbar so they need to be removed
const statusAddressBarHeight = (
<StatusAddressToolBarHeight>(await executor(getIosStatusAddressToolBarHeight, OFFSETS.IOS))
).statusAddressBar.height;
const iosNativeMobileOptions = {...nativeMobileOptions, statusAddressBarHeight};
return getFullPageScreenshotsDataNativeMobile(takeScreenshot, executor, iosNativeMobileOptions);
}
// Create a fullpage screenshot for all desktops
return getFullPageScreenshotsDataDesktop(takeScreenshot, executor, desktopOptions);
}
/**
* Take a full page screenshots for native mobile
*/
export async function getFullPageScreenshotsDataNativeMobile(
takeScreenshot: TakeScreenShot,
executor: Executor,
options: FullPageScreenshotNativeMobileOptions,
): Promise<FullPageScreenshotsData> {
const viewportScreenshots = [];
// The addressBarShadowPadding and toolBarShadowPadding is used because the viewport has a shadow on the address and the tool bar
// so the cutout of the vieport needs to be a little bit smaller
const {
addressBarShadowPadding,
devicePixelRatio,
fullPageScrollTimeout,
hideAfterFirstScroll,
innerHeight,
statusAddressBarHeight,
toolBarShadowPadding,
} = options;
const iosViewportHeight = innerHeight - addressBarShadowPadding - toolBarShadowPadding;
// Start with an empty array, during the scroll it will be filled because a page could also have a lazy loading
const amountOfScrollsArray = [];
let scrollHeight: number;
let screenshotSizeWidth: number;
for (let i = 0; i <= amountOfScrollsArray.length; i++) {
// Determine and start scrolling
const scrollY = iosViewportHeight * i;
await executor(scrollToPosition, scrollY);
// Hide scrollbars before taking a screenshot, we don't want them, on the screenshot
await executor(hideScrollBars, true);
// Simply wait the amount of time specified for lazy-loading
await waitFor(fullPageScrollTimeout);
// Elements that need to be hidden after the first scroll for a fullpage scroll
if (i === 1 && hideAfterFirstScroll.length > 0) {
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, 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.
#####################################################################################
`));
}
}
// Take the screenshot and get the width
const screenshot = await takeBase64Screenshot(takeScreenshot);
screenshotSizeWidth = getScreenshotSize(screenshot, devicePixelRatio).width;
// Determine scroll height and check if we need to scroll again
scrollHeight = await executor(getDocumentScrollHeight);
if (((scrollY + iosViewportHeight) < scrollHeight)) {
amountOfScrollsArray.push(amountOfScrollsArray.length);
}
// There is no else
// The height of the image of the last 1 could be different
const imageHeight = amountOfScrollsArray.length === i ? scrollHeight - scrollY : iosViewportHeight;
// The starting position for cropping could be different for the last image
// The cropping always needs to start at status and address bar height and the address bar shadow padding
const imageYPosition = (amountOfScrollsArray.length === i ? innerHeight - imageHeight : 0) + statusAddressBarHeight + addressBarShadowPadding;
// Store all the screenshot data in the screenshot object
viewportScreenshots.push({
...calculateDprData({
canvasWidth: screenshotSizeWidth,
canvasYPosition: scrollY,
imageHeight: imageHeight,
imageWidth: screenshotSizeWidth,
imageYPosition: imageYPosition,
}, devicePixelRatio),
screenshot,
});
// Show scrollbars again
await executor(hideScrollBars, false);
}
// Put back the hidden elements to visible
if (hideAfterFirstScroll.length > 0) {
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
} 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.
#####################################################################################
`));
}
}
return {
...calculateDprData({
fullPageHeight: scrollHeight - addressBarShadowPadding - toolBarShadowPadding,
fullPageWidth: screenshotSizeWidth,
}, devicePixelRatio),
data: viewportScreenshots,
};
}
/**
* Take a full page screenshot for Android with Chromedriver
*/
export async function getFullPageScreenshotsDataAndroidChromeDriver(
takeScreenshot: TakeScreenShot,
executor: Executor,
options: FullPageScreenshotOptions,
): Promise<FullPageScreenshotsData> {
const viewportScreenshots = [];
const {devicePixelRatio, fullPageScrollTimeout, hideAfterFirstScroll, innerHeight} = options;
// Start with an empty array, during the scroll it will be filled because a page could also have a lazy loading
const amountOfScrollsArray = [];
let scrollHeight: number;
let screenshotSize;
for (let i = 0; i <= amountOfScrollsArray.length; i++) {
// Determine and start scrolling
const scrollY = innerHeight * i;
await executor(scrollToPosition, scrollY);
// Hide scrollbars before taking a screenshot, we don't want them, on the screenshot
await executor(hideScrollBars, true);
// Simply wait the amount of time specified for lazy-loading
await waitFor(fullPageScrollTimeout);
// Elements that need to be hidden after the first scroll for a fullpage scroll
if (i === 1 && hideAfterFirstScroll.length > 0) {
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, 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.
#####################################################################################
`));
}
}
// Take the screenshot
const screenshot = await takeBase64Screenshot(takeScreenshot);
screenshotSize = getScreenshotSize(screenshot, devicePixelRatio);
// Determine scroll height and check if we need to scroll again
scrollHeight = await executor(getDocumentScrollHeight);
if ((scrollY + innerHeight) < scrollHeight) {
amountOfScrollsArray.push(amountOfScrollsArray.length);
}
// There is no else
// The height of the image of the last 1 could be different
const imageHeight: number = amountOfScrollsArray.length === i ? scrollHeight - (innerHeight * viewportScreenshots.length) : innerHeight;
// The starting position for cropping could be different for the last image (0 means no cropping)
const imageYPosition = (amountOfScrollsArray.length === i && amountOfScrollsArray.length !== 0) ? innerHeight - imageHeight : 0;
// Store all the screenshot data in the screenshot object
viewportScreenshots.push({
...calculateDprData({
canvasWidth: screenshotSize.width,
canvasYPosition: scrollY,
imageHeight: imageHeight,
imageWidth: screenshotSize.width,
imageYPosition: imageYPosition,
}, devicePixelRatio),
screenshot,
});
// Show the scrollbars again
await executor(hideScrollBars, false);
}
// Put back the hidden elements to visible
if (hideAfterFirstScroll.length > 0) {
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
} 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.
#####################################################################################
`));
}
}
return {
...calculateDprData({
fullPageHeight: scrollHeight,
fullPageWidth: screenshotSize.width,
}, devicePixelRatio),
data: viewportScreenshots,
};
}
/**
* Take a full page screenshots
*/
export async function getFullPageScreenshotsDataDesktop(
takeScreenshot: TakeScreenShot,
executor: Executor,
options: FullPageScreenshotOptions,
): Promise<FullPageScreenshotsData> {
const viewportScreenshots = [];
const {devicePixelRatio, fullPageScrollTimeout, hideAfterFirstScroll, innerHeight} = options;
let actualInnerHeight = innerHeight;
// Start with an empty array, during the scroll it will be filled because a page could also have a lazy loading
const amountOfScrollsArray = [];
let scrollHeight: number;
let screenshotSize;
for (let i = 0; i <= amountOfScrollsArray.length; i++) {
// Determine and start scrolling
const scrollY = actualInnerHeight * i;
await executor(scrollToPosition, scrollY);
// Simply wait the amount of time specified for lazy-loading
await waitFor(fullPageScrollTimeout);
// Elements that need to be hidden after the first scroll for a fullpage scroll
if (i === 1 && hideAfterFirstScroll.length > 0) {
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, 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.
#####################################################################################
`));
}
}
// Take the screenshot
const screenshot = await takeBase64Screenshot(takeScreenshot);
screenshotSize = getScreenshotSize(screenshot, devicePixelRatio);
// The actual screenshot size might be slightly different than the inner height
// In that case, use the screenshot size instead of the innerHeight
if (i === 0 && screenshotSize.height !== actualInnerHeight) {
if (Math.round(screenshotSize.height) === actualInnerHeight) {
actualInnerHeight = screenshotSize.height;
}
// No else, because some drivers take a full page screenshot, e.g. some versions of FireFox,
// and SafariDriver for Safari 11
}
// Determine scroll height and check if we need to scroll again
scrollHeight = await executor(getDocumentScrollHeight);
if (((scrollY + actualInnerHeight) < scrollHeight && screenshotSize.height === actualInnerHeight)) {
amountOfScrollsArray.push(amountOfScrollsArray.length);
}
// There is no else, Lazy load and large screenshots,
// like with older drivers such as FF <= 47 and IE11, will not work
// The height of the image of the last 1 could be different
const imageHeight: number = amountOfScrollsArray.length === i
? scrollHeight - (actualInnerHeight * viewportScreenshots.length)
: screenshotSize.height;
// The starting position for cropping could be different for the last image (0 means no cropping)
const imageYPosition = (amountOfScrollsArray.length === i && amountOfScrollsArray.length !== 0) ? actualInnerHeight - imageHeight : 0;
// Store all the screenshot data in the screenshot object
viewportScreenshots.push({
...calculateDprData({
canvasWidth: screenshotSize.width,
canvasYPosition: scrollY,
imageHeight: imageHeight,
imageWidth: screenshotSize.width,
imageYPosition: imageYPosition,
}, devicePixelRatio),
screenshot,
});
}
// Put back the hidden elements to visible
if (hideAfterFirstScroll.length > 0) {
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
} 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.
#####################################################################################
`));
}
}
return {
...calculateDprData({
fullPageHeight: scrollHeight,
fullPageWidth: screenshotSize.width,
}, devicePixelRatio),
data: viewportScreenshots,
};
}
/**
* Take a screenshot
*/
export async function takeBase64Screenshot(takeScreenshot: TakeScreenShot): Promise<string> {
return takeScreenshot();
}