Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 91 additions & 8 deletions bin/testObservability/cypress/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 }
});
}
}
});

Expand Down