Skip to content

Commit 261e10c

Browse files
Merge pull request #986 from 07souravkunda/fix_cypress_v13_approach_2
Fix cypress v13 approach 2
2 parents 5b94ffb + 4021b85 commit 261e10c

File tree

8 files changed

+241
-127
lines changed

8 files changed

+241
-127
lines changed

bin/commands/runs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ module.exports = function run(args, rawArgs) {
192192
logger.debug("Completed setting the configs");
193193

194194
if(!isBrowserstackInfra) {
195+
if(process.env.BS_TESTOPS_BUILD_COMPLETED) {
196+
setEventListeners(bsConfig);
197+
}
198+
195199
return runCypressTestsLocally(bsConfig, args, rawArgs);
196200
}
197201

@@ -203,7 +207,7 @@ module.exports = function run(args, rawArgs) {
203207
setAccessibilityEventListeners(bsConfig);
204208
}
205209
if(process.env.BS_TESTOPS_BUILD_COMPLETED) {
206-
// setEventListeners(bsConfig);
210+
setEventListeners(bsConfig);
207211
}
208212
markBlockEnd('validateConfig');
209213
logger.debug("Completed configs validation");
Lines changed: 194 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,256 @@
11
/* Event listeners + custom commands for Cypress */
22

33
/* Used to detect Gherkin steps */
4+
5+
const util = require('util');
6+
7+
let eventsQueue = [];
8+
let testRunStarted = false;
9+
10+
const browserStackLog = (message) => {
11+
12+
if (!Cypress.env('BROWSERSTACK_LOGS')) return;
13+
cy.task('browserstack_log', message);
14+
}
15+
16+
const shouldSkipCommand = (command) => {
17+
if (!Cypress.env('BROWSERSTACK_O11Y_LOGS')) {
18+
return true;
19+
}
20+
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))));
21+
}
22+
423
Cypress.on('log:added', (log) => {
5-
return () => {
6-
return cy.now('task', 'test_observability_step', {
7-
log
8-
}, {log: false})
24+
return () => {
25+
if (shouldSkipCommand(command)) {
26+
return;
927
}
10-
});
11-
28+
eventsQueue.push({
29+
task: 'test_observability_step',
30+
data: {
31+
log,
32+
started_at: new Date().toISOString(),
33+
finished_at: new Date().toISOString()
34+
},
35+
options: { log: false }
36+
});
37+
}
38+
});
39+
1240
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')))) {
41+
42+
if (!command || !command.attributes) return;
43+
if (shouldSkipCommand(command)) {
1544
return;
1645
}
46+
1747
/* 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) => {
48+
eventsQueue.push({
49+
task: 'test_observability_command',
50+
data: {
51+
type: 'COMMAND_START',
52+
command: {
53+
attributes: {
54+
id: command.attributes.id,
55+
name: command.attributes.name,
56+
args: command.attributes.args
57+
},
58+
state: 'pending',
59+
started_at: new Date().toISOString(),
60+
location: testRunStarted ? 'test' : 'hook'
61+
}
62+
},
63+
options: { log: false }
3064
});
31-
3265
/* 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) => {
66+
let testTitle = '';
67+
try {
68+
const runner = Cypress.mocha.getRunner();
69+
const ctx = runner.suite.ctx;
70+
testTitle = ctx.currentTest.title || ctx._runnable.title;
71+
} catch (error) {
72+
// Silently handle if any property is undefined
73+
}
74+
75+
eventsQueue.push({
76+
task: 'test_observability_platform_details',
77+
data: {
78+
testTitle,
79+
browser: Cypress.browser,
80+
platform: Cypress.platform,
81+
cypressVersion: Cypress.version
82+
},
83+
options: { log: false }
4084
});
4185
});
4286

4387
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')))) {
88+
if (!command || !command.attributes) return;
89+
if (shouldSkipCommand(command)) {
4690
return;
4791
}
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
92+
eventsQueue.push({
93+
task: 'test_observability_command',
94+
data: {
95+
type: 'COMMAND_RETRY',
96+
command: {
97+
_log: command._log,
98+
error: {
99+
message: command && command.error ? command.error.message : null,
100+
isDefaultAssertionErr: command && command.error ? command.error.isDefaultAssertionErr : null
101+
},
102+
location: testRunStarted ? 'test' : 'hook'
55103
}
56-
}
57-
}, {log: false}).then((res) => {
58-
}).catch((err) => {
104+
},
105+
options: { log: false }
59106
});
60107
});
61108

62109
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')))) {
110+
if (!command || !command.attributes) return;
111+
if (shouldSkipCommand(command)) {
65112
return;
66113
}
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) => {
114+
eventsQueue.push({
115+
task: 'test_observability_command',
116+
data: {
117+
'type': 'COMMAND_END',
118+
'command': {
119+
'attributes': {
120+
'id': command.attributes.id,
121+
'name': command.attributes.name,
122+
'args': command.attributes.args
123+
},
124+
'state': command.state,
125+
finished_at: new Date().toISOString(),
126+
location: testRunStarted ? 'test' : 'hook'
127+
}
128+
},
129+
options: { log: false }
79130
});
80131
});
81132

82133
Cypress.Commands.overwrite('log', (originalFn, ...args) => {
83-
if(args.includes('test_observability_log') || args.includes('test_observability_command')) return;
134+
if (args.includes('test_observability_log') || args.includes('test_observability_command')) return;
84135
const message = args.reduce((result, logItem) => {
85136
if (typeof logItem === 'object') {
86137
return [result, JSON.stringify(logItem)].join(' ');
87138
}
88139

89140
return [result, logItem ? logItem.toString() : ''].join(' ');
90141
}, '');
91-
cy.now('task', 'test_observability_log', {
92-
'level': 'info',
93-
message,
94-
}, {log: false}).then((res) => {
95-
}).catch((err) => {
142+
eventsQueue.push({
143+
task: 'test_observability_log',
144+
data: {
145+
'level': 'info',
146+
message,
147+
timestamp: new Date().toISOString()
148+
},
149+
options: { log: false }
96150
});
97151
originalFn(...args);
98152
});
99153

100154
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) => {
155+
eventsQueue.push({
156+
task: 'test_observability_log',
157+
data: {
158+
level: 'trace',
159+
message,
160+
file,
161+
},
162+
options: { log: false }
107163
});
108164
});
109165

110166
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) => {
167+
eventsQueue.push({
168+
task: 'test_observability_log',
169+
data: {
170+
level: 'debug',
171+
message,
172+
file,
173+
},
174+
options: { log: false }
117175
});
118176
});
119177

120178
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) => {
179+
eventsQueue.push({
180+
task: 'test_observability_log',
181+
data: {
182+
level: 'info',
183+
message,
184+
file,
185+
},
186+
options: { log: false }
127187
});
128188
});
129189

130190
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) => {
191+
eventsQueue.push({
192+
task: 'test_observability_log',
193+
data: {
194+
level: 'warn',
195+
message,
196+
file,
197+
},
198+
options: { log: false }
137199
});
138200
});
139201

140202
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) => {
203+
eventsQueue.push({
204+
task: 'test_observability_log',
205+
data: {
206+
level: 'error',
207+
message,
208+
file,
209+
},
210+
options: { log: false }
147211
});
148212
});
149213

150214
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) => {
215+
eventsQueue.push({
216+
task: 'test_observability_log',
217+
data: {
218+
level: 'fatal',
219+
message,
220+
file,
221+
},
222+
options: { log: false }
157223
});
158224
});
225+
226+
beforeEach(() => {
227+
/* browserstack internal helper hook */
228+
229+
if (!Cypress.env('BROWSERSTACK_O11Y_LOGS')) {
230+
return;
231+
}
232+
233+
if (eventsQueue.length > 0) {
234+
eventsQueue.forEach(event => {
235+
cy.task(event.task, event.data, event.options);
236+
});
237+
}
238+
eventsQueue = [];
239+
testRunStarted = true;
240+
});
241+
242+
afterEach(function() {
243+
/* browserstack internal helper hook */
244+
if (!Cypress.env('BROWSERSTACK_O11Y_LOGS')) {
245+
return;
246+
}
247+
248+
if (eventsQueue.length > 0) {
249+
eventsQueue.forEach(event => {
250+
cy.task(event.task, event.data, event.options);
251+
});
252+
}
253+
254+
eventsQueue = [];
255+
testRunStarted = false;
256+
});

bin/testObservability/helper/cleanupQueueSync.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* Sending all the remaining queues for synchronous manner
33
*/
44

5-
const RequestQueueHandler = require('./requestQueueHandler');
5+
const requestHandler = require('./requestQueueHandler');
66

77
const shutdown = async () => {
8-
const requestHandler = new RequestQueueHandler();
98
requestHandler.queue = require(process.argv[2].trim());
109
await requestHandler.shutdown();
1110
}

0 commit comments

Comments
 (0)