11/* Event listeners + custom commands for Cypress */
22
3+ const browserStackLog = ( message ) => {
4+ if ( ! Cypress . env ( 'BROWSERSTACK_LOGS' ) ) return ;
5+ cy . task ( 'browserstack_log' , message ) ;
6+ }
7+
38const commandsToWrap = [ 'visit' , 'click' , 'type' , 'request' , 'dblclick' , 'rightclick' , 'clear' , 'check' , 'uncheck' , 'select' , 'trigger' , 'selectFile' , 'scrollIntoView' , 'scroll' , 'scrollTo' , 'blur' , 'focus' , 'go' , 'reload' , 'submit' , 'viewport' , 'origin' ] ;
49
510const performScan = ( win , payloadToSend ) =>
611 new Promise ( async ( resolve , reject ) => {
7-
812 const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
913 if ( ! isHttpOrHttps ) {
1014 resolve ( ) ;
@@ -107,7 +111,7 @@ const getAccessibilityResultsSummary = (win) =>
107111 waitForScannerReadiness ( )
108112 . then ( getSummary )
109113 . catch ( ( err ) => {
110-
114+ resolve ( ) ;
111115 } ) ;
112116 }
113117 } )
@@ -146,7 +150,6 @@ const getAccessibilityResults = (win) =>
146150
147151 function getResults ( ) {
148152 function onReceivedResult ( event ) {
149-
150153 win . removeEventListener ( "A11Y_RESULTS_RESPONSE" , onReceivedResult ) ;
151154 resolve ( event . detail ) ;
152155 }
@@ -162,7 +165,7 @@ const getAccessibilityResults = (win) =>
162165 waitForScannerReadiness ( )
163166 . then ( getResults )
164167 . catch ( ( err ) => {
165-
168+ resolve ( ) ;
166169 } ) ;
167170 }
168171 } ) ;
@@ -253,7 +256,7 @@ const shouldScanForAccessibility = (attributes) => {
253256 const included = includeTagArray . length === 0 || includeTags . some ( ( include ) => fullTestName . includes ( include ) ) ;
254257 shouldScanTestForAccessibility = ! excluded && included ;
255258 } catch ( error ) {
256- console . log ( "Error while validating test case for accessibility before scanning. Error : " , error ) ;
259+ browserStackLog ( "Error while validating test case for accessibility before scanning. Error : " , error ) ;
257260 }
258261 }
259262
@@ -274,7 +277,8 @@ Cypress.on('command:start', async (command) => {
274277 if ( ! shouldScanTestForAccessibility ) return ;
275278
276279 cy . window ( ) . then ( ( win ) => {
277- cy . wrap ( performScan ( win , { method : command . attributes . name } ) , { timeout : 30000 } )
280+ browserStackLog ( 'Performsing scan form command ' + command . attributes . name ) ;
281+ cy . wrap ( performScan ( win , { method : command . attributes . name } ) , { timeout : 30000 } ) ;
278282 } )
279283} )
280284
@@ -314,7 +318,10 @@ afterEach(() => {
314318 "browser_version" : Cypress . browser . version
315319 }
316320 } ;
317- cy . wrap ( saveTestResults ( win , payloadToSend ) , { timeout : 30000 } )
321+ browserStackLog ( `Saving accessibility test results` ) ;
322+ cy . wrap ( saveTestResults ( win , payloadToSend ) , { timeout : 30000 } ) . then ( ( ) => {
323+ browserStackLog ( `Saved accessibility test results` ) ;
324+ } )
318325
319326 } catch ( er ) {
320327 }
@@ -327,12 +334,12 @@ Cypress.Commands.add('performScan', () => {
327334 const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
328335 const shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
329336 if ( ! shouldScanTestForAccessibility ) {
330- console . log ( `Not a Accessibility Automation session, cannot perform scan.` ) ;
337+ browserStackLog ( `Not a Accessibility Automation session, cannot perform scan.` ) ;
331338 return cy . wrap ( { } ) ;
332339 }
333340 cy . window ( ) . then ( async ( win ) => {
341+ browserStackLog ( `Performing accessibility scan` ) ;
334342 await performScan ( win ) ;
335- return await getAccessibilityResultsSummary ( win ) ;
336343 } ) ;
337344 } catch { }
338345} )
@@ -342,11 +349,12 @@ Cypress.Commands.add('getAccessibilityResultsSummary', () => {
342349 const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
343350 const shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
344351 if ( ! shouldScanTestForAccessibility ) {
345- console . log ( `Not a Accessibility Automation session, cannot retrieve Accessibility results summary.` ) ;
352+ browserStackLog ( `Not a Accessibility Automation session, cannot retrieve Accessibility results summary.` ) ;
346353 return cy . wrap ( { } ) ;
347354 }
348355 cy . window ( ) . then ( async ( win ) => {
349356 await performScan ( win ) ;
357+ browserStackLog ( 'Getting accessibility results summary' ) ;
350358 return await getAccessibilityResultsSummary ( win ) ;
351359 } ) ;
352360 } catch { }
@@ -358,14 +366,15 @@ Cypress.Commands.add('getAccessibilityResults', () => {
358366 const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
359367 const shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
360368 if ( ! shouldScanTestForAccessibility ) {
361- console . log ( `Not a Accessibility Automation session, cannot retrieve Accessibility results.` ) ;
369+ browserStackLog ( `Not a Accessibility Automation session, cannot retrieve Accessibility results.` ) ;
362370 return cy . wrap ( { } ) ;
363371 }
364372
365373/* browserstack_accessibility_automation_script */
366374
367375 cy . window ( ) . then ( async ( win ) => {
368376 await performScan ( win ) ;
377+ browserStackLog ( 'Getting accessibility results' ) ;
369378 return await getAccessibilityResults ( win ) ;
370379 } ) ;
371380
0 commit comments