22
33/* Used to detect Gherkin steps */
44
5+ const util = require ( 'util' ) ;
6+
7+ let eventsQueue = [ ] ;
8+
59const browserStackLog = ( message ) => {
610 if ( ! Cypress . env ( 'BROWSERSTACK_LOGS' ) ) return ;
711 cy . task ( 'browserstack_log' , message ) ;
@@ -13,72 +17,99 @@ const shouldSkipCommand = (command) => {
1317
1418Cypress . on ( 'log:added' , ( log ) => {
1519 return ( ) => {
16- return cy . task ( 'test_observability_step' , {
17- log
18- } , { log : false } )
20+ eventsQueue . push ( {
21+ task : 'test_observability_step' ,
22+ data : {
23+ log,
24+ started_at : new Date ( ) . toISOString ( ) ,
25+ finished_at : new Date ( ) . toISOString ( )
26+ } ,
27+ options : { log : false }
28+ } ) ;
1929 }
2030} ) ;
2131
2232Cypress . on ( 'command:start' , ( command ) => {
33+
2334 if ( ! command || ! command . attributes ) return ;
2435 if ( shouldSkipCommand ( command ) ) {
2536 return ;
2637 }
27- /* Send command details */
28- cy . task ( 'test_observability_command' , {
29- type : 'COMMAND_START' ,
30- command : {
31- attributes : {
32- id : command . attributes . id ,
33- name : command . attributes . name ,
34- args : command . attributes . args
35- } ,
36- state : 'pending'
37- }
38- } , { log : false } ) ;
3938
39+ /* Send command details */
40+ eventsQueue . push ( {
41+ task : 'test_observability_command' ,
42+ data : {
43+ type : 'COMMAND_START' ,
44+ command : {
45+ attributes : {
46+ id : command . attributes . id ,
47+ name : command . attributes . name ,
48+ args : command . attributes . args
49+ } ,
50+ state : 'pending' ,
51+ started_at : new Date ( ) . toISOString ( )
52+ }
53+ } ,
54+ options : { log : false }
55+ } ) ;
56+ browserStackLog ( `Command started: with args: ${ util . format ( Cypress . mocha ?. getRunner ( ) ?. suite ?. ctx ?. currentTest ?. title ) } }` ) ;
57+ // browserStackLog(`Command started: ${util.format(command.attributes)} with args: ${util.format(Cypress.mocha)} }`);
4058 /* Send platform details */
41- cy . task ( 'test_observability_platform_details' , {
42- testTitle : Cypress . currentRunnable ?. title || '' ,
43- browser : Cypress . browser ,
44- platform : Cypress . platform ,
45- cypressVersion : Cypress . version
46- } , { log : false } ) ;
59+ eventsQueue . push ( {
60+ task : 'test_observability_platform_details' ,
61+ data : {
62+ testTitle : Cypress ?. mocha ?. getRunner ( ) ?. suite ?. ctx ?. currentTest ?. title || Cypress ?. mocha ?. getRunner ( ) ?. suite ?. ctx ?. _runnable ?. title ,
63+ browser : Cypress . browser ,
64+ platform : Cypress . platform ,
65+ cypressVersion : Cypress . version
66+ } ,
67+ options : { log : false }
68+ } ) ;
4769} ) ;
4870
4971Cypress . on ( 'command:retry' , ( command ) => {
5072 if ( ! command || ! command . attributes ) return ;
5173 if ( shouldSkipCommand ( command ) ) {
5274 return ;
5375 }
54- cy . task ( 'test_observability_command' , {
55- type : 'COMMAND_RETRY' ,
56- command : {
57- _log : command . _log ,
58- error : {
59- message : command && command . error ? command . error . message : null ,
60- isDefaultAssertionErr : command && command . error ? command . error . isDefaultAssertionErr : null
76+ eventsQueue . push ( {
77+ task : 'test_observability_command' ,
78+ data : {
79+ type : 'COMMAND_RETRY' ,
80+ command : {
81+ _log : command . _log ,
82+ error : {
83+ message : command && command . error ? command . error . message : null ,
84+ isDefaultAssertionErr : command && command . error ? command . error . isDefaultAssertionErr : null
85+ }
6186 }
62- }
63- } , { log : false } ) ;
87+ } ,
88+ options : { log : false }
89+ } ) ;
6490} ) ;
6591
6692Cypress . on ( 'command:end' , ( command ) => {
6793 if ( ! command || ! command . attributes ) return ;
6894 if ( shouldSkipCommand ( command ) ) {
6995 return ;
7096 }
71- cy . task ( 'test_observability_command' , {
72- 'type' : 'COMMAND_END' ,
73- 'command' : {
74- 'attributes' : {
75- 'id' : command . attributes . id ,
76- 'name' : command . attributes . name ,
77- 'args' : command . attributes . args
78- } ,
79- 'state' : command . state
80- }
81- } , { log : false } ) ;
97+ eventsQueue . push ( {
98+ task : 'test_observability_command' ,
99+ data : {
100+ 'type' : 'COMMAND_END' ,
101+ 'command' : {
102+ 'attributes' : {
103+ 'id' : command . attributes . id ,
104+ 'name' : command . attributes . name ,
105+ 'args' : command . attributes . args
106+ } ,
107+ 'state' : command . state ,
108+ finished_at : new Date ( ) . toISOString ( )
109+ }
110+ } ,
111+ options : { log : false }
112+ } ) ;
82113} ) ;
83114
84115Cypress . Commands . overwrite ( 'log' , ( originalFn , ...args ) => {
@@ -90,57 +121,107 @@ Cypress.Commands.overwrite('log', (originalFn, ...args) => {
90121
91122 return [ result , logItem ? logItem . toString ( ) : '' ] . join ( ' ' ) ;
92123 } , '' ) ;
93- cy . task ( 'test_observability_log' , {
94- 'level' : 'info' ,
95- message,
96- } , { log : false } ) ;
124+ eventsQueue . push ( {
125+ task : 'test_observability_log' ,
126+ data : {
127+ 'level' : 'info' ,
128+ message,
129+ timestamp : new Date ( ) . toISOString ( )
130+ } ,
131+ options : { log : false }
132+ } ) ;
97133 originalFn ( ...args ) ;
98134} ) ;
99135
100136Cypress . Commands . add ( 'trace' , ( message , file ) => {
101- cy . task ( 'test_observability_log' , {
102- level : 'trace' ,
103- message,
104- file,
137+ eventsQueue . push ( {
138+ task : 'test_observability_log' ,
139+ data : {
140+ level : 'trace' ,
141+ message,
142+ file,
143+ } ,
144+ options : { log : false }
105145 } ) ;
106146} ) ;
107147
108148Cypress . Commands . add ( 'logDebug' , ( message , file ) => {
109- cy . task ( 'test_observability_log' , {
110- level : 'debug' ,
111- message,
112- file,
149+ eventsQueue . push ( {
150+ task : 'test_observability_log' ,
151+ data : {
152+ level : 'debug' ,
153+ message,
154+ file,
155+ } ,
156+ options : { log : false }
113157 } ) ;
114158} ) ;
115159
116160Cypress . Commands . add ( 'info' , ( message , file ) => {
117- cy . task ( 'test_observability_log' , {
118- level : 'info' ,
119- message,
120- file,
161+ eventsQueue . push ( {
162+ task : 'test_observability_log' ,
163+ data : {
164+ level : 'info' ,
165+ message,
166+ file,
167+ } ,
168+ options : { log : false }
121169 } ) ;
122170} ) ;
123171
124172Cypress . Commands . add ( 'warn' , ( message , file ) => {
125- cy . task ( 'test_observability_log' , {
126- level : 'warn' ,
127- message,
128- file,
173+ eventsQueue . push ( {
174+ task : 'test_observability_log' ,
175+ data : {
176+ level : 'warn' ,
177+ message,
178+ file,
179+ } ,
180+ options : { log : false }
129181 } ) ;
130182} ) ;
131183
132184Cypress . Commands . add ( 'error' , ( message , file ) => {
133- cy . task ( 'test_observability_log' , {
134- level : 'error' ,
135- message,
136- file,
185+ eventsQueue . push ( {
186+ task : 'test_observability_log' ,
187+ data : {
188+ level : 'error' ,
189+ message,
190+ file,
191+ } ,
192+ options : { log : false }
137193 } ) ;
138194} ) ;
139195
140196Cypress . Commands . add ( 'fatal' , ( message , file ) => {
141- cy . task ( 'test_observability_log' , {
142- level : 'fatal' ,
143- message,
144- file,
197+ eventsQueue . push ( {
198+ task : 'test_observability_log' ,
199+ data : {
200+ level : 'fatal' ,
201+ message,
202+ file,
203+ } ,
204+ options : { log : false }
145205 } ) ;
146206} ) ;
207+
208+ beforeEach ( ( ) => {
209+ /* browserstack internal helper hook */
210+ if ( eventsQueue . length > 0 ) {
211+ eventsQueue . forEach ( event => {
212+ cy . task ( event . task , event . data , event . options ) ;
213+ } ) ;
214+ }
215+ eventsQueue = [ ] ;
216+ } ) ;
217+
218+ afterEach ( function ( ) {
219+ /* browserstack internal helper hook */
220+ if ( eventsQueue . length > 0 ) {
221+ eventsQueue . forEach ( event => {
222+ cy . task ( event . task , event . data , event . options ) ;
223+ } ) ;
224+ }
225+
226+ eventsQueue = [ ] ;
227+ } ) ;
0 commit comments