@@ -6,41 +6,30 @@ const browserStackLog = (message) => {
66 }
77
88const 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.
910const 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+ */
1017const performModifiedScan = ( originalFn , Subject , stateType , ...args ) => {
1118 let customChaining = cy . wrap ( null ) . performScan ( ) ;
12- function changeSub ( args , stateType , newSubject ) {
19+ const changeSub = ( args , stateType , newSubject ) => {
1320 if ( stateType !== 'parent' ) {
1421 return [ newSubject , ...args . slice ( 1 ) ] ;
1522 }
1623 return args ;
1724 }
18- function runCutomizedCommand ( ) {
25+ const runCutomizedCommand = ( ) => {
1926 if ( ! Subject ) {
20- let orgS1 , orgS2 , cypressCommandSubject ;
21- if ( ( orgS2 = ( orgS1 = cy ) . subject ) !== null && orgS2 !== void 0 ) {
22- cypressCommandSubject = orgS2 . call ( orgS1 ) ;
23- }
24- else {
25- cypressCommandSubject = null ;
26- }
27- customChaining . then ( ( ) => cypressCommandSubject ) . then ( ( ) => { originalFn ( ...args ) } ) ;
28- }
29- else {
30- let orgSC1 , orgSC2 , timeO1 , cypressCommandChain , setTimeout ;
31- if ( ( timeO1 = args . find ( arg => arg !== null && arg !== void 0 ? arg . timeout : null ) ) !== null && timeO1 !== void 0 ) {
32- setTimeout = timeO1 . timeout ;
33- }
34- else {
35- setTimeout = null ;
36- }
37- if ( ( orgSC1 = ( orgSC2 = cy ) . subjectChain ) !== null && orgSC1 !== void 0 ) {
38- cypressCommandChain = orgSC1 . call ( orgSC2 ) ;
39- }
40- else {
41- cypressCommandChain = null ;
42- }
43- customChaining . performScanSubjectQuery ( cypressCommandChain , setTimeout ) . then ( { timeout : 30000 } , ( newSubject ) => originalFn ( ...changeSub ( args , stateType , newSubject ) ) ) ;
27+ let cypressCommandSubject = ( cy . subject ?. call ( cy ) ) ?? null ;
28+ customChaining . then ( ( ) => cypressCommandSubject ) . then ( ( ) => { originalFn ( ...args ) ; } ) ;
29+ } else {
30+ let setTimeout = args . find ( arg => arg ?. timeout ) ?. timeout ?? null ;
31+ let cypressCommandChain = ( cy . subjectChain ?. call ( cy ) ) ?? null ;
32+ customChaining . performScanSubjectQuery ( cypressCommandChain , setTimeout ) . then ( { timeout : 10000 } , newSubject => originalFn ( ...changeSub ( args , stateType , newSubject ) ) ) ;
4433 }
4534 }
4635 runCutomizedCommand ( ) ;
@@ -50,8 +39,7 @@ const performScan = (win, payloadToSend) =>
5039new Promise ( async ( resolve , reject ) => {
5140 const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
5241 if ( ! isHttpOrHttps ) {
53- resolve ( ) ;
54- return ;
42+ return resolve ( ) ;
5543 }
5644
5745 function findAccessibilityAutomationElement ( ) {
@@ -64,16 +52,14 @@ new Promise(async (resolve, reject) => {
6452 const intervalID = setInterval ( async ( ) => {
6553 if ( count > retryCount ) {
6654 clearInterval ( intervalID ) ;
67- reject (
55+ return reject (
6856 new Error (
6957 "Accessibility Automation Scanner is not ready on the page."
7058 )
7159 ) ;
72- return ;
7360 } else if ( findAccessibilityAutomationElement ( ) ) {
7461 clearInterval ( intervalID ) ;
75- resolve ( "Scanner set" ) ;
76- return ;
62+ return resolve ( "Scanner set" ) ;
7763 } else {
7864 count += 1 ;
7965 }
@@ -84,8 +70,7 @@ new Promise(async (resolve, reject) => {
8470 function startScan ( ) {
8571 function onScanComplete ( ) {
8672 win . removeEventListener ( "A11Y_SCAN_FINISHED" , onScanComplete ) ;
87- resolve ( ) ;
88- return ;
73+ return resolve ( ) ;
8974 }
9075
9176 win . addEventListener ( "A11Y_SCAN_FINISHED" , onScanComplete ) ;
@@ -99,8 +84,7 @@ new Promise(async (resolve, reject) => {
9984 waitForScannerReadiness ( )
10085 . then ( startScan )
10186 . catch ( async ( err ) => {
102- resolve ( "Scanner is not ready on the page after multiple retries. performscan" ) ;
103- return ;
87+ return resolve ( "Scanner is not ready on the page after multiple retries. performscan" ) ;
10488 } ) ;
10589 }
10690} )
@@ -109,8 +93,7 @@ const getAccessibilityResultsSummary = (win) =>
10993new Promise ( ( resolve ) => {
11094 const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( window . location . protocol ) ;
11195 if ( ! isHttpOrHttps ) {
112- resolve ( ) ;
113- return ;
96+ return resolve ( ) ;
11497 }
11598
11699 function findAccessibilityAutomationElement ( ) {
@@ -123,16 +106,14 @@ new Promise((resolve) => {
123106 const intervalID = setInterval ( ( ) => {
124107 if ( count > retryCount ) {
125108 clearInterval ( intervalID ) ;
126- reject (
109+ return reject (
127110 new Error (
128111 "Accessibility Automation Scanner is not ready on the page."
129112 )
130113 ) ;
131- return ;
132114 } else if ( findAccessibilityAutomationElement ( ) ) {
133115 clearInterval ( intervalID ) ;
134- resolve ( "Scanner set" ) ;
135- return ;
116+ return resolve ( "Scanner set" ) ;
136117 } else {
137118 count += 1 ;
138119 }
@@ -143,8 +124,7 @@ new Promise((resolve) => {
143124 function getSummary ( ) {
144125 function onReceiveSummary ( event ) {
145126 win . removeEventListener ( "A11Y_RESULTS_SUMMARY" , onReceiveSummary ) ;
146- resolve ( event . detail ) ;
147- return ;
127+ return resolve ( event . detail ) ;
148128 }
149129
150130 win . addEventListener ( "A11Y_RESULTS_SUMMARY" , onReceiveSummary ) ;
@@ -158,8 +138,7 @@ new Promise((resolve) => {
158138 waitForScannerReadiness ( )
159139 . then ( getSummary )
160140 . catch ( ( err ) => {
161- resolve ( ) ;
162- return ;
141+ return resolve ( ) ;
163142 } ) ;
164143 }
165144} )
@@ -168,8 +147,7 @@ const getAccessibilityResults = (win) =>
168147new Promise ( ( resolve ) => {
169148 const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( window . location . protocol ) ;
170149 if ( ! isHttpOrHttps ) {
171- resolve ( ) ;
172- return ;
150+ return resolve ( ) ;
173151 }
174152
175153 function findAccessibilityAutomationElement ( ) {
@@ -182,16 +160,14 @@ new Promise((resolve) => {
182160 const intervalID = setInterval ( ( ) => {
183161 if ( count > retryCount ) {
184162 clearInterval ( intervalID ) ;
185- reject (
163+ return reject (
186164 new Error (
187165 "Accessibility Automation Scanner is not ready on the page."
188166 )
189167 ) ;
190- return ;
191168 } else if ( findAccessibilityAutomationElement ( ) ) {
192169 clearInterval ( intervalID ) ;
193- resolve ( "Scanner set" ) ;
194- return ;
170+ return resolve ( "Scanner set" ) ;
195171 } else {
196172 count += 1 ;
197173 }
@@ -202,8 +178,7 @@ new Promise((resolve) => {
202178 function getResults ( ) {
203179 function onReceivedResult ( event ) {
204180 win . removeEventListener ( "A11Y_RESULTS_RESPONSE" , onReceivedResult ) ;
205- resolve ( event . detail ) ;
206- return ;
181+ return resolve ( event . detail ) ;
207182 }
208183
209184 win . addEventListener ( "A11Y_RESULTS_RESPONSE" , onReceivedResult ) ;
@@ -217,8 +192,7 @@ new Promise((resolve) => {
217192 waitForScannerReadiness ( )
218193 . then ( getResults )
219194 . catch ( ( err ) => {
220- resolve ( ) ;
221- return ;
195+ return resolve ( ) ;
222196 } ) ;
223197 }
224198} ) ;
@@ -242,16 +216,14 @@ new Promise( (resolve, reject) => {
242216 const intervalID = setInterval ( async ( ) => {
243217 if ( count > retryCount ) {
244218 clearInterval ( intervalID ) ;
245- reject (
219+ return reject (
246220 new Error (
247221 "Accessibility Automation Scanner is not ready on the page."
248222 )
249223 ) ;
250- return ;
251224 } else if ( findAccessibilityAutomationElement ( ) ) {
252225 clearInterval ( intervalID ) ;
253- resolve ( "Scanner set" ) ;
254- return ;
226+ return resolve ( "Scanner set" ) ;
255227 } else {
256228 count += 1 ;
257229 }
@@ -261,8 +233,7 @@ new Promise( (resolve, reject) => {
261233
262234 function saveResults ( ) {
263235 function onResultsSaved ( event ) {
264- resolve ( ) ;
265- return ;
236+ return resolve ( ) ;
266237 }
267238 win . addEventListener ( "A11Y_RESULTS_SAVED" , onResultsSaved ) ;
268239 const e = new CustomEvent ( "A11Y_SAVE_RESULTS" , {
@@ -277,14 +248,12 @@ new Promise( (resolve, reject) => {
277248 waitForScannerReadiness ( )
278249 . then ( saveResults )
279250 . catch ( async ( err ) => {
280- resolve ( "Scanner is not ready on the page after multiple retries. after run" ) ;
281- return ;
251+ return resolve ( "Scanner is not ready on the page after multiple retries. after run" ) ;
282252 } ) ;
283253 }
284254 } catch ( error ) {
285255 browserStackLog ( `Error in saving results with error: ${ error . message } ` ) ;
286- resolve ( ) ;
287- return ;
256+ return resolve ( ) ;
288257 }
289258
290259} )
@@ -348,7 +317,7 @@ afterEach(() => {
348317 let shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
349318 if ( ! shouldScanTestForAccessibility ) return cy . wrap ( { } ) ;
350319
351- cy . wrap ( performScan ( win ) , { timeout : 30000 } ) . then ( ( ) => {
320+ cy . wrap ( performScan ( win ) , { timeout : 10000 } ) . then ( ( ) => {
352321 try {
353322 let os_data ;
354323 if ( Cypress . env ( "OS" ) ) {
@@ -379,7 +348,7 @@ afterEach(() => {
379348 }
380349 } ;
381350 browserStackLog ( `Saving accessibility test results` ) ;
382- cy . wrap ( saveTestResults ( win , payloadToSend ) , { timeout : 30000 } ) . then ( ( ) => {
351+ cy . wrap ( saveTestResults ( win , payloadToSend ) , { timeout : 10000 } ) . then ( ( ) => {
383352 browserStackLog ( `Saved accessibility test results` ) ;
384353 } )
385354
@@ -400,7 +369,7 @@ Cypress.Commands.add('performScan', () => {
400369 }
401370 cy . window ( ) . then ( async ( win ) => {
402371 browserStackLog ( `Performing accessibility scan` ) ;
403- cy . wrap ( performScan ( win ) , { timeout :40000 } ) ;
372+ cy . wrap ( performScan ( win ) , { timeout :10000 } ) ;
404373 } ) ;
405374 } catch ( error ) {
406375 browserStackLog ( `Error in performing scan with error: ${ error . message } ` ) ;
0 commit comments