1
1
/* Event listeners + custom commands for Cypress */
2
2
3
+ const browserStackLog = ( message ) => {
4
+ if ( ! Cypress . env ( 'BROWSERSTACK_LOGS' ) ) return ;
5
+ cy . task ( 'browserstack_log' , message ) ;
6
+ }
7
+
3
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' ] ;
4
9
5
10
const performScan = ( win , payloadToSend ) =>
6
11
new Promise ( async ( resolve , reject ) => {
7
-
8
12
const isHttpOrHttps = / ^ ( h t t p | h t t p s ) : $ / . test ( win . location . protocol ) ;
9
13
if ( ! isHttpOrHttps ) {
10
14
resolve ( ) ;
@@ -107,7 +111,7 @@ const getAccessibilityResultsSummary = (win) =>
107
111
waitForScannerReadiness ( )
108
112
. then ( getSummary )
109
113
. catch ( ( err ) => {
110
-
114
+ resolve ( ) ;
111
115
} ) ;
112
116
}
113
117
} )
@@ -146,7 +150,6 @@ const getAccessibilityResults = (win) =>
146
150
147
151
function getResults ( ) {
148
152
function onReceivedResult ( event ) {
149
-
150
153
win . removeEventListener ( "A11Y_RESULTS_RESPONSE" , onReceivedResult ) ;
151
154
resolve ( event . detail ) ;
152
155
}
@@ -162,7 +165,7 @@ const getAccessibilityResults = (win) =>
162
165
waitForScannerReadiness ( )
163
166
. then ( getResults )
164
167
. catch ( ( err ) => {
165
-
168
+ resolve ( ) ;
166
169
} ) ;
167
170
}
168
171
} ) ;
@@ -253,7 +256,7 @@ const shouldScanForAccessibility = (attributes) => {
253
256
const included = includeTagArray . length === 0 || includeTags . some ( ( include ) => fullTestName . includes ( include ) ) ;
254
257
shouldScanTestForAccessibility = ! excluded && included ;
255
258
} 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 ) ;
257
260
}
258
261
}
259
262
@@ -274,7 +277,8 @@ Cypress.on('command:start', async (command) => {
274
277
if ( ! shouldScanTestForAccessibility ) return ;
275
278
276
279
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 } ) ;
278
282
} )
279
283
} )
280
284
@@ -314,7 +318,10 @@ afterEach(() => {
314
318
"browser_version" : Cypress . browser . version
315
319
}
316
320
} ;
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
+ } )
318
325
319
326
} catch ( er ) {
320
327
}
@@ -327,12 +334,12 @@ Cypress.Commands.add('performScan', () => {
327
334
const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
328
335
const shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
329
336
if ( ! shouldScanTestForAccessibility ) {
330
- console . log ( `Not a Accessibility Automation session, cannot perform scan.` ) ;
337
+ browserStackLog ( `Not a Accessibility Automation session, cannot perform scan.` ) ;
331
338
return cy . wrap ( { } ) ;
332
339
}
333
340
cy . window ( ) . then ( async ( win ) => {
341
+ browserStackLog ( `Performing accessibility scan` ) ;
334
342
await performScan ( win ) ;
335
- return await getAccessibilityResultsSummary ( win ) ;
336
343
} ) ;
337
344
} catch { }
338
345
} )
@@ -342,11 +349,12 @@ Cypress.Commands.add('getAccessibilityResultsSummary', () => {
342
349
const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
343
350
const shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
344
351
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.` ) ;
346
353
return cy . wrap ( { } ) ;
347
354
}
348
355
cy . window ( ) . then ( async ( win ) => {
349
356
await performScan ( win ) ;
357
+ browserStackLog ( 'Getting accessibility results summary' ) ;
350
358
return await getAccessibilityResultsSummary ( win ) ;
351
359
} ) ;
352
360
} catch { }
@@ -358,14 +366,15 @@ Cypress.Commands.add('getAccessibilityResults', () => {
358
366
const attributes = Cypress . mocha . getRunner ( ) . suite . ctx . currentTest || Cypress . mocha . getRunner ( ) . suite . ctx . _runnable ;
359
367
const shouldScanTestForAccessibility = shouldScanForAccessibility ( attributes ) ;
360
368
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.` ) ;
362
370
return cy . wrap ( { } ) ;
363
371
}
364
372
365
373
/* browserstack_accessibility_automation_script */
366
374
367
375
cy . window ( ) . then ( async ( win ) => {
368
376
await performScan ( win ) ;
377
+ browserStackLog ( 'Getting accessibility results' ) ;
369
378
return await getAccessibilityResults ( win ) ;
370
379
} ) ;
371
380
0 commit comments