@@ -6,32 +6,68 @@ const browserStackLog = (message) => {
6
6
}
7
7
8
8
const commandsToWrap = [ 'visit' , 'click' , 'type' , 'request' , 'dblclick' , 'rightclick' , 'clear' , 'check' , 'uncheck' , 'select' , 'trigger' , 'selectFile' , 'scrollIntoView' , 'scroll' , 'scrollTo' , 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin' ] ;
9
+ // scroll is not a default function in cypress.
10
+ const commandToOverwrite = [ 'visit' , 'click' , 'type' , 'request' , 'dblclick' , 'rightclick' , 'clear' , 'check' , 'uncheck' , 'select' , 'trigger' , 'selectFile' , 'scrollIntoView' , 'scrollTo' , 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin' ] ;
11
+
12
+ /*
13
+ Overrriding the cypress commands to perform Accessibility Scan before Each command
14
+ - runCutomizedCommand is handling both the cases of subject available in cypress original command
15
+ and chaning available from original cypress command.
16
+ */
17
+ const performModifiedScan = ( originalFn , Subject , stateType , ...args ) => {
18
+ let customChaining = cy . wrap ( null ) . performScan ( ) ;
19
+ const changeSub = ( args , stateType , newSubject ) => {
20
+ if ( stateType !== 'parent' ) {
21
+ return [ newSubject , ...args . slice ( 1 ) ] ;
22
+ }
23
+ return args ;
24
+ }
25
+ const runCustomizedCommand = ( ) => {
26
+ if ( ! Subject ) {
27
+ let orgS1 , orgS2 , cypressCommandSubject = null ;
28
+ if ( ( orgS2 = ( orgS1 = cy ) . subject ) !== null && orgS2 !== void 0 ) {
29
+ cypressCommandSubject = orgS2 . call ( orgS1 ) ;
30
+ }
31
+ customChaining . then ( ( ) => cypressCommandSubject ) . then ( ( ) => { originalFn ( ...args ) } ) ;
32
+ } else {
33
+ let orgSC1 , orgSC2 , timeO1 , cypressCommandChain = null , setTimeout = null ;
34
+ if ( ( timeO1 = args . find ( arg => arg !== null && arg !== void 0 ? arg . timeout : null ) ) !== null && timeO1 !== void 0 ) {
35
+ setTimeout = timeO1 . timeout ;
36
+ }
37
+ if ( ( orgSC1 = ( orgSC2 = cy ) . subjectChain ) !== null && orgSC1 !== void 0 ) {
38
+ cypressCommandChain = orgSC1 . call ( orgSC2 ) ;
39
+ }
40
+ customChaining . performScanSubjectQuery ( cypressCommandChain , setTimeout ) . then ( { timeout : 30000 } , ( newSubject ) => originalFn ( ...changeSub ( args , stateType , newSubject ) ) ) ;
41
+ }
42
+ }
43
+ runCustomizedCommand ( ) ;
44
+ }
9
45
10
46
const performScan = ( win , payloadToSend ) =>
11
47
new Promise ( async ( resolve , reject ) => {
12
48
const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
13
49
if ( ! isHttpOrHttps ) {
14
- resolve ( ) ;
50
+ return resolve ( ) ;
15
51
}
16
52
17
53
function findAccessibilityAutomationElement ( ) {
18
54
return win . document . querySelector ( "#accessibility-automation-element" ) ;
19
55
}
20
56
21
- function waitForScannerReadiness ( retryCount = 30 , retryInterval = 100 ) {
57
+ function waitForScannerReadiness ( retryCount = 100 , retryInterval = 100 ) {
22
58
return new Promise ( async ( resolve , reject ) => {
23
59
let count = 0 ;
24
60
const intervalID = setInterval ( async ( ) => {
25
61
if ( count > retryCount ) {
26
62
clearInterval ( intervalID ) ;
27
- reject (
63
+ return reject (
28
64
new Error (
29
65
"Accessibility Automation Scanner is not ready on the page."
30
66
)
31
67
) ;
32
68
} else if ( findAccessibilityAutomationElement ( ) ) {
33
69
clearInterval ( intervalID ) ;
34
- resolve ( "Scanner set" ) ;
70
+ return resolve ( "Scanner set" ) ;
35
71
} else {
36
72
count += 1 ;
37
73
}
@@ -42,7 +78,7 @@ new Promise(async (resolve, reject) => {
42
78
function startScan ( ) {
43
79
function onScanComplete ( ) {
44
80
win . removeEventListener ( "A11Y_SCAN_FINISHED" , onScanComplete ) ;
45
- resolve ( ) ;
81
+ return resolve ( ) ;
46
82
}
47
83
48
84
win . addEventListener ( "A11Y_SCAN_FINISHED" , onScanComplete ) ;
@@ -56,16 +92,16 @@ new Promise(async (resolve, reject) => {
56
92
waitForScannerReadiness ( )
57
93
. then ( startScan )
58
94
. catch ( async ( err ) => {
59
- resolve ( "Scanner is not ready on the page after multiple retries. performscan" ) ;
60
- } ) ;
95
+ return resolve ( "Scanner is not ready on the page after multiple retries. performscan" ) ;
96
+ } ) ;
61
97
}
62
98
} )
63
99
64
100
const getAccessibilityResultsSummary = ( win ) =>
65
101
new Promise ( ( resolve ) => {
66
102
const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( window . location . protocol ) ;
67
103
if ( ! isHttpOrHttps ) {
68
- resolve ( ) ;
104
+ return resolve ( ) ;
69
105
}
70
106
71
107
function findAccessibilityAutomationElement ( ) {
@@ -78,14 +114,14 @@ new Promise((resolve) => {
78
114
const intervalID = setInterval ( ( ) => {
79
115
if ( count > retryCount ) {
80
116
clearInterval ( intervalID ) ;
81
- reject (
117
+ return reject (
82
118
new Error (
83
119
"Accessibility Automation Scanner is not ready on the page."
84
120
)
85
121
) ;
86
122
} else if ( findAccessibilityAutomationElement ( ) ) {
87
123
clearInterval ( intervalID ) ;
88
- resolve ( "Scanner set" ) ;
124
+ return resolve ( "Scanner set" ) ;
89
125
} else {
90
126
count += 1 ;
91
127
}
@@ -96,7 +132,7 @@ new Promise((resolve) => {
96
132
function getSummary ( ) {
97
133
function onReceiveSummary ( event ) {
98
134
win . removeEventListener ( "A11Y_RESULTS_SUMMARY" , onReceiveSummary ) ;
99
- resolve ( event . detail ) ;
135
+ return resolve ( event . detail ) ;
100
136
}
101
137
102
138
win . addEventListener ( "A11Y_RESULTS_SUMMARY" , onReceiveSummary ) ;
@@ -110,16 +146,16 @@ new Promise((resolve) => {
110
146
waitForScannerReadiness ( )
111
147
. then ( getSummary )
112
148
. catch ( ( err ) => {
113
- resolve ( ) ;
114
- } ) ;
149
+ return resolve ( ) ;
150
+ } ) ;
115
151
}
116
152
} )
117
153
118
154
const getAccessibilityResults = ( win ) =>
119
155
new Promise ( ( resolve ) => {
120
156
const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( window . location . protocol ) ;
121
157
if ( ! isHttpOrHttps ) {
122
- resolve ( ) ;
158
+ return resolve ( ) ;
123
159
}
124
160
125
161
function findAccessibilityAutomationElement ( ) {
@@ -132,14 +168,14 @@ new Promise((resolve) => {
132
168
const intervalID = setInterval ( ( ) => {
133
169
if ( count > retryCount ) {
134
170
clearInterval ( intervalID ) ;
135
- reject (
171
+ return reject (
136
172
new Error (
137
173
"Accessibility Automation Scanner is not ready on the page."
138
174
)
139
175
) ;
140
176
} else if ( findAccessibilityAutomationElement ( ) ) {
141
177
clearInterval ( intervalID ) ;
142
- resolve ( "Scanner set" ) ;
178
+ return resolve ( "Scanner set" ) ;
143
179
} else {
144
180
count += 1 ;
145
181
}
@@ -150,7 +186,7 @@ new Promise((resolve) => {
150
186
function getResults ( ) {
151
187
function onReceivedResult ( event ) {
152
188
win . removeEventListener ( "A11Y_RESULTS_RESPONSE" , onReceivedResult ) ;
153
- resolve ( event . detail ) ;
189
+ return resolve ( event . detail ) ;
154
190
}
155
191
156
192
win . addEventListener ( "A11Y_RESULTS_RESPONSE" , onReceivedResult ) ;
@@ -164,8 +200,8 @@ new Promise((resolve) => {
164
200
waitForScannerReadiness ( )
165
201
. then ( getResults )
166
202
. catch ( ( err ) => {
167
- resolve ( ) ;
168
- } ) ;
203
+ return resolve ( ) ;
204
+ } ) ;
169
205
}
170
206
} ) ;
171
207
@@ -175,6 +211,7 @@ new Promise( (resolve, reject) => {
175
211
const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
176
212
if ( ! isHttpOrHttps ) {
177
213
resolve ( "Unable to save accessibility results, Invalid URL." ) ;
214
+ return ;
178
215
}
179
216
180
217
function findAccessibilityAutomationElement ( ) {
@@ -187,14 +224,14 @@ new Promise( (resolve, reject) => {
187
224
const intervalID = setInterval ( async ( ) => {
188
225
if ( count > retryCount ) {
189
226
clearInterval ( intervalID ) ;
190
- reject (
227
+ return reject (
191
228
new Error (
192
229
"Accessibility Automation Scanner is not ready on the page."
193
230
)
194
231
) ;
195
232
} else if ( findAccessibilityAutomationElement ( ) ) {
196
233
clearInterval ( intervalID ) ;
197
- resolve ( "Scanner set" ) ;
234
+ return resolve ( "Scanner set" ) ;
198
235
} else {
199
236
count += 1 ;
200
237
}
@@ -204,7 +241,7 @@ new Promise( (resolve, reject) => {
204
241
205
242
function saveResults ( ) {
206
243
function onResultsSaved ( event ) {
207
- resolve ( ) ;
244
+ return resolve ( ) ;
208
245
}
209
246
win . addEventListener ( "A11Y_RESULTS_SAVED" , onResultsSaved ) ;
210
247
const e = new CustomEvent ( "A11Y_SAVE_RESULTS" , {
@@ -219,11 +256,12 @@ new Promise( (resolve, reject) => {
219
256
waitForScannerReadiness ( )
220
257
. then ( saveResults )
221
258
. catch ( async ( err ) => {
222
- resolve ( "Scanner is not ready on the page after multiple retries. after run" ) ;
259
+ return resolve ( "Scanner is not ready on the page after multiple retries. after run" ) ;
223
260
} ) ;
224
261
}
225
- } catch ( er ) {
226
- resolve ( )
262
+ } catch ( error ) {
263
+ browserStackLog ( `Error in saving results with error: ${ error . message } ` ) ;
264
+ return resolve ( ) ;
227
265
}
228
266
229
267
} )
@@ -254,31 +292,29 @@ const shouldScanForAccessibility = (attributes) => {
254
292
const included = includeTagArray . length === 0 || includeTags . some ( ( include ) => fullTestName . includes ( include ) ) ;
255
293
shouldScanTestForAccessibility = ! excluded && included ;
256
294
} catch ( error ) {
257
- browserStackLog ( " Error while validating test case for accessibility before scanning. Error : " , error ) ;
295
+ browserStackLog ( ` Error while validating test case for accessibility before scanning. Error : ${ error . message } ` ) ;
258
296
}
259
297
}
260
298
261
299
return shouldScanTestForAccessibility ;
262
300
}
263
301
264
- Cypress . on ( 'command:start' , async ( command ) => {
265
- if ( ! command || ! command . attributes ) return ;
266
- if ( command . attributes . name == 'window' || command . attributes . name == 'then' || command . attributes . name == 'wrap' ) {
267
- return ;
268
- }
269
-
270
- if ( ! commandsToWrap . includes ( command . attributes . name ) ) return ;
271
-
272
- const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
273
-
274
- let shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
275
- if ( ! shouldScanTestForAccessibility ) return ;
276
-
277
- cy . window ( ) . then ( ( win ) => {
278
- browserStackLog ( 'Performing scan form command ' + command . attributes . name ) ;
279
- cy . wrap ( performScan ( win , { method : command . attributes . name } ) , { timeout : 30000 } ) ;
280
- } )
281
- } )
302
+ commandToOverwrite . forEach ( ( command ) => {
303
+ Cypress . Commands . overwrite ( command , ( originalFn , ...args ) => {
304
+ const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
305
+ const shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
306
+ const state = cy . state ( 'current' ) , Subject = 'getSubjectFromChain' in cy ;
307
+ const stateName = state === null || state === void 0 ? void 0 : state . get ( 'name' ) ;
308
+ let stateType = null ;
309
+ if ( ! shouldScanTestForAccessibility || ( stateName && stateName !== command ) ) {
310
+ return originalFn ( ...args ) ;
311
+ }
312
+ if ( state !== null && state !== void 0 ) {
313
+ stateType = state . get ( 'type' ) ;
314
+ }
315
+ performModifiedScan ( originalFn , Subject , stateType , ...args ) ;
316
+ } ) ;
317
+ } ) ;
282
318
283
319
afterEach ( ( ) => {
284
320
const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest ;
@@ -322,6 +358,7 @@ afterEach(() => {
322
358
} )
323
359
324
360
} catch ( er ) {
361
+ browserStackLog ( `Error in saving results with error: ${ er . message } ` ) ;
325
362
}
326
363
} )
327
364
} ) ;
@@ -337,9 +374,11 @@ Cypress.Commands.add('performScan', () => {
337
374
}
338
375
cy . window ( ) . then ( async ( win ) => {
339
376
browserStackLog ( `Performing accessibility scan` ) ;
340
- await performScan ( win ) ;
377
+ cy . wrap ( performScan ( win ) , { timeout : 30000 } ) ;
341
378
} ) ;
342
- } catch { }
379
+ } catch ( error ) {
380
+ browserStackLog ( `Error in performing scan with error: ${ error . message } ` ) ;
381
+ }
343
382
} )
344
383
345
384
Cypress . Commands . add ( 'getAccessibilityResultsSummary' , ( ) => {
@@ -355,7 +394,9 @@ Cypress.Commands.add('getAccessibilityResultsSummary', () => {
355
394
browserStackLog ( 'Getting accessibility results summary' ) ;
356
395
return await getAccessibilityResultsSummary ( win ) ;
357
396
} ) ;
358
- } catch { }
397
+ } catch ( error ) {
398
+ browserStackLog ( `Error in getting accessibilty results summary with error: ${ error . message } ` ) ;
399
+ }
359
400
360
401
} ) ;
361
402
@@ -376,6 +417,12 @@ Cypress.Commands.add('getAccessibilityResults', () => {
376
417
return await getAccessibilityResults ( win ) ;
377
418
} ) ;
378
419
379
- } catch { }
420
+ } catch ( error ) {
421
+ browserStackLog ( `Error in getting accessibilty results with error: ${ error . message } ` ) ;
422
+ }
423
+ } ) ;
380
424
425
+ Cypress . Commands . addQuery ( 'performScanSubjectQuery' , function ( chaining , setTimeout ) {
426
+ this . set ( 'timeout' , setTimeout ) ;
427
+ return ( ) => cy . getSubjectFromChain ( chaining ) ;
381
428
} ) ;
0 commit comments