11/* Event listeners + custom commands for Cypress */
22
33/* Used to detect Gherkin steps */
4+ const STEP_KEYWORDS = [ 'given' , 'when' , 'then' , 'and' , 'but' , '*' ] ;
45
56let eventsQueue = [ ] ;
67let testRunStarted = false ;
@@ -18,20 +19,102 @@ const shouldSkipCommand = (command) => {
1819 return command . attributes . name == 'log' || ( command . attributes . name == 'task' && ( [ 'test_observability_platform_details' , 'test_observability_step' , 'test_observability_command' , 'browserstack_log' , 'test_observability_log' ] . some ( event => command . attributes . args . includes ( event ) ) ) ) ;
1920}
2021
21- Cypress . on ( 'log:added' , ( log ) => {
22- return ( ) => {
23- if ( shouldSkipCommand ( command ) ) {
24- return ;
25- }
22+ // Cypress.on('log:added', (log) => {
23+ // return () => {
24+ // if (shouldSkipCommand(command)) {
25+ // return;
26+ // }
27+ // eventsQueue.push({
28+ // task: 'test_observability_step',
29+ // data: {
30+ // log,
31+ // started_at: new Date().toISOString(),
32+ // finished_at: new Date().toISOString()
33+ // },
34+ // options: { log: false }
35+ // });
36+ // }
37+ // });
38+
39+ Cypress . on ( 'log:changed' , ( attrs ) => {
40+ if ( ! Cypress . env ( 'BROWSERSTACK_O11Y_LOGS' ) ) return ;
41+ if ( ! attrs ) return ;
42+ if ( attrs . state !== 'passed' && attrs . state !== 'failed' ) return ;
43+
44+ if ( attrs . name === 'assert' ) {
45+ const assertMessage = ( attrs . message || '' ) . replace ( / \* \* / g, '' ) ;
46+
47+ eventsQueue . push ( {
48+ task : 'test_observability_command' ,
49+ data : {
50+ type : 'COMMAND_START' ,
51+ command : {
52+ attributes : {
53+ id : attrs . id ,
54+ name : 'assert' ,
55+ args : [ assertMessage ]
56+ } ,
57+ state : 'pending' ,
58+ started_at : new Date ( attrs . createdAtTimestamp ) . toISOString ( ) ,
59+ location : testRunStarted ? 'test' : 'hook'
60+ }
61+ } ,
62+ options : { log : false }
63+ } ) ;
64+
65+ eventsQueue . push ( {
66+ task : 'test_observability_command' ,
67+ data : {
68+ type : 'COMMAND_END' ,
69+ command : {
70+ attributes : {
71+ id : attrs . id ,
72+ name : 'assert' ,
73+ args : [ assertMessage ]
74+ } ,
75+ state : attrs . state ,
76+ finished_at : new Date ( attrs . updatedAtTimestamp ) . toISOString ( ) ,
77+ location : testRunStarted ? 'test' : 'hook'
78+ }
79+ } ,
80+ options : { log : false }
81+ } ) ;
82+ }
83+
84+ const keyword = ( attrs . name || '' ) . trim ( ) ;
85+ if ( STEP_KEYWORDS . includes ( keyword . toLowerCase ( ) ) ) {
86+ const text = ( attrs . message || '' ) . replace ( / \* \* / g, '' ) ;
87+
2688 eventsQueue . push ( {
2789 task : 'test_observability_step' ,
2890 data : {
29- log,
30- started_at : new Date ( ) . toISOString ( ) ,
31- finished_at : new Date ( ) . toISOString ( )
91+ log : {
92+ name : 'step' ,
93+ chainerId : attrs . chainerId ,
94+ consoleProps : { step : { keyword, text } }
95+ } ,
96+ started_at : new Date ( attrs . createdAtTimestamp ) . toISOString ( ) ,
97+ finished_at : new Date ( attrs . updatedAtTimestamp ) . toISOString ( )
3298 } ,
3399 options : { log : false }
34100 } ) ;
101+
102+ if ( attrs . state === 'failed' ) {
103+ eventsQueue . push ( {
104+ task : 'test_observability_step' ,
105+ data : {
106+ log : {
107+ name : 'then' ,
108+ type : 'child' ,
109+ chainerId : attrs . chainerId ,
110+ state : attrs . state ,
111+ err : attrs . err
112+ } ,
113+ finished_at : new Date ( attrs . updatedAtTimestamp ) . toISOString ( )
114+ } ,
115+ options : { log : false }
116+ } ) ;
117+ }
35118 }
36119} ) ;
37120
0 commit comments