Skip to content

Commit 506f6b0

Browse files
committed
Build unification changes
1 parent 7e703fd commit 506f6b0

File tree

11 files changed

+623
-43
lines changed

11 files changed

+623
-43
lines changed

bin/accessibility-automation/cypress/index.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -336,26 +336,33 @@ afterEach(() => {
336336
} else if (attributes.prevAttempts && attributes.prevAttempts.length > 0) {
337337
filePath = (attributes.prevAttempts[0].invocationDetails && attributes.prevAttempts[0].invocationDetails.relativeFile) || '';
338338
}
339-
const payloadToSend = {
340-
"saveResults": shouldScanTestForAccessibility,
341-
"testDetails": {
342-
"name": attributes.title,
343-
"testRunId": '5058', // variable not consumed, shouldn't matter what we send
344-
"filePath": filePath,
345-
"scopeList": [
346-
filePath,
347-
attributes.title
348-
]
349-
},
350-
"platform": {
351-
"os_name": os_data,
352-
"os_version": Cypress.env("OS_VERSION"),
353-
"browser_name": Cypress.browser.name,
354-
"browser_version": Cypress.browser.version
355-
}
356-
};
339+
340+
browserStackLog(`Printing attributes ${attributes.title}`);
357341
browserStackLog(`Saving accessibility test results`);
358-
cy.wrap(saveTestResults(win, payloadToSend), {timeout: 30000}).then(() => {
342+
browserStackLog(`Cypress env browserstack testhub uuid: ${Cypress.env("BROWSERSTACK_TESTHUB_UUID")}`);
343+
browserStackLog(`Cypress env browserstack testhub jwt: ${Cypress.env("BROWSERSTACK_TESTHUB_JWT")}`);
344+
browserStackLog(`Cypress env http port: ${Cypress.env("BROWSERSTACK_TESTHUB_API_PORT")}`);
345+
browserStackLog(`test env: ${Cypress.env("TEST_ENV")}`);
346+
browserStackLog(`reporter api: ${Cypress.env("REPORTER_API")}`);
347+
348+
let testRunUuid = null;
349+
cy.task('get_test_run_uuid', { testIdentifier: attributes.title })
350+
.then((response) => {
351+
browserStackLog(`Response from get_test_run_uuid task`);
352+
if (response && response.testRunUuid) {
353+
testRunUuid = response.testRunUuid;
354+
browserStackLog(`Fetched testRunId: ${testRunUuid} for test: ${attributes.title}`);
355+
}
356+
357+
const payloadToSend = {
358+
"thTestRunUuid": testRunUuid,
359+
"thBuildUuid": Cypress.env("BROWSERSTACK_TESTHUB_UUID"),
360+
"thJwtToken": Cypress.env("BROWSERSTACK_TESTHUB_JWT")
361+
};
362+
browserStackLog(`Payload to send: ${JSON.stringify(payloadToSend)}`);
363+
364+
return cy.wrap(saveTestResults(win, payloadToSend), {timeout: 30000});
365+
}).then(() => {
359366
browserStackLog(`Saved accessibility test results`);
360367
})
361368

bin/accessibility-automation/plugin/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,56 @@ const browserstackAccessibility = (on, config) => {
1414

1515
return null
1616
},
17+
get_test_run_uuid({ testIdentifier, retries = 15, interval = 300 } = {}) {
18+
return new Promise((resolve) => {
19+
console.log(`printing env variables take 5`);
20+
console.log(`Cypress env browserstack testhub uuid from plugin: ${config.env.BROWSERSTACK_TESTHUB_UUID}`);
21+
console.log(`Cypress env http port: ${config.env.REPORTER_API}`);
22+
console.log(`test env: ${config.env.TEST_ENV}`);
23+
console.log(`reporter api from process: ${process.env.REPORTER_API}`);
24+
console.log(`Fetching testRunUuid for testIdentifier: ${testIdentifier}`);
25+
if(!testIdentifier) return resolve(null);
26+
const port = process.env.REPORTER_API;
27+
let attempt = 0;
28+
const fetchUuid = () => {
29+
const options = {
30+
hostname: '127.0.0.1',
31+
port,
32+
path: `/test-uuid?testIdentifier=${encodeURIComponent(testIdentifier)}`,
33+
method: 'GET',
34+
timeout: 2000
35+
};
36+
const req = http.request(options, (res) => {
37+
let data = '';
38+
res.on('data', (chunk) => data += chunk);
39+
res.on('end', () => {
40+
if(res.statusCode === 200) {
41+
try {
42+
const json = JSON.parse(data || '{}');
43+
return resolve({ testRunUuid: json.testRunUuid || null });
44+
} catch(e) {
45+
return resolve(null);
46+
}
47+
} else if (res.statusCode === 404) {
48+
// Server up but endpoint not responding as expected – stop retrying.
49+
return resolve(null);
50+
} else {
51+
retryOrResolve();
52+
}
53+
});
54+
});
55+
req.on('error', () => retryOrResolve());
56+
req.on('timeout', () => { req.destroy(); retryOrResolve(); });
57+
req.end();
58+
};
59+
const retryOrResolve = () => {
60+
attempt += 1;
61+
if(attempt >= retries) return resolve(null);
62+
setTimeout(fetchUuid, interval);
63+
};
64+
fetchUuid();
65+
});
66+
}
1767
})
1868
on('before:browser:launch', (browser = {}, launchOptions) => {
1969
try {
@@ -51,6 +101,10 @@ const browserstackAccessibility = (on, config) => {
51101
config.env.ACCESSIBILITY_EXTENSION_PATH = process.env.ACCESSIBILITY_EXTENSION_PATH
52102
config.env.OS_VERSION = process.env.OS_VERSION
53103
config.env.OS = process.env.OS
104+
config.env.BROWSERSTACK_TESTHUB_UUID = process.env.BROWSERSTACK_TESTHUB_UUID
105+
config.env.BROWSERSTACK_TESTHUB_JWT = process.env.BROWSERSTACK_TESTHUB_JWT
106+
config.env.BROWSERSTACK_TESTHUB_API_PORT = process.env.BROWSERSTACK_TESTHUB_API_PORT
107+
config.env.REPORTER_API = process.env.REPORTER_API
54108

55109
config.env.IS_ACCESSIBILITY_EXTENSION_LOADED = browser_validation.toString()
56110

bin/commands/runs.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const {
3737
supportFileCleanup
3838
} = require('../accessibility-automation/helper');
3939
const { isTurboScaleSession, getTurboScaleGridDetails, patchCypressConfigFileContent, atsFileCleanup } = require('../helpers/atsHelper');
40-
40+
const { shouldProcessEventForTesthub, checkIfAccessibilityIsSupported, findAvailablePort } = require('../testhub/utils');
41+
const TestHubHandler = require('../testhub/testhubHandler');
4142

4243
module.exports = function run(args, rawArgs) {
4344
utils.normalizeTestReportingEnvVars();
@@ -112,9 +113,15 @@ module.exports = function run(args, rawArgs) {
112113
// set build tag caps
113114
utils.setBuildTags(bsConfig, args);
114115

116+
checkIfAccessibilityIsSupported(bsConfig, isAccessibilitySession);
117+
118+
const preferredPort = 5348;
119+
const port = await findAvailablePort(preferredPort);
120+
process.env.REPORTER_API = port
121+
115122
// Send build start to TEST REPORTING AND ANALYTICS
116-
if(isTestObservabilitySession) {
117-
await launchTestSession(bsConfig, bsConfigPath);
123+
if(shouldProcessEventForTesthub()) {
124+
await TestHubHandler.launchBuild(bsConfig, bsConfigPath);
118125
utils.setO11yProcessHooks(null, bsConfig, args, null, buildReportData);
119126
}
120127

@@ -149,10 +156,6 @@ module.exports = function run(args, rawArgs) {
149156
// add cypress dependency if missing
150157
utils.setCypressNpmDependency(bsConfig);
151158

152-
if (isAccessibilitySession && isBrowserstackInfra) {
153-
await createAccessibilityTestRun(bsConfig);
154-
}
155-
156159
if (turboScaleSession) {
157160
const gridDetails = await getTurboScaleGridDetails(bsConfig, args, rawArgs);
158161

bin/helpers/helper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ exports.getGitMetaData = () => {
182182
}
183183
})
184184
}
185+
186+
exports.getHostInfo = () => {
187+
return {
188+
hostname: os.hostname(),
189+
platform: os.platform(),
190+
type: os.type(),
191+
version: os.version(),
192+
arch: os.arch()
193+
}
194+
}
195+
185196
exports.getCiInfo = () => {
186197
var env = process.env;
187198
// Jenkins

bin/helpers/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const usageReporting = require("./usageReporting"),
2727
{ OBSERVABILITY_ENV_VARS, TEST_OBSERVABILITY_REPORTER } = require('../testObservability/helper/constants');
2828

2929
const { default: axios } = require("axios");
30+
const { shouldProcessEventForTesthub } = require("../testhub/utils");
3031

3132
exports.validateBstackJson = (bsConfigPath) => {
3233
return new Promise(function (resolve, reject) {
@@ -1499,7 +1500,7 @@ exports.splitStringByCharButIgnoreIfWithinARange = (str, splitChar, leftLimiter,
14991500

15001501
// blindly send other passed configs with run_settings and handle at backend
15011502
exports.setOtherConfigs = (bsConfig, args) => {
1502-
if(o11yHelpers.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
1503+
if(shouldProcessEventForTesthub()) {
15031504
bsConfig["run_settings"]["reporter"] = TEST_OBSERVABILITY_REPORTER;
15041505
return;
15051506
}

bin/testObservability/helper/helper.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ exports.printBuildLink = async (shouldStopSession, exitCode = null) => {
116116
if(exitCode) process.exit(exitCode);
117117
}
118118

119-
const nodeRequest = (type, url, data, config) => {
119+
exports.nodeRequest = (type, url, data, config) => {
120120
const requestQueueHandler = require('./requestQueueHandler');
121121
return new Promise(async (resolve, reject) => {
122122
const options = {
@@ -269,7 +269,7 @@ exports.getPackageVersion = (package_, bsConfig = null) => {
269269
return packageVersion;
270270
}
271271

272-
const setEnvironmentVariablesForRemoteReporter = (BS_TESTOPS_JWT, BS_TESTOPS_BUILD_HASHED_ID, BS_TESTOPS_ALLOW_SCREENSHOTS, OBSERVABILITY_LAUNCH_SDK_VERSION) => {
272+
exports.setEnvironmentVariablesForRemoteReporter = (BS_TESTOPS_JWT, BS_TESTOPS_BUILD_HASHED_ID, BS_TESTOPS_ALLOW_SCREENSHOTS, OBSERVABILITY_LAUNCH_SDK_VERSION) => {
273273
process.env.BS_TESTOPS_JWT = BS_TESTOPS_JWT;
274274
process.env.BS_TESTOPS_BUILD_HASHED_ID = BS_TESTOPS_BUILD_HASHED_ID;
275275
process.env.BS_TESTOPS_ALLOW_SCREENSHOTS = BS_TESTOPS_ALLOW_SCREENSHOTS;
@@ -414,10 +414,10 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
414414
}
415415
};
416416

417-
const response = await nodeRequest('POST','api/v1/builds',data,config);
417+
const response = await exports.nodeRequest('POST','api/v1/builds',data,config);
418418
exports.debug('Build creation successfull!');
419419
process.env.BS_TESTOPS_BUILD_COMPLETED = true;
420-
setEnvironmentVariablesForRemoteReporter(response.data.jwt, response.data.build_hashed_id, response.data.allow_screenshots, data.observability_version.sdkVersion);
420+
exports.setEnvironmentVariablesForRemoteReporter(response.data.jwt, response.data.build_hashed_id, response.data.allow_screenshots, data.observability_version.sdkVersion);
421421
if(this.isBrowserstackInfra() && (user_config.run_settings.auto_import_dev_dependencies != true)) helper.setBrowserstackCypressCliDependency(user_config);
422422
} catch(error) {
423423
if(!error.errorType) {
@@ -444,7 +444,7 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
444444
}
445445

446446
process.env.BS_TESTOPS_BUILD_COMPLETED = false;
447-
setEnvironmentVariablesForRemoteReporter(null, null, null);
447+
exports.setEnvironmentVariablesForRemoteReporter(null, null, null);
448448
}
449449
}
450450
}
@@ -503,7 +503,7 @@ exports.batchAndPostEvents = async (eventUrl, kind, data) => {
503503
try {
504504
const eventsUuids = data.map(eventData => `${eventData.event_type}:${eventData.test_run ? eventData.test_run.uuid : (eventData.hook_run ? eventData.hook_run.uuid : null)}`).join(', ');
505505
exports.debugOnConsole(`[Request Batch Send] for events:uuids ${eventsUuids}`);
506-
const response = await nodeRequest('POST',eventUrl,data,config);
506+
const response = await exports.nodeRequest('POST',eventUrl,data,config);
507507
exports.debugOnConsole(`[Request Batch Response] for events:uuids ${eventsUuids}`);
508508
if(response.data.error) {
509509
throw({message: response.data.error});
@@ -570,7 +570,7 @@ exports.uploadEventData = async (eventData, run=0) => {
570570
try {
571571
const eventsUuids = data.map(eventData => `${eventData.event_type}:${eventData.test_run ? eventData.test_run.uuid : (eventData.hook_run ? eventData.hook_run.uuid : null)}`).join(', ');
572572
exports.debugOnConsole(`[Request Send] for events:uuids ${eventsUuids}`);
573-
const response = await nodeRequest('POST',event_api_url,data,config);
573+
const response = await exports.nodeRequest('POST',event_api_url,data,config);
574574
exports.debugOnConsole(`[Request Repsonse] ${util.format(response.data)} for events:uuids ${eventsUuids}`)
575575
if(response.data.error) {
576576
throw({message: response.data.error});
@@ -681,7 +681,7 @@ exports.stopBuildUpstream = async () => {
681681
};
682682

683683
try {
684-
const response = await nodeRequest('PUT',`api/v1/builds/${process.env.BS_TESTOPS_BUILD_HASHED_ID}/stop`,data,config);
684+
const response = await exports.nodeRequest('PUT',`api/v1/builds/${process.env.BS_TESTOPS_BUILD_HASHED_ID}/stop`,data,config);
685685
if(response.data && response.data.error) {
686686
throw({message: response.data.error});
687687
} else {

0 commit comments

Comments
 (0)