Skip to content

Commit a0f7404

Browse files
committed
fixes pt.1
1 parent b7f0121 commit a0f7404

File tree

6 files changed

+150
-47
lines changed

6 files changed

+150
-47
lines changed

bin/testObservability/cypress/cypressEventListeners.js renamed to bin/testObservability/cypress/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/* Event listeners + custom commands for Cypress */
2+
3+
/* Used to detect Gherkin steps */
4+
Cypress.on('log:added', (log) => {
5+
cy.now('task', 'test_observability_step', {
6+
log
7+
}, {log: false}).then((res) => {
8+
}).catch((err) => {
9+
});
10+
});
11+
112
Cypress.on('command:start', (command) => {
213
if(
314
command?.attributes?.name == 'log' ||
@@ -21,7 +32,7 @@ Cypress.on('command:start', (command) => {
2132
},
2233
state: 'pending'
2334
}
24-
}).then((res) => {
35+
}, {log: false}).then((res) => {
2536
}).catch((err) => {
2637
});
2738

@@ -31,7 +42,7 @@ Cypress.on('command:start', (command) => {
3142
browser: Cypress.browser,
3243
platform: Cypress.platform,
3344
cypressVersion: Cypress.version
34-
}).then((res) => {
45+
}, {log: false}).then((res) => {
3546
}).catch((err) => {
3647
});
3748
});
@@ -57,7 +68,7 @@ Cypress.on('command:retry', (command) => {
5768
isDefaultAssertionErr: command?.error?.isDefaultAssertionErr
5869
}
5970
}
60-
}).then((res) => {
71+
}, {log: false}).then((res) => {
6172
}).catch((err) => {
6273
});
6374
});
@@ -84,7 +95,7 @@ Cypress.on('command:end', (command) => {
8495
},
8596
'state': command.state
8697
}
87-
}).then((res) => {
98+
}, {log: false}).then((res) => {
8899
}).catch((err) => {
89100
});
90101
});
@@ -101,7 +112,7 @@ Cypress.Commands.overwrite('log', (originalFn, ...args) => {
101112
cy.now('task', 'test_observability_log', {
102113
'level': 'info',
103114
message,
104-
}).then((res) => {
115+
}, {log: false}).then((res) => {
105116
}).catch((err) => {
106117
});
107118
originalFn(...args);

bin/testObservability/helper/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ exports.IPC_EVENTS = {
88
CONFIG: 'testObservability:cypressConfig',
99
SCREENSHOT: 'testObservability:cypressScreenshot',
1010
COMMAND: 'testObservability:cypressCommand',
11+
CUCUMBER: 'testObservability:cypressCucumberStep',
1112
PLATFORM_DETAILS: 'testObservability:cypressPlatformDetails'
1213
};
1314

bin/testObservability/helper/helper.js

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const getRepoInfo = require('git-repo-info');
1111
const gitconfig = require('gitconfiglocal');
1212
const { spawn, execSync } = require('child_process');
1313
const glob = require('glob');
14-
const platformDetect = require('platform-detect');
1514

1615
const pGitconfig = promisify(gitconfig);
1716

@@ -24,6 +23,7 @@ const GLOBAL_MODULE_PATH = execSync('npm root -g').toString().trim();
2423

2524
const { name, version } = require('../../../package.json');
2625

26+
const { CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS } = require('../../helpers/constants');
2727
const { consoleHolder, API_URL } = require('./constants');
2828
exports.pending_test_uploads = {
2929
count: 0
@@ -382,7 +382,7 @@ const setEnvironmentVariablesForRemoteReporter = (BS_TESTOPS_JWT, BS_TESTOPS_BUI
382382

383383
const getCypressCommandEventListener = () => {
384384
return (
385-
`require('browserstack-cypress-cli/bin/testObservability/cypress/cypressEventListeners');`
385+
`require('browserstack-cypress-cli/bin/testObservability/cypress');`
386386
);
387387
}
388388

@@ -427,7 +427,7 @@ const getBuildDetails = (bsConfig) => {
427427
if(isTestObservabilityOptionsPresent) {
428428
buildName = buildName || bsConfig["testObservabilityOptions"]["buildName"];
429429
projectName = projectName || bsConfig["testObservabilityOptions"]["projectName"];
430-
buildTags = [...buildTags, ...bsConfig["testObservabilityOptions"]["buildTag"]];
430+
if(!utils.isUndefined(bsConfig["testObservabilityOptions"]["buildTag"])) buildTags = [...buildTags, ...bsConfig["testObservabilityOptions"]["buildTag"]];
431431
buildDescription = buildDescription || bsConfig["testObservabilityOptions"]["buildDescription"];
432432
}
433433

@@ -460,8 +460,8 @@ const setBrowserstackCypressCliDependency = (bsConfig) => {
460460
}
461461

462462
const getCypressConfigFileContent = (bsConfig, cypressConfigPath) => {
463-
const cypressConfigFile = require(path.resolve(bsConfig ? bsConfig.run_settings.cypressConfigFilePath : cypressConfigPath));
464-
if(bsConfig) process.env.OBS_CRASH_REPORTING_CYPRESS_CONFIG_PATH = bsConfig.run_settings.cypressConfigFilePath;
463+
const cypressConfigFile = require(path.resolve(bsConfig ? bsConfig.run_settings.cypress_config_file : cypressConfigPath));
464+
if(bsConfig) process.env.OBS_CRASH_REPORTING_CYPRESS_CONFIG_PATH = bsConfig.run_settings.cypress_config_file;
465465
return cypressConfigFile;
466466
}
467467

@@ -560,27 +560,26 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
560560
setEventListeners();
561561
if(this.isBrowserstackInfra()) setBrowserstackCypressCliDependency(user_config);
562562
} catch(error) {
563-
if (error.response) {
564-
exports.debug(`EXCEPTION IN BUILD START EVENT : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error);
563+
if(!error.errorType) {
564+
if (error.response) {
565+
exports.debug(`EXCEPTION IN BUILD START EVENT : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error);
566+
} else {
567+
exports.debug(`EXCEPTION IN BUILD START EVENT : ${error.message || error}`, true, error);
568+
}
565569
} else {
566-
exports.debug(`EXCEPTION IN BUILD START EVENT : ${error.message || error}`, true, error);
567-
}
568-
569-
if(error.response) {
570-
const errorMessageJson = error.response.body ? JSON.parse(error.response.body.toString()) : null
571-
const errorMessage = errorMessageJson ? errorMessageJson.message : null, errorType = errorMessageJson ? errorMessageJson.errorType : null
570+
const { errorType, message } = error;
572571
switch (errorType) {
573572
case 'ERROR_INVALID_CREDENTIALS':
574-
logger.error(errorMessage);
573+
logger.error(message);
575574
break;
576575
case 'ERROR_ACCESS_DENIED':
577-
logger.info(errorMessage);
576+
logger.info(message);
578577
break;
579578
case 'ERROR_SDK_DEPRECATED':
580-
logger.error(errorMessage);
579+
logger.error(message);
581580
break;
582581
default:
583-
logger.error(errorMessage);
582+
logger.error(message);
584583
}
585584
}
586585

@@ -626,6 +625,7 @@ exports.mapTestHooks = (test) => {
626625
testHook.hookAnalyticsId = uuidv4();
627626
delete testHook.markedStatus;
628627
}
628+
testHook['test_run_id'] = testHook['test_run_id'] || test.testAnalyticsId;
629629
})
630630
});
631631
exports.mapTestHooks(test.parent);
@@ -740,17 +740,23 @@ exports.uploadEventData = async (eventData, run=0) => {
740740
}
741741
}
742742

