Skip to content

Commit a9d94c4

Browse files
committed
update: approach 2
1 parent 9ce92f7 commit a9d94c4

File tree

4 files changed

+186
-82
lines changed

4 files changed

+186
-82
lines changed

bin/testObservability/cypress/index.js

Lines changed: 150 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
/* Used to detect Gherkin steps */
44

5+
const util = require('util');
6+
7+
let eventsQueue = [];
8+
59
const browserStackLog = (message) => {
610
if (!Cypress.env('BROWSERSTACK_LOGS')) return;
711
cy.task('browserstack_log', message);
@@ -13,72 +17,99 @@ const shouldSkipCommand = (command) => {
1317

1418
Cypress.on('log:added', (log) => {
1519
return () => {
16-
return cy.task('test_observability_step', {
17-
log
18-
}, { log: false })
20+
eventsQueue.push({
21+
task: 'test_observability_step',
22+
data: {
23+
log,
24+
started_at: new Date().toISOString(),
25+
finished_at: new Date().toISOString()
26+
},
27+
options: { log: false }
28+
});
1929
}
2030
});
2131

2232
Cypress.on('command:start', (command) => {
33+
2334
if (!command || !command.attributes) return;
2435
if (shouldSkipCommand(command)) {
2536
return;
2637
}
27-
/* Send command details */
28-
cy.task('test_observability_command', {
29-
type: 'COMMAND_START',
30-
command: {
31-
attributes: {
32-
id: command.attributes.id,
33-
name: command.attributes.name,
34-
args: command.attributes.args
35-
},
36-
state: 'pending'
37-
}
38-
}, { log: false });
3938

39+
/* Send command details */
40+
eventsQueue.push({
41+
task: 'test_observability_command',
42+
data: {
43+
type: 'COMMAND_START',
44+
command: {
45+
attributes: {
46+
id: command.attributes.id,
47+
name: command.attributes.name,
48+
args: command.attributes.args
49+
},
50+
state: 'pending',
51+
started_at: new Date().toISOString()
52+
}
53+
},
54+
options: { log: false }
55+
});
56+
browserStackLog(`Command started: with args: ${util.format(Cypress.mocha?.getRunner()?.suite?.ctx?.currentTest?.title)}}`);
57+
// browserStackLog(`Command started: ${util.format(command.attributes)} with args: ${util.format(Cypress.mocha)}}`);
4058
/* Send platform details */
41-
cy.task('test_observability_platform_details', {
42-
testTitle: Cypress.currentRunnable?.title || '',
43-
browser: Cypress.browser,
44-
platform: Cypress.platform,
45-
cypressVersion: Cypress.version
46-
}, { log: false });
59+
eventsQueue.push({
60+
task: 'test_observability_platform_details',
61+
data: {
62+
testTitle: Cypress?.mocha?.getRunner()?.suite?.ctx?.currentTest?.title || Cypress?.mocha?.getRunner()?.suite?.ctx?._runnable?.title,
63+
browser: Cypress.browser,
64+
platform: Cypress.platform,
65+
cypressVersion: Cypress.version
66+
},
67+
options: { log: false }
68+
});
4769
});
4870

4971
Cypress.on('command:retry', (command) => {
5072
if (!command || !command.attributes) return;
5173
if (shouldSkipCommand(command)) {
5274
return;
5375
}
54-
cy.task('test_observability_command', {
55-
type: 'COMMAND_RETRY',
56-
command: {
57-
_log: command._log,
58-
error: {
59-
message: command && command.error ? command.error.message : null,
60-
isDefaultAssertionErr: command && command.error ? command.error.isDefaultAssertionErr : null
76+
eventsQueue.push({
77+
task: 'test_observability_command',
78+
data: {
79+
type: 'COMMAND_RETRY',
80+
command: {
81+
_log: command._log,
82+
error: {
83+
message: command && command.error ? command.error.message : null,
84+
isDefaultAssertionErr: command && command.error ? command.error.isDefaultAssertionErr : null
85+
}
6186
}
62-
}
63-
}, { log: false });
87+
},
88+
options: { log: false }
89+
});
6490
});
6591

6692
Cypress.on('command:end', (command) => {
6793
if (!command || !command.attributes) return;
6894
if (shouldSkipCommand(command)) {
6995
return;
7096
}
71-
cy.task('test_observability_command', {
72-
'type': 'COMMAND_END',
73-
'command': {
74-
'attributes': {
75-
'id': command.attributes.id,
76-
'name': command.attributes.name,
77-
'args': command.attributes.args
78-
},
79-
'state': command.state
80-
}
81-
}, { log: false });
97+
eventsQueue.push({
98+
task: 'test_observability_command',
99+
data: {
100+
'type': 'COMMAND_END',
101+
'command': {
102+
'attributes': {
103+
'id': command.attributes.id,
104+
'name': command.attributes.name,
105+
'args': command.attributes.args
106+
},
107+
'state': command.state,
108+
finished_at: new Date().toISOString()
109+
}
110+
},
111+
options: { log: false }
112+
});
82113
});
83114

84115
Cypress.Commands.overwrite('log', (originalFn, ...args) => {
@@ -90,57 +121,107 @@ Cypress.Commands.overwrite('log', (originalFn, ...args) => {
90121

91122
return [result, logItem ? logItem.toString() : ''].join(' ');
92123
}, '');
93-
cy.task('test_observability_log', {
94-
'level': 'info',
95-
message,
96-
}, { log: false });
124+
eventsQueue.push({
125+
task: 'test_observability_log',
126+
data: {
127+
'level': 'info',
128+
message,
129+
timestamp: new Date().toISOString()
130+
},
131+
options: { log: false }
132+
});
97133
originalFn(...args);
98134
});
99135

100136
Cypress.Commands.add('trace', (message, file) => {
101-
cy.task('test_observability_log', {
102-
level: 'trace',
103-
message,
104-
file,
137+
eventsQueue.push({
138+
task: 'test_observability_log',
139+
data: {
140+
level: 'trace',
141+
message,
142+
file,
143+
},
144+
options: { log: false }
105145
});
106146
});
107147

108148
Cypress.Commands.add('logDebug', (message, file) => {
109-
cy.task('test_observability_log', {
110-
level: 'debug',
111-
message,
112-
file,
149+
eventsQueue.push({
150+
task: 'test_observability_log',
151+
data: {
152+
level: 'debug',
153+
message,
154+
file,
155+
},
156+
options: { log: false }
113157
});
114158
});
115159

116160
Cypress.Commands.add('info', (message, file) => {
117-
cy.task('test_observability_log', {
118-
level: 'info',
119-
message,
120-
file,
161+
eventsQueue.push({
162+
task: 'test_observability_log',
163+
data: {
164+
level: 'info',
165+
message,
166+
file,
167+
},
168+
options: { log: false }
121169
});
122170
});
123171

124172
Cypress.Commands.add('warn', (message, file) => {
125-
cy.task('test_observability_log', {
126-
level: 'warn',
127-
message,
128-
file,
173+
eventsQueue.push({
174+
task: 'test_observability_log',
175+
data: {
176+
level: 'warn',
177+
message,
178+
file,
179+
},
180+
options: { log: false }
129181
});
130182
});
131183

132184
Cypress.Commands.add('error', (message, file) => {
133-
cy.task('test_observability_log', {
134-
level: 'error',
135-
message,
136-
file,
185+
eventsQueue.push({
186+
task: 'test_observability_log',
187+
data: {
188+
level: 'error',
189+
message,
190+
file,
191+
},
192+
options: { log: false }
137193
});
138194
});
139195

140196
Cypress.Commands.add('fatal', (message, file) => {
141-
cy.task('test_observability_log', {
142-
level: 'fatal',
143-
message,
144-
file,
197+
eventsQueue.push({
198+
task: 'test_observability_log',
199+
data: {
200+
level: 'fatal',
201+
message,
202+
file,
203+
},
204+
options: { log: false }
145205
});
146206
});
207+
208+
beforeEach(() => {
209+
/* browserstack internal helper hook */
210+
if (eventsQueue.length > 0) {
211+
eventsQueue.forEach(event => {
212+
cy.task(event.task, event.data, event.options);
213+
});
214+
}
215+
eventsQueue = [];
216+
});
217+
218+
afterEach(function() {
219+
/* browserstack internal helper hook */
220+
if (eventsQueue.length > 0) {
221+
eventsQueue.forEach(event => {
222+
cy.task(event.task, event.data, event.options);
223+
});
224+
}
225+
226+
eventsQueue = [];
227+
});

bin/testObservability/helper/helper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ exports.getHooksForTest = (test) => {
468468
['_beforeAll','_afterAll','_beforeEach','_afterEach'].forEach(hookType => {
469469
let hooks = test.parent[hookType] || []
470470
hooks.forEach(testHook => {
471+
this.debugOnConsole(`[getHooksForTest] Hook ${util.format(testHook)} with title ${testHook.title} and analyticsId ${testHook.hookAnalyticsId}`);
471472
if(testHook.hookAnalyticsId) hooksArr.push(testHook.hookAnalyticsId);
472473
})
473474
});
@@ -479,6 +480,7 @@ exports.mapTestHooks = (test) => {
479480
['_beforeAll','_afterAll','_beforeEach','_afterEach'].forEach(hookType => {
480481
let hooks = test.parent[hookType] || []
481482
hooks.forEach(testHook => {
483+
this.debugOnConsole(`[mapTestHooks] Hook ${util.format(testHook)} with title ${testHook.title} and analyticsId ${testHook.hookAnalyticsId}`);
482484
if(!testHook.hookAnalyticsId) {
483485
testHook.hookAnalyticsId = uuidv4();
484486
} else if(testHook.markedStatus && hookType == '_afterEach') {
@@ -523,6 +525,9 @@ exports.batchAndPostEvents = async (eventUrl, kind, data) => {
523525
}
524526

525527
exports.uploadEventData = async (eventData, run=0) => {
528+
529+
530+
this.debugOnConsole('event data is ' + util.format(eventData))
526531

527532
const requestQueueHandler = require('./requestQueueHandler');
528533
exports.debugOnConsole(`[uploadEventData] ${eventData.event_type}`);
@@ -925,7 +930,7 @@ exports.runCypressTestsLocally = (bsConfig, args, rawArgs) => {
925930
logger.info(`Running npx cypress run ${getReRunSpecs(rawArgs.slice(1)).join(' ')} ${getLocalSessionReporter().join(' ')}`);
926931
const cypressProcess = spawn(
927932
'npx',
928-
['cypress', 'run', ...getReRunSpecs(rawArgs.slice(1)), ...getLocalSessionReporter()],
933+
['cypress', 'run', '--headed', ...getReRunSpecs(rawArgs.slice(1)), ...getLocalSessionReporter()],
929934
{ stdio: 'inherit', cwd: process.cwd(), env: process.env, shell: true }
930935
);
931936
cypressProcess.on('close', async (code) => {

bin/testObservability/plugin/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ const browserstackTestObservabilityPlugin = (on, config, callbacks) => {
77

88
on('task', {
99
test_observability_log(log) {
10+
console.log('test_observability_log', log);
1011
ipc.of.browserstackTestObservability.emit(IPC_EVENTS.LOG, log);
1112
return null;
1213
},
1314
test_observability_command(commandObj) {
15+
console.log('test_observability_command', commandObj);
1416
ipc.of.browserstackTestObservability.emit(IPC_EVENTS.COMMAND, commandObj);
1517
return null;
1618
},
1719
test_observability_platform_details(platformObj) {
20+
console.log('test_observability_platform_details', platformObj);
1821
ipc.of.browserstackTestObservability.emit(IPC_EVENTS.PLATFORM_DETAILS, platformObj);
1922
return null;
2023
},
2124
test_observability_step(log) {
25+
console.log('test_observability_step', log);
2226
ipc.of.browserstackTestObservability.emit(IPC_EVENTS.CUCUMBER, log);
2327
return null;
2428
}

0 commit comments

Comments
 (0)