1+ /* Event listeners + custom commands for Cypress */
2+
3+ /* Used to detect Gherkin steps */
4+ Cypress . on ( 'log:added' , ( log ) => {
5+ return ( ) => {
6+ return cy . now ( 'task' , 'test_observability_step' , {
7+ log
8+ } , { log : false } )
9+ }
10+ } ) ;
11+
12+ Cypress . 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' ) ) ) ) {
15+ return ;
16+ }
17+ /* Send command details */
18+ cy . now ( 'task' , 'test_observability_command' , {
19+ type : 'COMMAND_START' ,
20+ command : {
21+ attributes : {
22+ id : command . attributes . id ,
23+ name : command . attributes . name ,
24+ args : command . attributes . args
25+ } ,
26+ state : 'pending'
27+ }
28+ } , { log : false } ) . then ( ( res ) => {
29+ } ) . catch ( ( err ) => {
30+ } ) ;
31+
32+ /* Send platform details */
33+ cy . now ( 'task' , 'test_observability_platform_details' , {
34+ testTitle : Cypress . currentTest . title ,
35+ browser : Cypress . browser ,
36+ platform : Cypress . platform ,
37+ cypressVersion : Cypress . version
38+ } , { log : false } ) . then ( ( res ) => {
39+ } ) . catch ( ( err ) => {
40+ } ) ;
41+ } ) ;
42+
43+ Cypress . 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' ) ) ) ) {
46+ return ;
47+ }
48+ cy . now ( 'task' , 'test_observability_command' , {
49+ type : 'COMMAND_RETRY' ,
50+ command : {
51+ _log : command . _log ,
52+ error : {
53+ message : command && command . error ? command . error . message : null ,
54+ isDefaultAssertionErr : command && command . error ? command . error . isDefaultAssertionErr : null
55+ }
56+ }
57+ } , { log : false } ) . then ( ( res ) => {
58+ } ) . catch ( ( err ) => {
59+ } ) ;
60+ } ) ;
61+
62+ Cypress . 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' ) ) ) ) {
65+ return ;
66+ }
67+ cy . now ( 'task' , 'test_observability_command' , {
68+ 'type' : 'COMMAND_END' ,
69+ 'command' : {
70+ 'attributes' : {
71+ 'id' : command . attributes . id ,
72+ 'name' : command . attributes . name ,
73+ 'args' : command . attributes . args
74+ } ,
75+ 'state' : command . state
76+ }
77+ } , { log : false } ) . then ( ( res ) => {
78+ } ) . catch ( ( err ) => {
79+ } ) ;
80+ } ) ;
81+
82+ Cypress . Commands . overwrite ( 'log' , ( originalFn , ...args ) => {
83+ if ( args . includes ( 'test_observability_log' ) || args . includes ( 'test_observability_command' ) ) return ;
84+ const message = args . reduce ( ( result , logItem ) => {
85+ if ( typeof logItem === 'object' ) {
86+ return [ result , JSON . stringify ( logItem ) ] . join ( ' ' ) ;
87+ }
88+
89+ return [ result , logItem ? logItem . toString ( ) : '' ] . join ( ' ' ) ;
90+ } , '' ) ;
91+ cy . now ( 'task' , 'test_observability_log' , {
92+ 'level' : 'info' ,
93+ message,
94+ } , { log : false } ) . then ( ( res ) => {
95+ } ) . catch ( ( err ) => {
96+ } ) ;
97+ originalFn ( ...args ) ;
98+ } ) ;
99+
100+ Cypress . Commands . add ( 'trace' , ( message , file ) => {
101+ cy . now ( 'task' , 'test_observability_log' , {
102+ level : 'trace' ,
103+ message,
104+ file,
105+ } ) . then ( ( res ) => {
106+ } ) . catch ( ( err ) => {
107+ } ) ;
108+ } ) ;
109+
110+ Cypress . Commands . add ( 'logDebug' , ( message , file ) => {
111+ cy . now ( 'task' , 'test_observability_log' , {
112+ level : 'debug' ,
113+ message,
114+ file,
115+ } ) . then ( ( res ) => {
116+ } ) . catch ( ( err ) => {
117+ } ) ;
118+ } ) ;
119+
120+ Cypress . Commands . add ( 'info' , ( message , file ) => {
121+ cy . now ( 'task' , 'test_observability_log' , {
122+ level : 'info' ,
123+ message,
124+ file,
125+ } ) . then ( ( res ) => {
126+ } ) . catch ( ( err ) => {
127+ } ) ;
128+ } ) ;
129+
130+ Cypress . Commands . add ( 'warn' , ( message , file ) => {
131+ cy . now ( 'task' , 'test_observability_log' , {
132+ level : 'warn' ,
133+ message,
134+ file,
135+ } ) . then ( ( res ) => {
136+ } ) . catch ( ( err ) => {
137+ } ) ;
138+ } ) ;
139+
140+ Cypress . Commands . add ( 'error' , ( message , file ) => {
141+ cy . now ( 'task' , 'test_observability_log' , {
142+ level : 'error' ,
143+ message,
144+ file,
145+ } ) . then ( ( res ) => {
146+ } ) . catch ( ( err ) => {
147+ } ) ;
148+ } ) ;
149+
150+ Cypress . Commands . add ( 'fatal' , ( message , file ) => {
151+ cy . now ( 'task' , 'test_observability_log' , {
152+ level : 'fatal' ,
153+ message,
154+ file,
155+ } ) . then ( ( res ) => {
156+ } ) . catch ( ( err ) => {
157+ } ) ;
158+ } ) ;
0 commit comments