Skip to content

Commit 570c8f7

Browse files
authored
Merge pull request #1055 from browserstack/SDK-4928
Cucumber step definition and assertion logs detection
2 parents b90804d + b273c02 commit 570c8f7

File tree

1 file changed

+76
-8
lines changed

1 file changed

+76
-8
lines changed

bin/testObservability/cypress/index.js

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Event listeners + custom commands for Cypress */
22

33
/* Used to detect Gherkin steps */
4+
const STEP_KEYWORDS = ['given', 'when', 'then', 'and', 'but', '*'];
45

56
let eventsQueue = [];
67
let testRunStarted = false;
@@ -18,20 +19,87 @@ 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:changed', (attrs) => {
23+
if (!Cypress.env('BROWSERSTACK_O11Y_LOGS')) return;
24+
if (!attrs) return;
25+
if (!attrs.createdAtTimestamp || !attrs.updatedAtTimestamp) return;
26+
if (attrs.state !== 'passed' && attrs.state !== 'failed') return;
27+
28+
if (attrs.name === 'assert') {
29+
const assertMessage = (attrs.message || '')
30+
31+
eventsQueue.push({
32+
task: 'test_observability_command',
33+
data: {
34+
type: 'COMMAND_START',
35+
command: {
36+
attributes: {
37+
id: attrs.id,
38+
name: 'assert',
39+
args: [assertMessage]
40+
},
41+
state: 'pending',
42+
started_at: new Date(attrs.createdAtTimestamp).toISOString(),
43+
location: testRunStarted ? 'test' : 'hook'
44+
}
45+
},
46+
options: { log: false }
47+
});
48+
49+
eventsQueue.push({
50+
task: 'test_observability_command',
51+
data: {
52+
type: 'COMMAND_END',
53+
command: {
54+
attributes: {
55+
id: attrs.id,
56+
name: 'assert',
57+
args: [assertMessage]
58+
},
59+
state: attrs.state,
60+
finished_at: new Date(attrs.updatedAtTimestamp).toISOString(),
61+
location: testRunStarted ? 'test' : 'hook'
62+
}
63+
},
64+
options: { log: false }
65+
});
66+
}
67+
68+
const keyword = (attrs.displayName || attrs.name || '').trim();
69+
70+
if (STEP_KEYWORDS.includes(keyword.toLowerCase())) {
71+
const text = (attrs.message || '')
72+
2673
eventsQueue.push({
2774
task: 'test_observability_step',
2875
data: {
29-
log,
30-
started_at: new Date().toISOString(),
31-
finished_at: new Date().toISOString()
76+
log: {
77+
name: 'step',
78+
chainerId: attrs.chainerId,
79+
consoleProps: { step: { keyword, text } }
80+
},
81+
started_at: new Date(attrs.createdAtTimestamp).toISOString(),
82+
finished_at: new Date(attrs.updatedAtTimestamp).toISOString()
3283
},
3384
options: { log: false }
3485
});
86+
87+
if (attrs.state === 'failed') {
88+
eventsQueue.push({
89+
task: 'test_observability_step',
90+
data: {
91+
log: {
92+
name: 'then',
93+
type: 'child',
94+
chainerId: attrs.chainerId,
95+
state: attrs.state,
96+
err: attrs.err
97+
},
98+
finished_at: new Date(attrs.updatedAtTimestamp).toISOString()
99+
},
100+
options: { log: false }
101+
});
102+
}
35103
}
36104
});
37105

0 commit comments

Comments
 (0)