@@ -9,25 +9,50 @@ import $dom from '../../dom'
9
9
import $errUtils from '../../cypress/error_utils'
10
10
import $utils from '../../cypress/utils'
11
11
import type { Log } from '../../cypress/log'
12
+ import type { StateFunc } from '../../cypress/state'
12
13
13
- const getViewportHeight = ( state ) => {
14
+ interface InternalScreenshotOptions extends Partial < Cypress . Loggable & Cypress . Timeoutable & Cypress . ScreenshotOptions > {
15
+ _log ?: Log
16
+ }
17
+
18
+ type Scroll = {
19
+ y : number
20
+ clip ?: Cypress . ScreenshotOptions [ 'clip' ]
21
+ afterScroll ?: ( ) => Cypress . Dimensions
22
+ }
23
+
24
+ type TakeScreenshotOptions = {
25
+ name ?: string
26
+ subject ?: JQuery < HTMLElement >
27
+ simple ?: boolean
28
+ testFailure ?: boolean
29
+ runnable : ( Mocha . Test | Mocha . Hook ) & {
30
+ id : string
31
+ }
32
+ log ?: Log
33
+ timeout ?: number
34
+ }
35
+
36
+ type AutomationOptions = TakeScreenshotOptions & Omit < Cypress . ScreenshotOptions , 'onBeforeScreenshot' | 'onAfterScreenshot' | 'disableTimersAndAnimations' | 'scale' | 'padding' > & Partial < Cypress . ScreenshotOptions >
37
+
38
+ const getViewportHeight = ( state : StateFunc ) => {
14
39
// TODO this doesn't seem correct
15
40
return Math . min ( state ( 'viewportHeight' ) , window . innerHeight )
16
41
}
17
42
18
- const getViewportWidth = ( state ) => {
43
+ const getViewportWidth = ( state : StateFunc ) => {
19
44
return Math . min ( state ( 'viewportWidth' ) , window . innerWidth )
20
45
}
21
46
22
- const automateScreenshot = ( state , options : TakeScreenshotOptions = { } ) => {
47
+ const automateScreenshot = ( state : StateFunc , options : TakeScreenshotOptions ) => {
23
48
const { runnable, timeout } = options
24
49
25
50
const titles : string [ ] = [ ]
26
51
27
52
// if this a hook then push both the current test title
28
53
// and our own hook title
29
54
if ( runnable . type === 'hook' ) {
30
- let ct = runnable . ctx . currentTest
55
+ let ct = runnable . ctx ? .currentTest
31
56
32
57
if ( runnable . ctx && ct ) {
33
58
titles . push ( ct . title , runnable . title )
@@ -56,6 +81,7 @@ const automateScreenshot = (state, options: TakeScreenshotOptions = {}) => {
56
81
titles,
57
82
testId : runnable . id ,
58
83
takenPaths : state ( 'screenshotPaths' ) ,
84
+ // @ts -ignore
59
85
testAttemptIndex : $utils . getTestFromRunnable ( runnable ) . _currentRetry ,
60
86
} , _ . omit ( options , 'runnable' , 'timeout' , 'log' , 'subject' ) )
61
87
@@ -84,7 +110,7 @@ const automateScreenshot = (state, options: TakeScreenshotOptions = {}) => {
84
110
} )
85
111
}
86
112
87
- const scrollOverrides = ( win , doc ) => {
113
+ const scrollOverrides = ( win : Window , doc : Document ) => {
88
114
const originalOverflow = doc . documentElement . style . overflow
89
115
const originalBodyOverflowY = doc . body . style . overflowY
90
116
const originalX = win . scrollX
@@ -108,7 +134,8 @@ const scrollOverrides = (win, doc) => {
108
134
// since we scroll down the page in takeScrollingScreenshots
109
135
// and don't want the page size to change once we start
110
136
// https://github.com/cypress-io/cypress/issues/6099
111
- win . dispatchEvent ( new win . Event ( 'scroll' ) )
137
+
138
+ win . dispatchEvent ( new Event ( 'scroll' ) )
112
139
113
140
return ( ) => {
114
141
doc . documentElement . style . overflow = originalOverflow
@@ -122,16 +149,16 @@ const scrollOverrides = (win, doc) => {
122
149
}
123
150
}
124
151
125
- const validateNumScreenshots = ( numScreenshots , automationOptions ) => {
152
+ const validateNumScreenshots = ( numScreenshots : number , automationOptions : AutomationOptions ) => {
126
153
if ( numScreenshots < 1 ) {
127
154
$errUtils . throwErrByPath ( 'screenshot.invalid_height' , {
128
155
log : automationOptions . log ,
129
156
} )
130
157
}
131
158
}
132
159
133
- const takeScrollingScreenshots = ( scrolls , win , state , automationOptions ) => {
134
- const scrollAndTake = ( { y, clip, afterScroll } , index ) => {
160
+ const takeScrollingScreenshots = ( scrolls : Scroll [ ] , win : Window , state : StateFunc , automationOptions : AutomationOptions ) => {
161
+ const scrollAndTake = ( { y, clip, afterScroll } : Scroll , index ) => {
135
162
win . scrollTo ( 0 , y )
136
163
if ( afterScroll ) {
137
164
clip = afterScroll ( )
@@ -151,7 +178,7 @@ const takeScrollingScreenshots = (scrolls, win, state, automationOptions) => {
151
178
. then ( _ . last )
152
179
}
153
180
154
- const takeFullPageScreenshot = ( state , automationOptions ) => {
181
+ const takeFullPageScreenshot = ( state : StateFunc , automationOptions : AutomationOptions ) => {
155
182
const win = state ( 'window' )
156
183
const doc = state ( 'document' )
157
184
@@ -187,11 +214,12 @@ const takeFullPageScreenshot = (state, automationOptions) => {
187
214
. finally ( resetScrollOverrides )
188
215
}
189
216
190
- const applyPaddingToElementPositioning = ( elPosition , automationOptions ) => {
217
+ const applyPaddingToElementPositioning = ( elPosition : Cypress . ElementPositioning , automationOptions : AutomationOptions ) => {
191
218
if ( ! automationOptions . padding ) {
192
219
return elPosition
193
220
}
194
221
222
+ // @ts -ignore
195
223
const [ paddingTop , paddingRight , paddingBottom , paddingLeft ] = automationOptions . padding
196
224
197
225
return {
@@ -208,7 +236,7 @@ const applyPaddingToElementPositioning = (elPosition, automationOptions) => {
208
236
}
209
237
}
210
238
211
- const takeElementScreenshot = ( $el , state , automationOptions ) => {
239
+ const takeElementScreenshot = ( $el : JQuery < HTMLElement > , state : StateFunc , automationOptions : AutomationOptions ) => {
212
240
const win = state ( 'window' )
213
241
const doc = state ( 'document' )
214
242
@@ -224,7 +252,7 @@ const takeElementScreenshot = ($el, state, automationOptions) => {
224
252
225
253
validateNumScreenshots ( numScreenshots , automationOptions )
226
254
227
- const scrolls = _ . map ( _ . times ( numScreenshots ) , ( index ) => {
255
+ const scrolls : Scroll [ ] = _ . map ( _ . times ( numScreenshots ) , ( index ) => {
228
256
const y = elPosition . fromElWindow . top + ( viewportHeight * index )
229
257
230
258
const afterScroll = ( ) => {
@@ -274,30 +302,30 @@ const takeElementScreenshot = ($el, state, automationOptions) => {
274
302
}
275
303
276
304
// "app only" means we're hiding the runner UI
277
- const isAppOnly = ( { capture } ) => {
305
+ const isAppOnly = ( { capture } : { capture : Cypress . ScreenshotOptions [ 'capture' ] } ) => {
278
306
return ( capture === 'viewport' ) || ( capture === 'fullPage' )
279
307
}
280
308
281
- const getShouldScale = ( { capture, scale } ) => {
309
+ const getShouldScale = ( { capture, scale } : {
310
+ capture : Cypress . ScreenshotOptions [ 'capture' ]
311
+ scale : Cypress . ScreenshotOptions [ 'scale' ]
312
+ } ) => {
282
313
return isAppOnly ( { capture } ) ? scale : true
283
314
}
284
315
285
- const getBlackout = ( { capture, blackout } ) => {
316
+ const getBlackout = ( { capture, blackout } : {
317
+ capture : Cypress . ScreenshotOptions [ 'capture' ]
318
+ blackout : Cypress . ScreenshotOptions [ 'blackout' ]
319
+ } ) => {
286
320
return isAppOnly ( { capture } ) ? blackout : [ ]
287
321
}
288
322
289
- // TODO: anys should be removed.
290
- type TakeScreenshotOptions = {
291
- name ?: string
292
- subject ?: any
293
- simple ?: boolean
294
- testFailure ?: boolean
295
- runnable ?: any
296
- log ?: any
297
- timeout ?: number
298
- }
299
-
300
- const takeScreenshot = ( Cypress , state , screenshotConfig , options : TakeScreenshotOptions = { } ) => {
323
+ const takeScreenshot = (
324
+ Cypress : Cypress . Cypress ,
325
+ state : StateFunc ,
326
+ screenshotConfig : Partial < Cypress . ScreenshotOptions > & Pick < Cypress . ScreenshotOptions , 'capture' | 'scale' | 'blackout' | 'overwrite' > ,
327
+ options : TakeScreenshotOptions ,
328
+ ) => {
301
329
const {
302
330
capture,
303
331
padding,
@@ -326,7 +354,8 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options: TakeScreensho
326
354
const getOptions = ( isOpen ) => {
327
355
return {
328
356
id : runnable . id ,
329
- testAttemptIndex : $utils . getTestFromRunnable ( runnable ) . _currentRetry ,
357
+ // @ts -ignore
358
+ testAttemptIndex : $utils . getTestFromRunnable ( runnable ) ?. _currentRetry ,
330
359
isOpen,
331
360
appOnly : isAppOnly ( screenshotConfig ) ,
332
361
scale : getShouldScale ( screenshotConfig ) ,
@@ -337,7 +366,7 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options: TakeScreensho
337
366
}
338
367
}
339
368
340
- const before = ( $body , $container ) => {
369
+ const before = ( $body : JQuery < HTMLBodyElement > , $container : JQuery < HTMLElement > ) => {
341
370
return Promise . try ( ( ) => {
342
371
if ( disableTimersAndAnimations ) {
343
372
return cy . pauseTimers ( true )
@@ -366,7 +395,7 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options: TakeScreensho
366
395
} )
367
396
}
368
397
369
- const after = ( $body ) => {
398
+ const after = ( $body : JQuery < HTMLBodyElement > ) => {
370
399
// could fail if iframe is cross-origin, so fail gracefully
371
400
try {
372
401
if ( disableTimersAndAnimations ) {
@@ -392,7 +421,7 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options: TakeScreensho
392
421
} )
393
422
}
394
423
395
- const automationOptions = _ . extend ( { } , options , {
424
+ const automationOptions : AutomationOptions = _ . extend ( { } , options , {
396
425
capture,
397
426
clip : {
398
427
x : 0 ,
@@ -413,13 +442,13 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options: TakeScreensho
413
442
} )
414
443
415
444
// use the subject as $el or yield the wrapped documentElement
416
- const $el = $dom . isElement ( subject )
445
+ const $el : JQuery < HTMLElement > = $dom . isElement ( subject )
417
446
? subject
418
447
: $dom . wrap ( state ( 'document' ) . documentElement )
419
448
420
449
// get the current body of the AUT to accurately calculate screenshot blackouts
421
450
// as well as properly enable/disable CSS animations while screenshotting is happening
422
- const $body = Cypress . $ ( 'body' )
451
+ const $body : JQuery < HTMLBodyElement > = Cypress . $ ( 'body' )
423
452
424
453
return before ( $body , $el )
425
454
. then ( ( ) => {
@@ -451,10 +480,6 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options: TakeScreensho
451
480
. finally ( ( ) => after ( $body ) )
452
481
}
453
482
454
- interface InternalScreenshotOptions extends Partial < Cypress . Loggable & Cypress . Timeoutable & Cypress . ScreenshotOptions > {
455
- _log ?: Log
456
- }
457
-
458
483
export default function ( Commands , Cypress , cy , state , config ) {
459
484
// failure screenshot when not interactive
460
485
Cypress . on ( 'runnable:after:run:async' , ( test , runnable ) => {
0 commit comments