diff --git a/bin/testObservability/cypress/index.js b/bin/testObservability/cypress/index.js index 41b725de..1571165e 100644 --- a/bin/testObservability/cypress/index.js +++ b/bin/testObservability/cypress/index.js @@ -1,6 +1,7 @@ /* Event listeners + custom commands for Cypress */ /* Used to detect Gherkin steps */ +const STEP_KEYWORDS = ['given', 'when', 'then', 'and', 'but', '*']; let eventsQueue = []; let testRunStarted = false; @@ -18,20 +19,102 @@ const shouldSkipCommand = (command) => { 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)))); } -Cypress.on('log:added', (log) => { - return () => { - if (shouldSkipCommand(command)) { - return; - } +// Cypress.on('log:added', (log) => { +// return () => { +// if (shouldSkipCommand(command)) { +// return; +// } +// eventsQueue.push({ +// task: 'test_observability_step', +// data: { +// log, +// started_at: new Date().toISOString(), +// finished_at: new Date().toISOString() +// }, +// options: { log: false } +// }); +// } +// }); + +Cypress.on('log:changed', (attrs) => { + if (!Cypress.env('BROWSERSTACK_O11Y_LOGS')) return; + if (!attrs) return; + if (attrs.state !== 'passed' && attrs.state !== 'failed') return; + + if (attrs.name === 'assert') { + const assertMessage = (attrs.message || '').replace(/\*\*/g, ''); + + eventsQueue.push({ + task: 'test_observability_command', + data: { + type: 'COMMAND_START', + command: { + attributes: { + id: attrs.id, + name: 'assert', + args: [assertMessage] + }, + state: 'pending', + started_at: new Date(attrs.createdAtTimestamp).toISOString(), + location: testRunStarted ? 'test' : 'hook' + } + }, + options: { log: false } + }); + + eventsQueue.push({ + task: 'test_observability_command', + data: { + type: 'COMMAND_END', + command: { + attributes: { + id: attrs.id, + name: 'assert', + args: [assertMessage] + }, + state: attrs.state, + finished_at: new Date(attrs.updatedAtTimestamp).toISOString(), + location: testRunStarted ? 'test' : 'hook' + } + }, + options: { log: false } + }); + } + + const keyword = (attrs.name || '').trim(); + if (STEP_KEYWORDS.includes(keyword.toLowerCase())) { + const text = (attrs.message || '').replace(/\*\*/g, ''); + eventsQueue.push({ task: 'test_observability_step', data: { - log, - started_at: new Date().toISOString(), - finished_at: new Date().toISOString() + log: { + name: 'step', + chainerId: attrs.chainerId, + consoleProps: { step: { keyword, text } } + }, + started_at: new Date(attrs.createdAtTimestamp).toISOString(), + finished_at: new Date(attrs.updatedAtTimestamp).toISOString() }, options: { log: false } }); + + if (attrs.state === 'failed') { + eventsQueue.push({ + task: 'test_observability_step', + data: { + log: { + name: 'then', + type: 'child', + chainerId: attrs.chainerId, + state: attrs.state, + err: attrs.err + }, + finished_at: new Date(attrs.updatedAtTimestamp).toISOString() + }, + options: { log: false } + }); + } } });