@@ -9,6 +9,7 @@ const path = require('path');
99const AccessibilityAutomation = require ( '../src/accessibilityAutomation' ) ;
1010const eventHelper = require ( '../src/utils/eventHelper' ) ;
1111const OrchestrationUtils = require ( '../src/testorchestration/orchestrationUtils' ) ;
12+ const TestMap = require ( '../src/utils/testMap' ) ;
1213const localTunnel = new LocalTunnel ( ) ;
1314const testObservability = new TestObservability ( ) ;
1415const accessibilityAutomation = new AccessibilityAutomation ( ) ;
@@ -19,6 +20,8 @@ const _tests = {};
1920const _testCasesData = { } ;
2021let currentTestUUID = '' ;
2122let workerList = { } ;
23+ let testRunner = '' ;
24+ let testEventPromises = [ ] ;
2225
2326eventHelper . eventEmitter . on ( EVENTS . LOG_INIT , ( loggingData ) => {
2427 const testCaseStartedId = loggingData . message . replace ( 'TEST-OBSERVABILITY-PID-TESTCASE-MAPPING-' , '' ) . slice ( 1 , - 1 ) ;
@@ -65,8 +68,8 @@ module.exports = {
6568 promises . push ( testObservability . processTestReportFile ( JSON . parse ( JSON . stringify ( modulesWithEnv [ testSetting ] [ testFile ] ) ) ) ) ;
6669 }
6770 }
68-
6971 await Promise . all ( promises ) ;
72+
7073 done ( ) ;
7174 } catch ( error ) {
7275 CrashReporter . uploadCrashReport ( error . message , error . stack ) ;
@@ -92,7 +95,7 @@ module.exports = {
9295 const gherkinDocument = reportData ?. gherkinDocument . find ( ( document ) => document . uri === pickleData . uri ) ;
9396 const featureData = gherkinDocument . feature ;
9497 const uniqueId = uuidv4 ( ) ;
95- process . env . TEST_OPS_TEST_UUID = uniqueId ;
98+ process . env . TEST_RUN_UUID = uniqueId ;
9699
97100 Object . values ( workerList ) . forEach ( ( worker ) => {
98101 worker . process . on ( 'message' , async ( data ) => {
@@ -253,15 +256,37 @@ module.exports = {
253256
254257 eventBroadcaster . on ( 'ScreenshotCreated' , async ( args ) => {
255258 if ( ! helper . isTestObservabilitySession ( ) ) { return }
256- handleScreenshotUpload ( { args : args , uuid : process . env . TEST_OPS_TEST_UUID } ) ;
259+ handleScreenshotUpload ( { args : args , uuid : process . env . TEST_RUN_UUID } ) ;
257260 } ) ;
258261
259262 eventBroadcaster . on ( 'TestRunStarted' , async ( test ) => {
263+ process . env . VALID_ALLY_PLATFORM = accessibilityAutomation . validateA11yCaps ( browser ) ;
260264 await accessibilityAutomation . beforeEachExecution ( test ) ;
265+ if ( testRunner !== 'cucumber' ) {
266+ const uuid = TestMap . storeTestDetails ( test ) ;
267+ process . env . TEST_RUN_UUID = uuid ;
268+ testEventPromises . push ( testObservability . sendTestRunEvent ( 'TestRunStarted' , test , uuid ) ) ;
269+ }
261270 } ) ;
262271
263272 eventBroadcaster . on ( 'TestRunFinished' , async ( test ) => {
264- await accessibilityAutomation . afterEachExecution ( test ) ;
273+ const uuid = process . env . TEST_RUN_UUID || TestMap . getUUID ( test ) ;
274+ if ( TestMap . hasTestFinished ( uuid ) ) {
275+ Logger . debug ( `Test with UUID ${ uuid } already marked as finished, skipping duplicate TestRunFinished event` ) ;
276+
277+ return ;
278+ }
279+ try {
280+ await accessibilityAutomation . afterEachExecution ( test , uuid ) ;
281+ if ( testRunner !== 'cucumber' ) {
282+ testEventPromises . push ( testObservability . sendTestRunEvent ( 'TestRunFinished' , test , uuid ) ) ;
283+ TestMap . markTestFinished ( uuid ) ;
284+ }
285+
286+ } catch ( error ) {
287+ Logger . error ( `Error in TestRunFinished event: ${ error . message } ` ) ;
288+ TestMap . markTestFinished ( uuid ) ;
289+ }
265290 } ) ;
266291 } ,
267292
@@ -272,10 +297,12 @@ module.exports = {
272297 } ,
273298
274299 async before ( settings , testEnvSettings ) {
300+ testRunner = settings . test_runner . type ;
301+ const pluginSettings = settings [ '@nightwatch/browserstack' ] ;
275302 if ( ! settings . desiredCapabilities [ 'bstack:options' ] ) {
276303 settings . desiredCapabilities [ 'bstack:options' ] = { } ;
277304 }
278-
305+ process . env . BROWSERSTACK_APP_AUTOMATE = helper . checkTestEnvironmentForAppAutomate ( testEnvSettings ) ;
279306 // Plugin identifier
280307 settings . desiredCapabilities [ 'bstack:options' ] [ 'browserstackSDK' ] = `nightwatch-plugin/${ helper . getAgentVersion ( ) } ` ;
281308
@@ -295,6 +322,7 @@ module.exports = {
295322
296323 try {
297324 testObservability . configure ( settings ) ;
325+ accessibilityAutomation . configure ( settings ) ;
298326 if ( helper . isTestObservabilitySession ( ) ) {
299327 if ( settings . reporter_options ) {
300328 if ( settings . reporter_options [ 'save_command_result_value' ] !== true ) {
@@ -312,7 +340,7 @@ module.exports = {
312340 settings . test_runner . options [ 'require' ] = path . resolve ( __dirname , 'observabilityLogPatcherHook.js' ) ;
313341 }
314342 settings . globals [ 'customReporterCallbackTimeout' ] = CUSTOM_REPORTER_CALLBACK_TIMEOUT ;
315- if ( testObservability . _user && testObservability . _key ) {
343+ if ( helper . isTestHubBuild ( pluginSettings , true ) ) {
316344 await testObservability . launchTestSession ( ) ;
317345 }
318346 if ( process . env . BROWSERSTACK_RERUN === 'true' && process . env . BROWSERSTACK_RERUN_TESTS && process . env . BROWSERSTACK_RERUN_TESTS !== 'null' ) {
@@ -323,7 +351,17 @@ module.exports = {
323351 } catch ( error ) {
324352 Logger . error ( `Could not configure or launch test reporting and analytics - ${ error } ` ) ;
325353 }
326-
354+
355+ try {
356+ // In parallel mode, env-specific settings are passed to beforeChildProcess hook instead of before hook,
357+ if ( helper . isAccessibilitySession ( ) && ! settings . parallel_mode ) {
358+ accessibilityAutomation . setAccessibilityCapabilities ( settings ) ;
359+ accessibilityAutomation . commandWrapper ( ) ;
360+ helper . patchBrowserTerminateCommand ( ) ;
361+ }
362+ } catch ( err ) {
363+ Logger . debug ( `Exception while setting Accessibility Automation capabilities. Error ${ err } ` ) ;
364+ }
327365 // Initialize and configure test orchestration
328366 try {
329367 if ( helper . isTestObservabilitySession ( ) ) {
@@ -408,22 +446,6 @@ module.exports = {
408446 Logger . error ( `Could not configure test orchestration - ${ error } ` ) ;
409447 }
410448
411- try {
412- accessibilityAutomation . configure ( settings ) ;
413- if ( helper . isAccessibilitySession ( ) ) {
414- if ( accessibilityAutomation . _user && accessibilityAutomation . _key ) {
415- const [ jwtToken , testRunId ] = await accessibilityAutomation . createAccessibilityTestRun ( ) ;
416- process . env . BS_A11Y_JWT = jwtToken ;
417- process . env . BS_A11Y_TEST_RUN_ID = testRunId ;
418- if ( helper . isAccessibilitySession ( ) ) {
419- accessibilityAutomation . setAccessibilityCapabilities ( settings ) ;
420- }
421- }
422- }
423- } catch ( error ) {
424- Logger . error ( `Could not configure or launch accessibility automation - ${ error } ` ) ;
425- }
426-
427449 addProductMapAndbuildUuidCapability ( settings ) ;
428450 } ,
429451
@@ -443,46 +465,40 @@ module.exports = {
443465 } catch ( error ) {
444466 Logger . error ( `Error collecting build data for test orchestration: ${ error } ` ) ;
445467 }
446-
447- if ( helper . isTestObservabilitySession ( ) ) {
468+
469+ if ( helper . isTestHubBuild ( ) ) {
448470 process . env . NIGHTWATCH_RERUN_FAILED = nightwatchRerun ;
449471 process . env . NIGHTWATCH_RERUN_REPORT_FILE = nightwatchRerunFile ;
450472 if ( process . env . BROWSERSTACK_RERUN === 'true' && process . env . BROWSERSTACK_RERUN_TESTS ) {
451473 await helper . deleteRerunFile ( ) ;
452474 }
453475 try {
476+ if ( testEventPromises . length > 0 ) {
477+ await Promise . all ( testEventPromises ) ;
478+ testEventPromises . length = 0 ; // Clear the array
479+ }
454480 await testObservability . stopBuildUpstream ( ) ;
455- if ( process . env . BS_TESTOPS_BUILD_HASHED_ID ) {
456- Logger . info ( `\nVisit https://automation.browserstack.com/builds/${ process . env . BS_TESTOPS_BUILD_HASHED_ID } to view build report, insights, and many more debugging information all at one place!\n` ) ;
481+ if ( process . env . BROWSERSTACK_TESTHUB_UUID ) {
482+ Logger . info ( `\nVisit https://automation.browserstack.com/builds/${ process . env . BROWSERSTACK_TESTHUB_UUID } to view build report, insights, and many more debugging information all at one place!\n` ) ;
457483 }
458484 } catch ( error ) {
459485 Logger . error ( `Something went wrong in stopping build session for test reporting and analytics - ${ error } ` ) ;
460486 }
461487 process . exit ( ) ;
462488 }
463- if ( helper . isAccessibilitySession ( ) ) {
464- try {
465- await accessibilityAutomation . stopAccessibilityTestRun ( ) ;
466- } catch ( error ) {
467- Logger . error ( `Exception in stop accessibility test run: ${ error } ` ) ;
468- }
469-
470- }
471489 } ,
472490
473491 async beforeEach ( settings ) {
474492 browser . getAccessibilityResults = ( ) => { return accessibilityAutomation . getAccessibilityResults ( ) } ;
475493 browser . getAccessibilityResultsSummary = ( ) => { return accessibilityAutomation . getAccessibilityResultsSummary ( ) } ;
476- // await accessibilityAutomation.beforeEachExecution(browser);
477494 } ,
478495
479496 // This will be run after each test suite is finished
480497 async afterEach ( settings ) {
481- // await accessibilityAutomation.afterEachExecution(browser);
482498 } ,
483499
484500 beforeChildProcess ( settings ) {
485-
501+
486502 if ( ! settings . desiredCapabilities [ 'bstack:options' ] ) {
487503 settings . desiredCapabilities [ 'bstack:options' ] = { } ;
488504 }
@@ -514,12 +530,23 @@ module.exports = {
514530 try {
515531 if ( helper . isAccessibilitySession ( ) ) {
516532 accessibilityAutomation . setAccessibilityCapabilities ( settings ) ;
533+ accessibilityAutomation . commandWrapper ( ) ;
534+ helper . patchBrowserTerminateCommand ( ) ;
517535 }
518536 } catch ( err ) {
519537 Logger . debug ( `Exception while setting Accessibility Automation capabilities. Error ${ err } ` ) ;
520538 }
521539 addProductMapAndbuildUuidCapability ( settings ) ;
522540
541+ } ,
542+
543+ async afterChildProcess ( ) {
544+
545+ await helper . shutDownRequestHandler ( ) ;
546+ if ( testEventPromises . length > 0 ) {
547+ await Promise . all ( testEventPromises ) ;
548+ testEventPromises . length = 0 ; // Clear the array
549+ }
523550 }
524551} ;
525552
@@ -569,10 +596,10 @@ const addProductMapAndbuildUuidCapability = (settings) => {
569596
570597 if ( settings . desiredCapabilities [ 'bstack:options' ] ) {
571598 settings . desiredCapabilities [ 'bstack:options' ] [ 'buildProductMap' ] = buildProductMap ;
572- settings . desiredCapabilities [ 'bstack:options' ] [ 'testhubBuildUuid' ] = process . env . BS_TESTOPS_BUILD_HASHED_ID ? process . env . BS_TESTOPS_BUILD_HASHED_ID : '' ;
599+ settings . desiredCapabilities [ 'bstack:options' ] [ 'testhubBuildUuid' ] = process . env . BROWSERSTACK_TESTHUB_UUID ? process . env . BROWSERSTACK_TESTHUB_UUID : '' ;
573600 } else {
574601 settings . desiredCapabilities [ 'browserstack.buildProductMap' ] = buildProductMap ;
575- settings . desiredCapabilities [ 'browserstack.testhubBuildUuid' ] = process . env . BS_TESTOPS_BUILD_HASHED_ID ? process . env . BS_TESTOPS_BUILD_HASHED_ID : '' ;
602+ settings . desiredCapabilities [ 'browserstack.testhubBuildUuid' ] = process . env . BROWSERSTACK_TESTHUB_UUID ? process . env . BROWSERSTACK_TESTHUB_UUID : '' ;
576603 }
577604 } catch ( error ) {
578605 Logger . debug ( `Error while sending productmap and build capabilities ${ error } ` ) ;
0 commit comments