11/* Event listeners + custom commands for Cypress */
22
33/* Used to detect Gherkin steps */
4+
5+ const browserStackLog = ( message ) => {
6+ if ( ! Cypress . env ( 'BROWSERSTACK_LOGS' ) ) return ;
7+ cy . task ( 'browserstack_log' , message ) ;
8+ }
9+
10+ const shouldSkipCommand = ( command ) => {
11+ return command . attributes . name == 'log' || ( command . attributes . name == 'task' && ( [ 'test_observability_platform_details' , 'test_observability_step' , 'test_observability_command' ] . some ( event => command . attributes . args . includes ( event ) ) ) ) ;
12+ }
13+
414Cypress . on ( 'log:added' , ( log ) => {
5- return ( ) => {
6- return cy . now ( ' task' , 'test_observability_step' , {
7- log
8- } , { log : false } )
9- }
10- } ) ;
11-
15+ return ( ) => {
16+ return cy . task ( 'test_observability_step' , {
17+ log
18+ } , { log : false } )
19+ }
20+ } ) ;
21+
1222Cypress . on ( 'command:start' , ( command ) => {
13- if ( ! command || ! command . attributes ) return ;
14- if ( command . attributes . name == 'log' || ( command . attributes . name == 'task' && ( command . attributes . args . includes ( 'test_observability_command' ) || command . attributes . args . includes ( 'test_observability_log' ) ) ) ) {
23+ if ( ! command || ! command . attributes ) return ;
24+ if ( shouldSkipCommand ( command ) ) {
1525 return ;
1626 }
1727 /* Send command details */
18- cy . now ( ' task' , 'test_observability_command' , {
28+ cy . task ( 'test_observability_command' , {
1929 type : 'COMMAND_START' ,
2030 command : {
2131 attributes : {
@@ -25,27 +35,23 @@ Cypress.on('command:start', (command) => {
2535 } ,
2636 state : 'pending'
2737 }
28- } , { log : false } ) . then ( ( res ) => {
29- } ) . catch ( ( err ) => {
30- } ) ;
38+ } , { log : false } ) ;
3139
3240 /* Send platform details */
33- cy . now ( ' task' , 'test_observability_platform_details' , {
34- testTitle : Cypress . currentTest . title ,
41+ cy . task ( 'test_observability_platform_details' , {
42+ testTitle : Cypress . currentRunnable ? .title || '' ,
3543 browser : Cypress . browser ,
3644 platform : Cypress . platform ,
3745 cypressVersion : Cypress . version
38- } , { log : false } ) . then ( ( res ) => {
39- } ) . catch ( ( err ) => {
40- } ) ;
46+ } , { log : false } ) ;
4147} ) ;
4248
4349Cypress . on ( 'command:retry' , ( command ) => {
44- if ( ! command || ! command . attributes ) return ;
45- if ( command . attributes . name == 'log' || ( command . attributes . name == 'task' && ( command . attributes . args . includes ( 'test_observability_command' ) || command . attributes . args . includes ( 'test_observability_log' ) ) ) ) {
50+ if ( ! command || ! command . attributes ) return ;
51+ if ( shouldSkipCommand ( command ) ) {
4652 return ;
4753 }
48- cy . now ( ' task' , 'test_observability_command' , {
54+ cy . task ( 'test_observability_command' , {
4955 type : 'COMMAND_RETRY' ,
5056 command : {
5157 _log : command . _log ,
@@ -54,17 +60,15 @@ Cypress.on('command:retry', (command) => {
5460 isDefaultAssertionErr : command && command . error ? command . error . isDefaultAssertionErr : null
5561 }
5662 }
57- } , { log : false } ) . then ( ( res ) => {
58- } ) . catch ( ( err ) => {
59- } ) ;
63+ } , { log : false } ) ;
6064} ) ;
6165
6266Cypress . on ( 'command:end' , ( command ) => {
63- if ( ! command || ! command . attributes ) return ;
64- if ( command . attributes . name == 'log' || ( command . attributes . name == 'task' && ( command . attributes . args . includes ( 'test_observability_command' ) || command . attributes . args . includes ( 'test_observability_log' ) ) ) ) {
67+ if ( ! command || ! command . attributes ) return ;
68+ if ( shouldSkipCommand ( command ) ) {
6569 return ;
6670 }
67- cy . now ( ' task' , 'test_observability_command' , {
71+ cy . task ( 'test_observability_command' , {
6872 'type' : 'COMMAND_END' ,
6973 'command' : {
7074 'attributes' : {
@@ -74,85 +78,69 @@ Cypress.on('command:end', (command) => {
7478 } ,
7579 'state' : command . state
7680 }
77- } , { log : false } ) . then ( ( res ) => {
78- } ) . catch ( ( err ) => {
79- } ) ;
81+ } , { log : false } ) ;
8082} ) ;
8183
8284Cypress . Commands . overwrite ( 'log' , ( originalFn , ...args ) => {
83- if ( args . includes ( 'test_observability_log' ) || args . includes ( 'test_observability_command' ) ) return ;
85+ if ( args . includes ( 'test_observability_log' ) || args . includes ( 'test_observability_command' ) ) return ;
8486 const message = args . reduce ( ( result , logItem ) => {
8587 if ( typeof logItem === 'object' ) {
8688 return [ result , JSON . stringify ( logItem ) ] . join ( ' ' ) ;
8789 }
8890
8991 return [ result , logItem ? logItem . toString ( ) : '' ] . join ( ' ' ) ;
9092 } , '' ) ;
91- cy . now ( ' task' , 'test_observability_log' , {
93+ cy . task ( 'test_observability_log' , {
9294 'level' : 'info' ,
9395 message,
94- } , { log : false } ) . then ( ( res ) => {
95- } ) . catch ( ( err ) => {
96- } ) ;
96+ } , { log : false } ) ;
9797 originalFn ( ...args ) ;
9898} ) ;
9999
100100Cypress . Commands . add ( 'trace' , ( message , file ) => {
101- cy . now ( ' task' , 'test_observability_log' , {
101+ cy . task ( 'test_observability_log' , {
102102 level : 'trace' ,
103103 message,
104104 file,
105- } ) . then ( ( res ) => {
106- } ) . catch ( ( err ) => {
107105 } ) ;
108106} ) ;
109107
110108Cypress . Commands . add ( 'logDebug' , ( message , file ) => {
111- cy . now ( ' task' , 'test_observability_log' , {
109+ cy . task ( 'test_observability_log' , {
112110 level : 'debug' ,
113111 message,
114112 file,
115- } ) . then ( ( res ) => {
116- } ) . catch ( ( err ) => {
117113 } ) ;
118114} ) ;
119115
120116Cypress . Commands . add ( 'info' , ( message , file ) => {
121- cy . now ( ' task' , 'test_observability_log' , {
117+ cy . task ( 'test_observability_log' , {
122118 level : 'info' ,
123119 message,
124120 file,
125- } ) . then ( ( res ) => {
126- } ) . catch ( ( err ) => {
127121 } ) ;
128122} ) ;
129123
130124Cypress . Commands . add ( 'warn' , ( message , file ) => {
131- cy . now ( ' task' , 'test_observability_log' , {
125+ cy . task ( 'test_observability_log' , {
132126 level : 'warn' ,
133127 message,
134128 file,
135- } ) . then ( ( res ) => {
136- } ) . catch ( ( err ) => {
137129 } ) ;
138130} ) ;
139131
140132Cypress . Commands . add ( 'error' , ( message , file ) => {
141- cy . now ( ' task' , 'test_observability_log' , {
133+ cy . task ( 'test_observability_log' , {
142134 level : 'error' ,
143135 message,
144136 file,
145- } ) . then ( ( res ) => {
146- } ) . catch ( ( err ) => {
147137 } ) ;
148138} ) ;
149139
150140Cypress . Commands . add ( 'fatal' , ( message , file ) => {
151- cy . now ( ' task' , 'test_observability_log' , {
141+ cy . task ( 'test_observability_log' , {
152142 level : 'fatal' ,
153143 message,
154144 file,
155- } ) . then ( ( res ) => {
156- } ) . catch ( ( err ) => {
157145 } ) ;
158146} ) ;
0 commit comments