743+
exports.isTestObservabilitySupportedCypressVersion = (cypress_config_filename) => {
744+
const extension = cypress_config_filename.split('.').pop();
745+
return CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS.includes(extension);
746+
}
747+
743748
exports.setTestObservabilityFlags = (bsConfig) => {
744749
/* testObservability */
745750
let isTestObservabilitySession = true;
746-
if(bsConfig["testObservability"]) isTestObservabilitySession = ( bsConfig["testObservability"] == true );
747-
if(process.env.BROWSERSTACK_TEST_OBSERVABILITY) isTestObservabilitySession = ( process.env.BROWSERSTACK_TEST_OBSERVABILITY == "true" );
751+
if(!utils.isUndefined(bsConfig["testObservability"])) isTestObservabilitySession = ( bsConfig["testObservability"] == true );
752+
if(!utils.isUndefined(process.env.BROWSERSTACK_TEST_OBSERVABILITY)) isTestObservabilitySession = ( process.env.BROWSERSTACK_TEST_OBSERVABILITY == "true" );
748753
if(process.argv.includes('--disable-test-observability')) isTestObservabilitySession = false;
754+
isTestObservabilitySession = isTestObservabilitySession && this.isTestObservabilitySupportedCypressVersion(bsConfig.run_settings.cypress_config_file);
749755

750756
/* browserstackAutomation */
751757
let isBrowserstackInfra = true;
752-
if(bsConfig["browserstackAutomation"]) isBrowserstackInfra = ( bsConfig["browserstackAutomation"] == true );
753-
if(process.env.BROWSERSTACK_AUTOMATION) isBrowserstackInfra = ( process.env.BROWSERSTACK_AUTOMATION == "true" );
758+
if(!utils.isUndefined(bsConfig["browserstackAutomation"])) isBrowserstackInfra = ( bsConfig["browserstackAutomation"] == true );
759+
if(!utils.isUndefined(process.env.BROWSERSTACK_AUTOMATION)) isBrowserstackInfra = ( process.env.BROWSERSTACK_AUTOMATION == "true" );
754760
if(process.argv.includes('--disable-browserstack-automation')) isBrowserstackInfra = false;
755761

756762
process.env.BROWSERSTACK_TEST_OBSERVABILITY = isTestObservabilitySession;
@@ -845,13 +851,9 @@ exports.getHookSkippedTests = (suite) => {
845851
}
846852

847853
const getPlatformName = () => {
848-
if (platformDetect.windows) return 'Windows'
849-
if (platformDetect.macos) return 'OS X'
850-
if (platformDetect.android) return 'Android'
851-
if (platformDetect.ios) return 'IOS'
852-
if (platformDetect.linux) return 'Linux'
853-
if (platformDetect.tizen) return 'Tizen'
854-
if (platformDetect.chromeos) return 'Chrome OS'
854+
if (process.platform === 'win32') return 'Windows'
855+
if (process.platform === 'darwin') return 'OS X'
856+
if (process.platform === "linux") return 'Linux'
855857
return 'Unknown'
856858
}
857859

@@ -946,8 +948,19 @@ const getLocalSessionReporter = () => {
946948
}
947949
}
948950

