Skip to content

Commit 8539ae8

Browse files
committed
review changes pt.4
1 parent bb86129 commit 8539ae8

File tree

4 files changed

+85
-24
lines changed

4 files changed

+85
-24
lines changed

nightwatch/globals.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,31 @@ module.exports = {
259259
});
260260

261261
eventBroadcaster.on('TestRunStarted', async (test) => {
262-
TestMap.storeTestDetails(test);
262+
process.env.VALID_ALLY_PLATFORM = accessibilityAutomation.validateA11yCaps(browser)
263263
await accessibilityAutomation.beforeEachExecution(test)
264264
if (testRunner != "cucumber"){
265-
const uuid = TestMap.getUUID(test);
265+
const uuid = TestMap.storeTestDetails(test);
266266
process.env.TEST_RUN_UUID = uuid;
267267
await testObservability.sendTestRunEvent('TestRunStarted', test, uuid)
268268
}
269-
270269
});
271270

272271
eventBroadcaster.on('TestRunFinished', async (test) => {
273272
const uuid = process.env.TEST_RUN_UUID || TestMap.getUUID(test);
274-
await accessibilityAutomation.afterEachExecution(test, uuid);
275-
if (testRunner != "cucumber"){
276-
await testObservability.sendTestRunEvent('TestRunFinished', test, uuid)
273+
if (TestMap.hasTestFinished(uuid)) {
274+
Logger.debug(`Test with UUID ${uuid} already marked as finished, skipping duplicate TestRunFinished event`);
275+
return;
276+
}
277+
try {
278+
await accessibilityAutomation.afterEachExecution(test, uuid)
279+
if (testRunner != "cucumber"){
280+
await testObservability.sendTestRunEvent('TestRunFinished', test, uuid)
281+
TestMap.markTestFinished(uuid);
282+
}
283+
284+
} catch (error) {
285+
Logger.error(`Error in TestRunFinished event: ${error.message}`);
286+
TestMap.markTestFinished(uuid);
277287
}
278288
});
279289
},
@@ -341,6 +351,7 @@ module.exports = {
341351
}
342352

