2
2
3
3
/* Used to detect Gherkin steps */
4
4
5
+ const util = require ( 'util' ) ;
6
+
7
+ let eventsQueue = [ ] ;
8
+
5
9
const browserStackLog = ( message ) => {
6
10
if ( ! Cypress . env ( 'BROWSERSTACK_LOGS' ) ) return ;
7
11
cy . task ( 'browserstack_log' , message ) ;
@@ -13,72 +17,99 @@ const shouldSkipCommand = (command) => {
13
17
14
18
Cypress . on ( 'log:added' , ( log ) => {
15
19
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
+ } ) ;
19
29
}
20
30
} ) ;
21
31
22
32
Cypress . on ( 'command:start' , ( command ) => {
33
+
23
34
if ( ! command || ! command . attributes ) return ;
24
35
if ( shouldSkipCommand ( command ) ) {
25
36
return ;
26
37
}
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 } ) ;
39
38
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)} }`);
40
58
/* 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
+ } ) ;
47
69
} ) ;
48
70
49
71
Cypress . on ( 'command:retry' , ( command ) => {
50
72
if ( ! command || ! command . attributes ) return ;
51
73
if ( shouldSkipCommand ( command ) ) {
52
74
return ;
53
75
}
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
+ }
61
86
}
62
- }
63
- } , { log : false } ) ;
87
+ } ,
88
+ options : { log : false }
89
+ } ) ;
64
90
} ) ;
65
91
66
92
Cypress . on ( 'command:end' , ( command ) => {
67
93
if ( ! command || ! command . attributes ) return ;
68
94
if ( shouldSkipCommand ( command ) ) {
69
95
return ;
70
96
}
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
+ } ) ;
82
113
} ) ;
83
114
84
115
Cypress . Commands . overwrite ( 'log' , ( originalFn , ...args ) => {
@@ -90,57 +121,107 @@ Cypress.Commands.overwrite('log', (originalFn, ...args) => {
90
121
91
122
return [ result , logItem ? logItem . toString ( ) : '' ] . join ( ' ' ) ;
92
123
} , '' ) ;
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
+ } ) ;
97
133
originalFn ( ...args ) ;
98
134
} ) ;
99
135
100
136
Cypress . 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 }
105
145
} ) ;
106
146
} ) ;
107
147
108
148
Cypress . 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 }
113
157
} ) ;
114
158
} ) ;
115
159
116
160
Cypress . 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 }
121
169
} ) ;
122
170
} ) ;
123
171
124
172
Cypress . 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 }
129
181
} ) ;
130
182
} ) ;
131
183
132
184
Cypress . 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 }
137
193
} ) ;
138
194
} ) ;
139
195
140
196
Cypress . 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 }
145
205
} ) ;
146
206
} ) ;
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