Skip to content

Commit 79b1832

Browse files
committed
review fixes
1 parent a1bc49b commit 79b1832

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

bin/commands/runs.js

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

4343
module.exports = function run(args, rawArgs) {
@@ -113,7 +113,7 @@ module.exports = function run(args, rawArgs) {
113113
// set build tag caps
114114
utils.setBuildTags(bsConfig, args);
115115

116-
checkIfAccessibilityIsSupported(bsConfig, isAccessibilitySession);
116+
checkAndSetAccessibility(bsConfig, isAccessibilitySession);
117117

118118
const preferredPort = 5348;
119119
const port = await findAvailablePort(preferredPort);

bin/testObservability/reporter/index.js

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class MyReporter {
7272
this._testEnv = getTestEnv();
7373
this._paths = new PathHelper({ cwd: process.cwd() }, this._testEnv.location_prefix);
7474
this.currentTestSteps = [];
75+
this.httpServer = null;
7576
this.currentTestCucumberSteps = [];
7677
this.hooksStarted = {};
7778
this.beforeHooks = [];
@@ -219,48 +220,62 @@ class MyReporter {
219220
}
220221

221222
async startHttpServer() {
222-
this.httpServer = http.createServer(async(req, res) => {
223-
// Set CORS headers
224-
res.setHeader('Access-Control-Allow-Origin', '*');
225-
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
226-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
227-
228-
if (req.method === 'OPTIONS') {
229-
res.writeHead(200);
230-
res.end();
231-
return;
232-
}
233-
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
234-
const pathname = parsedUrl.pathname;
235-
const query = parsedUrl.searchParams;
236-
237-
if (pathname === '/test-uuid' && req.method === 'GET') {
238-
const testIdentifier = query.get('testIdentifier');
239-
240-
if (!testIdentifier) {
241-
res.writeHead(400, { 'Content-Type': 'application/json' });
242-
res.end(JSON.stringify({
243-
error: 'testIdentifier parameter is required',
244-
testRunUuid: null
245-
}));
246-
return;
247-
}
248-
const testRunUuid = this.getTestId(testIdentifier);
223+
if(this.httpServer !== null) return;
224+
225+
try {
226+
this.httpServer = http.createServer(async(req, res) => {
227+
try {
228+
// Set CORS headers
229+
res.setHeader('Access-Control-Allow-Origin', '*');
230+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
231+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
232+
233+
if (req.method === 'OPTIONS') {
234+
res.writeHead(200);
235+
res.end();
236+
return;
237+
}
238+
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
239+
const pathname = parsedUrl.pathname;
240+
const query = parsedUrl.searchParams;
241+
242+
if (pathname === '/test-uuid' && req.method === 'GET') {
243+
const testIdentifier = query.get('testIdentifier');
244+
245+
if (!testIdentifier) {
246+
res.writeHead(400, { 'Content-Type': 'application/json' });
247+
res.end(JSON.stringify({
248+
error: 'testIdentifier parameter is required',
249+
testRunUuid: null
250+
}));
251+
return;
252+
}
253+
const testRunUuid = this.getTestId(testIdentifier);
249254

250255

251-
res.writeHead(200, { 'Content-Type': 'application/json' });
252-
res.end(JSON.stringify({ testRunUuid: testRunUuid }));
253-
} else {
254-
res.writeHead(404, { 'Content-Type': 'text/plain' });
255-
res.end('Not Found');
256-
}
257-
});
256+
res.writeHead(200, { 'Content-Type': 'application/json' });
257+
res.end(JSON.stringify({ testRunUuid: testRunUuid }));
258+
} else {
259+
res.writeHead(404, { 'Content-Type': 'text/plain' });
260+
res.end('Not Found');
261+
}
262+
} catch (error) {
263+
debugOnConsole(`Exception in handling HTTP request : ${error}`);
264+
debug(`Exception in handling HTTP request : ${error}`, true, error);
265+
res.writeHead(500, { 'Content-Type': 'application/json' });
266+
res.end(JSON.stringify({ testRunUuid: null }));
267+
}
268+
});
258269

259-
const port = process.env.REPORTER_API_PORT_NO;
270+
const port = process.env.REPORTER_API_PORT_NO;
260271

261-
this.httpServer.listen(port, '127.0.0.1', async () => {
262-
console.log(`Reporter HTTP server listening on port ${port}`);
263-
});
272+
this.httpServer.listen(port, '127.0.0.1', async () => {
273+
console.log(`Reporter HTTP server listening on port ${port}`);
274+
});
275+
} catch (error) {
276+
debugOnConsole(`Exception in starting reporter server : ${error}`);
277+
debug(`Exception in starting reporter server : ${error}`, true, error);
278+
}
264279
}
265280

266281
registerListeners() {

bin/testhub/testhubHandler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class TestHubHandler {
108108
testhubUtils.setAccessibilityVariables(user_config, response.data);
109109
} else {
110110
process.env.BROWSERSTACK_ACCESSIBILITY = 'false';
111-
testhubUtils.checkIfAccessibilityIsSupported(user_config, false)
111+
testhubUtils.checkAndSetAccessibility(user_config, false)
112112
}
113113

114114
if (testhubUtils.shouldProcessEventForTesthub()) {
@@ -132,4 +132,4 @@ class TestHubHandler {
132132
}
133133
}
134134

135-
module.exports = TestHubHandler;
135+
module.exports = TestHubHandler;

bin/testhub/utils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports.isAccessibilityEnabled = () => {
3030
if (process.env.BROWSERSTACK_TEST_ACCESSIBILITY !== undefined) {
3131
return process.env.BROWSERSTACK_TEST_ACCESSIBILITY === "true";
3232
}
33+
logger.debug('Accessibility is disabled');
3334
return false;
3435
};
3536

@@ -135,7 +136,7 @@ const setAccessibilityCypressCapabilities = (user_config, responseData) => {
135136
process.env.ACCESSIBILITY_SCANNERVERSION = scannerVersion;
136137

137138
if (accessibilityToken && responseData.build_hashed_id) {
138-
this.checkIfAccessibilityIsSupported(user_config, true);
139+
this.checkAndSetAccessibility(user_config, true);
139140
}
140141

141142
user_config.run_settings.accessibilityOptions["authToken"] = accessibilityToken;
@@ -156,7 +157,7 @@ const jsonifyAccessibilityArray = (dataArray, keyName, valueName) => {
156157
};
157158

158159
exports.handleErrorForAccessibility = (user_config, error = null) => {
159-
exports.checkIfAccessibilityIsSupported(user_config, false);
160+
exports.checkAndSetAccessibility(user_config, false);
160161
process.env.BROWSERSTACK_TESTHUB_UUID = "null";
161162
process.env.BROWSERSTACK_TESTHUB_JWT = "null";
162163
exports.logBuildError(error, TESTHUB_CONSTANTS.ACCESSIBILITY);
@@ -237,7 +238,7 @@ exports.setTestHubCommonMetaInfo = (user_config, responseData) => {
237238
user_config.run_settings.system_env_vars.push(`REPORTER_API_PORT_NO`);
238239
};
239240

240-
exports.checkIfAccessibilityIsSupported = (user_config, accessibilityFlag) => {
241+
exports.checkAndSetAccessibility = (user_config, accessibilityFlag) => {
241242
if (!accessibilityHelper.isAccessibilitySupportedCypressVersion(user_config.run_settings.cypress_config_file))
242243
{
243244
logger.warn(`Accessibility Testing is not supported on Cypress version 9 and below.`);
@@ -282,4 +283,4 @@ exports.appendTestHubParams = (testData, eventType, accessibilityScanInfo) => {
282283
accessibility: accessibilityScanInfo[testData.name],
283284
};
284285
}
285-
};
286+
};

0 commit comments

Comments
 (0)