343353
try {
354+
// In parallel mode, env-specific settings are passed to beforeChildProcess hook instead of before hook,
344355
if (helper.isAccessibilitySession() && !settings.parallel_mode) {
345356
accessibilityAutomation.setAccessibilityCapabilities(settings);
346357
accessibilityAutomation.commandWrapper();

src/scripts/accessibilityScripts.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,23 @@ class AccessibilityScripts {
8484
}
8585

8686
store() {
87-
if (!fs.existsSync(this.browserstackFolderPath)){
88-
fs.mkdirSync(this.browserstackFolderPath);
89-
}
90-
91-
fs.writeFileSync(this.commandsPath, JSON.stringify({
92-
commands: this.commandsToWrap,
93-
scripts: {
94-
scan: this.performScan,
95-
getResults: this.getResults,
96-
getResultsSummary: this.getResultsSummary,
97-
saveResults: this.saveTestResults
87+
try {
88+
if (!fs.existsSync(this.browserstackFolderPath)){
89+
fs.mkdirSync(this.browserstackFolderPath);
9890
}
99-
}));
91+
92+
fs.writeFileSync(this.commandsPath, JSON.stringify({
93+
commands: this.commandsToWrap,
94+
scripts: {
95+
scan: this.performScan,
96+
getResults: this.getResults,
97+
getResultsSummary: this.getResultsSummary,
98+
saveResults: this.saveTestResults
99+
}
100+
}));
101+
} catch (error) {
102+
Logger.debug(`Failed to store accessibility commands file: ${error.message}`);
103+
}
100104
}
101105
}
102106

src/testObservability.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ const CrashReporter = require('./utils/crashReporter');
99
const Logger = require('./utils/logger');
1010
const {API_URL, TAKE_SCREENSHOT_REGEX} = require('./utils/constants');
1111
const OrchestrationUtils = require('./testorchestration/orchestrationUtils');
12+
const AccessibilityAutomation = require('./accessibilityAutomation');
1213
const accessibilityScripts = require('./scripts/accessibilityScripts');
1314
const TestMap = require('./utils/testMap');
1415
const hooksMap = {};
16+
const accessibilityAutomation = new AccessibilityAutomation();
1517

1618
class TestObservability {
1719
configure(settings = {}) {
@@ -92,7 +94,7 @@ class TestObservability {
9294
this._parentSettings?.testReportingOptions ||
9395
this._parentSettings?.testObservabilityOptions ||
9496
{};
95-
const accessibility = this._settings.accessibility || false;
97+
const accessibility = helper.isAccessibilityEnabled(this._parentSettings);
9698
const accessibilityOptions = accessibility ? this._settings.accessibilityOptions || {} : {};
9799
this._gitMetadata = await helper.getGitMetaData();
98100
const fromProduct = {
@@ -490,7 +492,8 @@ class TestObservability {
490492
[provider]: helper.getIntegrationsObject(testMetaData.sessionCapabilities, testMetaData.sessionId, testMetaData.host, settings?.desiredCapabilities?.['bstack:options']?.osVersion)
491493
},
492494
product_map: {
493-
accessibility: helper.isAccessibilitySession()
495+
observability: helper.isTestObservabilitySession(),
496+
accessibility: helper.isAccessibilitySession() && accessibilityAutomation.shouldScanTestForAccessibility() && process.env.VALID_ALLY_PLATFORM
494497
}
495498
};
496499

src/utils/testMap.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,77 @@ const {v4: uuidv4} = require('uuid');
22

33
const sharedTestMap = new Map();
44
let sharedCurrentTest = null;
5+
let activeTestRuns = new Map();
56

67
class TestMap {
78

89
static storeTestDetails(test) {
910
const testIdentifier = this.generateTestIdentifier(test);
11+
const uuid = this.generateUUID();
1012

1113
if (!sharedTestMap.has(testIdentifier)) {
12-
const uuid = this.generateUUID();
1314
sharedTestMap.set(testIdentifier, {
14-
uuid,
15+
baseUuid: uuid, // Store the first UUID as base
16+
retries: [],
17+
currentUuid: uuid,
1518
test,
1619
createdAt: new Date().toISOString()
1720
});
21+
} else {
22+
// This is a retry - add new UUID to retries array
23+
const testData = sharedTestMap.get(testIdentifier);
24+
testData.retries.push({
25+
uuid,
26+
startedAt: new Date().toISOString()
27+
});
28+
testData.currentUuid = uuid; // Update to latest UUID
29+
sharedTestMap.set(testIdentifier, testData);
1830
}
31+
32+
// Track this as an active test run
33+
activeTestRuns.set(uuid, {
34+
identifier: testIdentifier,
35+
startedAt: new Date().toISOString(),
36+
hasFinished: false
37+
});
38+
1939
sharedCurrentTest = testIdentifier;
40+
41+
return uuid;
2042
}
2143

2244
static getUUID(test = null) {
2345
if (test) {
2446
const testIdentifier = typeof test === 'string' ? test : this.generateTestIdentifier(test);
2547
const testData = sharedTestMap.get(testIdentifier);
2648

27-
return testData ? testData.uuid : null;
49+
return testData ? testData.currentUuid : null;
2850
}
51+
52+
return null;
2953
}
3054

55+
static markTestFinished(uuid) {
56+
if (activeTestRuns.has(uuid)) {
57+
const testRun = activeTestRuns.get(uuid);
58+
testRun.hasFinished = true;
59+
testRun.finishedAt = new Date().toISOString();
60+
activeTestRuns.set(uuid, testRun);
61+
62+
return true;
63+
}
64+
65+
return false;
66+
}
67+
68+
static hasTestFinished(uuid) {
69+
const testRun = activeTestRuns.get(uuid);
70+
return testRun ? testRun.hasFinished : false;
71+
}
72+
73+
3174
static getTestDetails(identifier) {
32-
return sharedTestMap.has(identifier) ? sharedTestMap.get(identifier) : null;
75+
return sharedTestMap.has(identifier) ? sharedTestMap.get(identifier) : null;
3376
}
3477

3578
static generateTestIdentifier(test) {

0 commit comments

Comments
 (0)