Skip to content

Commit 9789b2f

Browse files
authored
Master merge
2 parents aa9674d + 8801c5f commit 9789b2f

File tree

15 files changed

+357
-181
lines changed

15 files changed

+357
-181
lines changed

bin/accessibility-automation/cypress/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const browserStackLog = (message) => {
44
if (!Cypress.env('BROWSERSTACK_LOGS')) return;
55
cy.task('browserstack_log', message);
6-
}
6+
}
77

88
const commandsToWrap = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
99
// scroll is not a default function in cypress.

bin/accessibility-automation/helper.js

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exports.checkAccessibilityPlatform = (user_config) => {
2121
}
2222
})
2323
} catch {}
24-
24+
2525
return accessibility;
2626
}
2727

@@ -113,35 +113,33 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {
113113

114114
} catch (error) {
115115
if (error.response) {
116-
logger.error("Incorrect Cred")
116+
logger.error("Incorrect Cred");
117117
logger.error(
118118
`Exception while creating test run for BrowserStack Accessibility Automation: ${
119119
error.response.status
120-
} ${error.response.statusText} ${JSON.stringify(error.response.data)}`
120+
} ${error.response.statusText} ${JSON.stringify(error.response.data)}
121+
`
121122
);
122-
} else {
123-
if(error.message === 'Invalid configuration passed.') {
124-
logger.error("Invalid configuration passed.")
125-
logger.error(
126-
`Exception while creating test run for BrowserStack Accessibility Automation: ${
127-
error.message || error.stack
128-
}`
129-
);
130-
for(const errorkey of error.errors){
131-
logger.error(errorkey.message);
132-
}
133-
134-
} else {
135-
logger.error(
136-
`Exception while creating test run for BrowserStack Accessibility Automation: ${
137-
error.message || error.stack
138-
}`
139-
);
123+
} else if (error.message === 'Invalid configuration passed.') {
124+
logger.error("Invalid configuration passed.");
125+
logger.error(
126+
`Exception while creating test run for BrowserStack Accessibility Automation: ${
127+
error.message || error.stack
128+
}`
129+
);
130+
for (const errorkey of error.errors) {
131+
logger.error(errorkey.message);
140132
}
141-
// since create accessibility session failed
142-
process.env.BROWSERSTACK_TEST_ACCESSIBILITY = 'false';
143-
user_config.run_settings.accessibility = false;
133+
} else {
134+
logger.error(
135+
`Exception while creating test run for BrowserStack Accessibility Automation: ${
136+
error.message || error.stack
137+
}`
138+
);
144139
}
140+
// since create accessibility session failed
141+
process.env.BROWSERSTACK_TEST_ACCESSIBILITY = 'false';
142+
user_config.run_settings.accessibility = false;
145143
}
146144
}
147145

@@ -216,22 +214,52 @@ const getAccessibilityCypressCommandEventListener = (extName) => {
216214

217215
exports.setAccessibilityEventListeners = (bsConfig) => {
218216
try {
219-
// Searching form command.js recursively
217+
220218
const supportFilesData = helper.getSupportFiles(bsConfig, true);
221219
if(!supportFilesData.supportFile) return;
222-
glob(process.cwd() + supportFilesData.supportFile, {}, (err, files) => {
223-
if(err) return logger.debug('EXCEPTION IN BUILD START EVENT : Unable to parse cypress support files');
220+
221+
const isPattern = glob.hasMagic(supportFilesData.supportFile);
222+
if(!isPattern) {
223+
logger.debug(`Using user defined support file: ${supportFilesData.supportFile}`);
224+
let file;
225+
try {
226+
file = process.cwd() + supportFilesData.supportFile;
227+
const defaultFileContent = fs.readFileSync(file, {encoding: 'utf-8'});
228+
let cypressCommandEventListener = getAccessibilityCypressCommandEventListener(path.extname(file));
229+
const alreadyIncludes = defaultFileContent.includes(cypressCommandEventListener);
230+
if(!alreadyIncludes) {
231+
let newFileContent = defaultFileContent +
232+
'\n' +
233+
cypressCommandEventListener +
234+
'\n';
235+
fs.writeFileSync(file, newFileContent, {encoding: 'utf-8'});
236+
supportFileContentMap[file] = supportFilesData.cleanupParams ? supportFilesData.cleanupParams : defaultFileContent;
237+
}
238+
} catch(e) {
239+
logger.debug(`Unable to modify file contents for ${file} to set event listeners with error ${e}`, true, e);
240+
}
241+
return;
242+
}
243+
244+
const globPattern = process.cwd() + supportFilesData.supportFile;
245+
glob(globPattern, {}, (err, files) => {
246+
if(err) {
247+
logger.debug('EXCEPTION IN BUILD START EVENT : Unable to parse cypress support files');
248+
return;
249+
}
250+
224251
files.forEach(file => {
225252
try {
226-
if(!file.includes('commands.js') && !file.includes('commands.ts')) {
253+
const fileName = path.basename(file);
254+
if(['e2e.js', 'e2e.ts', 'component.ts', 'component.js'].includes(fileName) && !file.includes('node_modules')) {
255+
227256
const defaultFileContent = fs.readFileSync(file, {encoding: 'utf-8'});
228-
229257
let cypressCommandEventListener = getAccessibilityCypressCommandEventListener(path.extname(file));
230258
if(!defaultFileContent.includes(cypressCommandEventListener)) {
231-
let newFileContent = defaultFileContent +
259+
let newFileContent = defaultFileContent +
232260
'\n' +
233261
cypressCommandEventListener +
234-
'\n'
262+
'\n';
235263
fs.writeFileSync(file, newFileContent, {encoding: 'utf-8'});
236264
supportFileContentMap[file] = supportFilesData.cleanupParams ? supportFilesData.cleanupParams : defaultFileContent;
237265
}

bin/commands/runs.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const { isTurboScaleSession, getTurboScaleGridDetails, patchCypressConfigFileCon
4040

4141

4242
module.exports = function run(args, rawArgs) {
43-
43+
utils.normalizeTestReportingEnvVars();
4444
markBlockStart('preBuild');
4545
// set debug mode (--cli-debug)
4646
utils.setDebugMode(args);
@@ -112,7 +112,7 @@ module.exports = function run(args, rawArgs) {
112112
// set build tag caps
113113
utils.setBuildTags(bsConfig, args);
114114

115-
// Send build start to Observability
115+
// Send build start to TEST REPORTING AND ANALYTICS
116116
if(isTestObservabilitySession) {
117117
await launchTestSession(bsConfig, bsConfigPath);
118118
utils.setO11yProcessHooks(null, bsConfig, args, null, buildReportData);
@@ -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");

bin/helpers/helper.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ const { readCypressConfigFile } = require('./readCypressConfigUtil');
1919
const { MAX_GIT_META_DATA_SIZE_IN_BYTES, GIT_META_DATA_TRUNCATED } = require('./constants')
2020
const CrashReporter = require('../testObservability/crashReporter');
2121
const HttpsProxyAgent = require('https-proxy-agent');
22+
const { TEST_REPORTING_ANALYTICS } = require("../testObservability/helper/constants");
2223

2324
exports.debug = (text, shouldReport = false, throwable = null) => {
2425
if (process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "true" || process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "1") {
25-
logger.info(`[ OBSERVABILITY ] ${text}`);
26+
logger.info(`[ ${TEST_REPORTING_ANALYTICS} ] ${text}`);
2627
}
2728
if(shouldReport) {
2829
CrashReporter.getInstance().uploadCrashReport(text, throwable ? throwable && throwable.stack : null);

bin/helpers/utils.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ exports.validateBstackJson = (bsConfigPath) => {
3333
try {
3434
logger.info(`Reading config from ${bsConfigPath}`);
3535
let bsConfig = require(bsConfigPath);
36+
bsConfig = exports.normalizeTestReportingConfig(bsConfig);
3637
resolve(bsConfig);
3738
} catch (e) {
3839
reject(
@@ -492,7 +493,6 @@ exports.setNodeVersion = (bsConfig, args) => {
492493
// specs can be passed via command line args as a string
493494
// command line args takes precedence over config
494495
exports.setUserSpecs = (bsConfig, args) => {
495-
496496
if(o11yHelpers.isBrowserstackInfra() && o11yHelpers.isTestObservabilitySession() && o11yHelpers.shouldReRunObservabilityTests()) {
497497
bsConfig.run_settings.specs = process.env.BROWSERSTACK_RERUN_TESTS;
498498
return;
@@ -1499,7 +1499,6 @@ exports.splitStringByCharButIgnoreIfWithinARange = (str, splitChar, leftLimiter,
14991499

15001500
// blindly send other passed configs with run_settings and handle at backend
15011501
exports.setOtherConfigs = (bsConfig, args) => {
1502-
15031502
if(o11yHelpers.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
15041503
bsConfig["run_settings"]["reporter"] = TEST_OBSERVABILITY_REPORTER;
15051504
return;
@@ -1519,7 +1518,15 @@ exports.setOtherConfigs = (bsConfig, args) => {
15191518
exports.readBsConfigJSON = (bsConfigPath) => {
15201519
try {
15211520
fs.accessSync(bsConfigPath, fs.constants.R_OK);
1522-
return fs.readFileSync(bsConfigPath, 'utf-8');
1521+
const configContent = fs.readFileSync(bsConfigPath, 'utf-8');
1522+
try {
1523+
const bsConfig = JSON.parse(configContent);
1524+
const normalizedBsConfig = exports.normalizeTestReportingConfig(bsConfig);
1525+
return JSON.stringify(normalizedBsConfig);
1526+
} catch (err) {
1527+
logger.error(`Error parsing JSON from ${bsConfigPath}:`, err);
1528+
return null;
1529+
}
15231530
} catch (err) {
15241531
return null;
15251532
}
@@ -1804,3 +1811,25 @@ exports.decodeJWTToken = (token) => {
18041811
return undefined;
18051812
}
18061813
}
1814+
1815+
exports.normalizeTestReportingEnvVars = () => {
1816+
if (!this.isUndefined(process.env.BROWSERSTACK_TEST_REPORTING)){
1817+
process.env.BROWSERSTACK_TEST_OBSERVABILITY = process.env.BROWSERSTACK_TEST_REPORTING;
1818+
}
1819+
1820+
if (!this.isUndefined(process.env.BROWSERSTACK_TEST_REPORTING_DEBUG)){
1821+
process.env.BROWSERSTACK_OBSERVABILITY_DEBUG = process.env.BROWSERSTACK_TEST_REPORTING_DEBUG;
1822+
}
1823+
}
1824+
1825+
exports.normalizeTestReportingConfig = (bsConfig) => {
1826+
if (!this.isUndefined(bsConfig["testReporting"])) {
1827+
bsConfig["testObservability"] = bsConfig["testReporting"];
1828+
}
1829+
1830+
if (!this.isUndefined(bsConfig["testReportingOptions"])) {
1831+
bsConfig["testObservabilityOptions"] = bsConfig["testReportingOptions"];
1832+
}
1833+
1834+
return bsConfig;
1835+
}

bin/testObservability/crashReporter/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const HttpsProxyAgent = require('https-proxy-agent');
77
const logger = require("../../helpers/logger").winstonLogger;
88
const utils = require('../../helpers/utils');
99

10-
const { API_URL, consoleHolder } = require('../helper/constants');
10+
const { API_URL, consoleHolder, TEST_REPORTING_ANALYTICS } = require('../helper/constants');
1111

1212
/* Below global methods are added here to remove cyclic dependency with helper.js, refactor later */
1313
const httpsKeepAliveAgent = new https.Agent({
@@ -19,7 +19,7 @@ const httpsKeepAliveAgent = new https.Agent({
1919

2020
const debug = (text) => {
2121
if (process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "true" || process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "1") {
22-
logger.info(`[ OBSERVABILITY ] ${text}`);
22+
logger.info(`[ ${TEST_REPORTING_ANALYTICS} ] ${text}`);
2323
}
2424
}
2525

0 commit comments

Comments
 (0)