-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
screenshots.ts
557 lines (496 loc) · 20.2 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import logger from '@wdio/logger'
import scrollToPosition from '../clientSideScripts/scrollToPosition.js'
import getDocumentScrollHeight from '../clientSideScripts/getDocumentScrollHeight.js'
import getAndroidStatusAddressToolBarOffsets from '../clientSideScripts/getAndroidStatusAddressToolBarOffsets.js'
import getIosStatusAddressToolBarOffsets from '../clientSideScripts/getIosStatusAddressToolBarOffsets.js'
import { ANDROID_OFFSETS, IOS_OFFSETS } from '../helpers/constants.js'
import { calculateDprData, getScreenshotSize, waitFor } from '../helpers/utils.js'
import type { Executor, TakeScreenShot } from './methods.interfaces.js'
import type {
FullPageScreenshotOptions,
FullPageScreenshotNativeMobileOptions,
FullPageScreenshotDataOptions,
FullPageScreenshotsData,
TakeWebElementScreenshot,
TakeWebElementScreenshotData,
} from './screenshots.interfaces.js'
import type { StatusAddressToolBarOffsets } from '../clientSideScripts/statusAddressToolBarOffsets.interfaces.js'
import hideRemoveElements from '../clientSideScripts/hideRemoveElements.js'
import hideScrollBars from '../clientSideScripts/hideScrollbars.js'
import type { ElementRectanglesOptions } from './rectangles.interfaces.js'
import { determineElementRectangles } from './rectangles.js'
const log = logger('@wdio/visual-service:webdriver-image-comparison-screenshots')
/**
* 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,
isLandscape,
screenHeight,
screenWidth,
toolBarShadowPadding,
} = options
const desktopOptions = {
devicePixelRatio,
fullPageScrollTimeout,
hideAfterFirstScroll,
innerHeight,
}
const nativeMobileOptions = {
...desktopOptions,
addressBarShadowPadding,
screenHeight,
screenWidth,
toolBarShadowPadding,
}
if (isAndroid && isAndroidNativeWebScreenshot) {
// Create a fullpage screenshot for Android when native screenshot (so including status, address and toolbar) is created
const {
safeArea,
screenHeight,
screenWidth,
sideBar: { width: sideBarWidth },
statusAddressBar: { height: statusAddressBarHeight },
} = <StatusAddressToolBarOffsets>(
await executor(getAndroidStatusAddressToolBarOffsets, ANDROID_OFFSETS, { isHybridApp, isLandscape })
)
const androidNativeMobileOptions = {
...nativeMobileOptions,
isLandscape,
safeArea,
screenHeight,
screenWidth,
sideBarWidth,
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 {
safeArea,
screenHeight,
screenWidth,
sideBar: { width: sideBarWidth },
statusAddressBar: { height: statusAddressBarHeight },
toolBar: { y: iosHomeBarY },
} = <StatusAddressToolBarOffsets> await executor(getIosStatusAddressToolBarOffsets, IOS_OFFSETS, isLandscape)
const iosNativeMobileOptions = {
...nativeMobileOptions,
iosHomeBarY,
isLandscape,
safeArea,
screenHeight,
screenWidth,
sideBarWidth,
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 viewport needs to be a little bit smaller
const {
addressBarShadowPadding,
devicePixelRatio,
fullPageScrollTimeout,
hideAfterFirstScroll,
innerHeight,
iosHomeBarY,
safeArea,
isLandscape,
statusAddressBarHeight,
screenHeight,
sideBarWidth,
toolBarShadowPadding,
} = options
const iosViewportHeight =
innerHeight -
addressBarShadowPadding -
toolBarShadowPadding -
// This is for iOS devices in landscape mode with a notch. They have a home bar at the bottom of the screen
// which is not part of the bottom toolbar. This home bar is not part of the viewport and needs to be subtracted
// 1133 is for iPads with a home bar, see the constants
(iosHomeBarY && ((isLandscape && safeArea) || screenHeight >= 1133) ? screenHeight - iosHomeBarY : 0)
// 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 | undefined
let screenshotSizeHeight: number | undefined
let screenshotSizeWidth: number | undefined
let isRotated = false
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) {
logHiddenRemovedError(e)
}
}
// Take the screenshot and get the width
const screenshot = await takeBase64Screenshot(takeScreenshot)
screenshotSizeHeight = getScreenshotSize(screenshot, devicePixelRatio).height - sideBarWidth
screenshotSizeWidth = getScreenshotSize(screenshot, devicePixelRatio).width - sideBarWidth
isRotated = Boolean(isLandscape && screenshotSizeHeight > screenshotSizeWidth)
// Determine scroll height and check if we need to scroll again
scrollHeight = await executor(getDocumentScrollHeight)
if (scrollHeight && (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
? 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: isRotated ? screenshotSizeHeight : screenshotSizeWidth,
canvasYPosition: scrollY,
imageHeight: imageHeight,
imageWidth: isRotated ? screenshotSizeHeight : screenshotSizeWidth,
imageXPosition: sideBarWidth,
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) {
logHiddenRemovedError(e)
}
}
if (!scrollHeight || !screenshotSizeHeight || !screenshotSizeWidth) {
throw new Error('Couldn\'t determine scroll height or screenshot size')
}
return {
...calculateDprData(
{
fullPageHeight: scrollHeight - addressBarShadowPadding - toolBarShadowPadding,
fullPageWidth: isRotated ? screenshotSizeHeight : 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 | undefined
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) {
logHiddenRemovedError(e)
}
}
// 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 (scrollHeight && (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
? 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,
imageXPosition: 0,
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) {
logHiddenRemovedError(e)
}
}
if (!scrollHeight || !screenshotSize) {
throw new Error('Couldn\'t determine scroll height or screenshot size')
}
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 | undefined
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) {
logHiddenRemovedError(e)
}
}
// 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 (scrollHeight && (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 = scrollHeight && 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,
imageXPosition: 0,
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) {
logHiddenRemovedError(e)
}
}
if (!scrollHeight || !screenshotSize) {
throw new Error('Couldn\'t determine scroll height or screenshot size')
}
return {
...calculateDprData(
{
fullPageHeight: scrollHeight,
fullPageWidth: screenshotSize.width,
},
devicePixelRatio,
),
data: viewportScreenshots,
}
}
/**
* Take a screenshot
*/
export async function takeBase64Screenshot(takeScreenshot: TakeScreenShot): Promise<string> {
return takeScreenshot()
}
/**
* Log an error for not being able to hide remove elements
*
* @TODO: remove the any
*/
function logHiddenRemovedError(error: any) {
log.warn(
'\x1b[33m%s\x1b[0m',
`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${error}
We made sure the test didn't break.
#####################################################################################
`,
)
}
/**
* Take an element screenshot on the web
*/
export async function takeWebElementScreenshot({
devicePixelRatio,
element,
executor,
fallback = false,
innerHeight,
isAndroidNativeWebScreenshot,
isAndroid,
isIOS,
isLandscape,
screenShot,
takeElementScreenshot,
}:TakeWebElementScreenshot): Promise<TakeWebElementScreenshotData>{
if (isIOS || fallback){
const base64Image = await takeBase64Screenshot(screenShot)
const elementRectangleOptions: ElementRectanglesOptions = {
/**
* ToDo: handle NaA case
*/
devicePixelRatio: devicePixelRatio || NaN,
innerHeight: innerHeight || NaN,
isAndroidNativeWebScreenshot,
isAndroid,
isIOS,
isLandscape,
}
const rectangles = await determineElementRectangles({
executor,
base64Image,
options: elementRectangleOptions,
element,
})
return {
base64Image,
isWebDriverElementScreenshot: false,
rectangles,
}
}
try {
const base64Image = await takeElementScreenshot!((await element as WebdriverIO.Element).elementId)
const { height, width } = getScreenshotSize(base64Image)
const rectangles = { x: 0, y: 0, width, height }
if (rectangles.width === 0 || rectangles.height === 0) {
throw new Error('The element has no width or height.')
}
return {
base64Image,
isWebDriverElementScreenshot: true,
rectangles,
}
} catch (_e) {
return takeWebElementScreenshot({
devicePixelRatio,
element,
executor,
fallback: true,
innerHeight,
isAndroidNativeWebScreenshot,
isAndroid,
isIOS,
isLandscape,
screenShot,
takeElementScreenshot,
})
}
}