951+
const cleanupTestObservabilityFlags = (rawArgs) => {
952+
const newRawArgs = [];
953+
for(let idx=0; idx<rawArgs.length; idx++) {
954+
if(!['--disable-browserstack-automation', '--disable-test-observability'].includes(rawArgs[idx])) {
955+
newRawArgs.push(rawArgs[idx]);
956+
}
957+
}
958+
return newRawArgs;
959+
}
960+
949961
exports.runCypressTestsLocally = (bsConfig, args, rawArgs) => {
950962
try {
963+
rawArgs = cleanupTestObservabilityFlags(rawArgs);
951964
logger.info(`Running npx cypress run ${getReRunSpecs(rawArgs.slice(1)).join(' ')} ${getLocalSessionReporter().join(' ')}`);
952965
const cypressProcess = spawn(
953966
'npx',

bin/testObservability/plugin/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const browserstackTestObservabilityPlugin = (on, config, callbacks) => {
1717
test_observability_platform_details(platformObj) {
1818
ipc.of.browserstackTestObservability.emit(IPC_EVENTS.PLATFORM_DETAILS, platformObj);
1919
return null;
20+
},
21+
test_observability_step(log) {
22+
ipc.of.browserstackTestObservability.emit(IPC_EVENTS.CUCUMBER, log);
23+
return null;
2024
}
2125
});
2226

0 commit comments

Comments
 (0)