Skip to content

Commit 80fdb1f

Browse files
committed
fixes pt.2
1 parent a0f7404 commit 80fdb1f

File tree

4 files changed

+83
-61
lines changed

4 files changed

+83
-61
lines changed

bin/helpers/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const usageReporting = require("./usageReporting"),
2222
pkg = require('../../package.json'),
2323
transports = require('./logger').transports,
2424
{ findGitConfig, printBuildLink, isTestObservabilitySession, isBrowserstackInfra, shouldReRunObservabilityTests } = require('../testObservability/helper/helper'),
25-
{ OBSERVABILITY_ENV_VARS } = require('../testObservability/helper/constants');
25+
{ OBSERVABILITY_ENV_VARS, TEST_OBSERVABILITY_REPORTER } = require('../testObservability/helper/constants');
2626

2727
const request = require('request');
2828

@@ -1223,7 +1223,7 @@ exports.setConfig = (bsConfig, args) => {
12231223
// blindly send other passed configs with run_settings and handle at backend
12241224
exports.setOtherConfigs = (bsConfig, args) => {
12251225
if(isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
1226-
bsConfig["run_settings"]["reporter"] = "browserstack-cypress-cli/bin/testObservability/reporter";
1226+
bsConfig["run_settings"]["reporter"] = TEST_OBSERVABILITY_REPORTER;
12271227
return;
12281228
}
12291229

bin/testObservability/helper/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ exports.OBSERVABILITY_ENV_VARS = [
2626
"OBS_CRASH_REPORTING_BS_CONFIG_PATH",
2727
"OBS_CRASH_REPORTING_CYPRESS_CONFIG_PATH"
2828
];
29+
30+
exports.TEST_OBSERVABILITY_REPORTER = 'browserstack-cypress-cli/bin/testObservability/reporter';

bin/testObservability/helper/helper.js

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const GLOBAL_MODULE_PATH = execSync('npm root -g').toString().trim();
2424
const { name, version } = require('../../../package.json');
2525

2626
const { CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS } = require('../../helpers/constants');
27-
const { consoleHolder, API_URL } = require('./constants');
27+
const { consoleHolder, API_URL, TEST_OBSERVABILITY_REPORTER } = require('./constants');
2828
exports.pending_test_uploads = {
2929
count: 0
3030
};
@@ -777,56 +777,48 @@ exports.shouldReRunObservabilityTests = () => {
777777
return (process.env.BROWSERSTACK_RERUN_TESTS && process.env.BROWSERSTACK_RERUN_TESTS !== "null") ? true : false
778778
}
779779

780-
exports.stopBuildUpstream = async (buildStartWaitRun = 0, testUploadWaitRun = 0, forceStop = false) => {
781-
if(process.env.BS_TESTOPS_BUILD_COMPLETED !== "false" && !forceStop && exports.pending_test_uploads.count && testUploadWaitRun < 5) {
782-
exports.debug(`stopBuildUpstream event : retry count ${testUploadWaitRun+1} due to pending event uploads ${exports.pending_test_uploads.count}`);
783-
setTimeout(function(){ exports.stopBuildUpstream(buildStartWaitRun, testUploadWaitRun+1,false) }, 5000);
784-
} else {
785-
if (process.env.BS_TESTOPS_BUILD_COMPLETED === "true") {
786-
if(process.env.BS_TESTOPS_JWT == "null" || process.env.BS_TESTOPS_BUILD_HASHED_ID == "null") {
787-
exports.debug('EXCEPTION IN stopBuildUpstream REQUEST TO TEST OBSERVABILITY : Missing authentication token');
788-
return {
789-
status: 'error',
790-
message: 'Token/buildID is undefined, build creation might have failed'
791-
};
792-
} else {
793-
const data = {
794-
'stop_time': (new Date()).toISOString()
795-
};
796-
const config = {
797-
headers: {
798-
'Authorization': `Bearer ${process.env.BS_TESTOPS_JWT}`,
799-
'Content-Type': 'application/json',
800-
'X-BSTACK-TESTOPS': 'true'
801-
}
802-
};
803-
804-
try {
805-
const response = await nodeRequest('PUT',`api/v1/builds/${process.env.BS_TESTOPS_BUILD_HASHED_ID}/stop`,data,config);
806-
if(response.data && response.data.error) {
807-
throw({message: response.data.error});
808-
} else {
809-
exports.debug(`stopBuildUpstream buildStartWaitRun[${buildStartWaitRun}] testUploadWaitRun[${testUploadWaitRun}] event successfull!`)
810-
return {
811-
status: 'success',
812-
message: ''
813-
};
814-
}
815-
} catch(error) {
816-
if (error.response) {
817-
exports.debug(`EXCEPTION IN stopBuildUpstream REQUEST TO TEST OBSERVABILITY : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error);
818-
} else {
819-
exports.debug(`EXCEPTION IN stopBuildUpstream REQUEST TO TEST OBSERVABILITY : ${error.message || error}`, true, error);
820-
}
780+
exports.stopBuildUpstream = async () => {
781+
if (process.env.BS_TESTOPS_BUILD_COMPLETED === "true") {
782+
if(process.env.BS_TESTOPS_JWT == "null" || process.env.BS_TESTOPS_BUILD_HASHED_ID == "null") {
783+
exports.debug('EXCEPTION IN stopBuildUpstream REQUEST TO TEST OBSERVABILITY : Missing authentication token');
784+
return {
785+
status: 'error',
786+
message: 'Token/buildID is undefined, build creation might have failed'
787+
};
788+
} else {
789+
const data = {
790+
'stop_time': (new Date()).toISOString()
791+
};
792+
const config = {
793+
headers: {
794+
'Authorization': `Bearer ${process.env.BS_TESTOPS_JWT}`,
795+
'Content-Type': 'application/json',
796+
'X-BSTACK-TESTOPS': 'true'
797+
}
798+
};
799+
800+
try {
801+
const response = await nodeRequest('PUT',`api/v1/builds/${process.env.BS_TESTOPS_BUILD_HASHED_ID}/stop`,data,config);
802+
if(response.data && response.data.error) {
803+
throw({message: response.data.error});
804+
} else {
805+
exports.debug(`stopBuildUpstream event successfull!`)
821806
return {
822-
status: 'error',
823-
message: error.message || error.response ? `${error.response.status}:${error.response.statusText}` : error
807+
status: 'success',
808+
message: ''
824809
};
825810
}
811+
} catch(error) {
812+
if (error.response) {
813+
exports.debug(`EXCEPTION IN stopBuildUpstream REQUEST TO TEST OBSERVABILITY : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error);
814+
} else {
815+
exports.debug(`EXCEPTION IN stopBuildUpstream REQUEST TO TEST OBSERVABILITY : ${error.message || error}`, true, error);
816+
}
817+
return {
818+
status: 'error',
819+
message: error.message || error.response ? `${error.response.status}:${error.response.statusText}` : error
820+
};
826821
}
827-
} else if(process.env.BS_TESTOPS_BUILD_COMPLETED !== "false") {
828-
/* forceStop = true since we won't wait for pendingUploads trigerred post stop flow */
829-
setTimeout(function(){ exports.stopBuildUpstream(buildStartWaitRun+1,testUploadWaitRun,true) }, 1000);
830822
}
831823
}
832824
}
@@ -942,7 +934,7 @@ const getReRunSpecs = (rawArgs) => {
942934

943935
const getLocalSessionReporter = () => {
944936
if(this.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
945-
return ['--reporter', 'browserstack-cypress-cli/bin/testObservability/reporter'];
937+
return ['--reporter', TEST_OBSERVABILITY_REPORTER];
946938
} else {
947939
return [];
948940
}

bin/testObservability/reporter/index.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class MyReporter {
178178
} catch(err) {
179179
debug(`Exception in populating test data for hook skipped test with error : ${err}`, true, err);
180180
}
181-
181+
182182
await requestQueueHandler.shutdown();
183183
});
184184
}
@@ -224,6 +224,30 @@ class MyReporter {
224224
}
225225
}
226226

227+
uploadTestSteps = async (test_run_uuid = null, hook_run_uuid = null, shouldClearCurrentSteps = true, cypressSteps = null) => {
228+
const currentTestSteps = cypressSteps ? cypressSteps : JSON.parse(JSON.stringify(this.currentTestSteps));
229+
/* TODO - Send as test logs */
230+
const allStepsAsLogs = [];
231+
currentTestSteps.forEach(step => {
232+
const currentStepAsLog = {
233+
test_run_uuid,
234+
hook_run_uuid,
235+
timestamp: step.started_at,
236+
level: step.result,
237+
message: step.text,
238+
kind: 'TEST_LOG',
239+
http_response: {}
240+
};
241+
allStepsAsLogs.push(currentStepAsLog);
242+
});
243+
if(currentTestSteps.length) consoleHolder.log(`\n SENDING BELOW STEPS FOR Test = ${test_run_uuid} OR Hook = ${hook_run_uuid} \n ${JSON.stringify(currentTestSteps, 0, 3)} \n`);
244+
await uploadEventData({
245+
event_type: 'LogCreated',
246+
logs: allStepsAsLogs
247+
});
248+
if(shouldClearCurrentSteps) this.currentTestSteps = [];
249+
}
250+
227251
sendTestRunEvent = async (test, err = undefined, customFinished=false, eventType = "TestRunFinished") => {
228252
try {
229253
if(test.body && test.body.match(/browserstack internal helper hook/)) return;
@@ -258,16 +282,6 @@ class MyReporter {
258282

259283
const { fileSeparator, fileSeparatorRegex } = getFileSeparatorData();
260284

261-
if(eventType == 'TestRunStarted') {
262-
this.currentTestSteps = [];
263-
this.currentTestCucumberSteps = [];
264-
}
265-
266-
if(eventType.match(/HookRun/)) {
267-
consoleHolder.log(`\n >>> HOOKOBJ : ${test.title} : ${test.state} : ${test.isPassed()} ${test.isFailed()} ${test.isPending()} \n`);
268-
} else if(eventType.match(/TestRun/)) {
269-
consoleHolder.log(`\n SENDTESTRUNEVENT : ${eventType} ${test.testAnalyticsId} || ${test.hookAnalyticsId} ${test.title} ${test.state} \n`);
270-
}
271285
let testData = {
272286
'framework': 'Cypress',
273287
'uuid': (eventType.includes("Test") ? test.testAnalyticsId : test.hookAnalyticsId) || uuidv4(),
@@ -295,6 +309,8 @@ class MyReporter {
295309
}
296310
};
297311

312+
this.currentTestCucumberSteps = [];
313+
298314
const { os, os_version } = await getOSDetailsFromSystem(process.env.observability_product);
299315
if(process.env.observability_integration) {
300316
testData = {...testData, integrations: {
@@ -374,13 +390,25 @@ class MyReporter {
374390
}
375391

376392
if(eventType == 'HookRunFinished' && testData['hook_type'] == 'BEFORE_ALL') {
393+
uploadData.cypressSteps = JSON.parse(JSON.stringify(this.currentTestSteps));
377394
this.beforeHooks.push(uploadData);
395+
this.currentTestSteps = [];
378396
} else {
379397
await uploadEventData(uploadData);
398+
399+
if(eventType == 'HookRunFinished') {
400+
await this.uploadTestSteps(null, testData['uuid']);
401+
} else if(eventType == 'TestRunFinished') {
402+
await this.uploadTestSteps(testData['uuid']);
403+
}
404+
380405
if(eventType.match(/TestRun/)) {
381406
this.beforeHooks.forEach(async(hookUploadObj) => {
407+
const currentTestSteps = hookUploadObj.cypressSteps;
408+
delete hookUploadObj.cypressSteps;
382409
hookUploadObj['hook_run']['test_run_id'] = test.testAnalyticsId;
383410
await uploadEventData(hookUploadObj);
411+
await this.uploadTestSteps(null, hookUploadObj['hook_run']['uuid'], false, currentTestSteps);
384412
});
385413
this.beforeHooks = [];
386414
}

0 commit comments

Comments
 (0)