fix(automate): name the session created by a mid-test reload (SDK-7270) - #163
fix(automate): name the session created by a mid-test reload (SDK-7270)#163harshit-browserstack wants to merge 2 commits into
Conversation
Follow-up to #148. @wdio/mocha-framework binds beforeTest to the test function itself (wrapGlobalTestMethod), so onBeforeTest is the single per-test naming opportunity and it has already passed by the time the test body calls browser.reloadSession(). The replacement session is never registered in sessionMap, so flushSessionName() returns early on !sessionData and the onAfterExecute sweep -- which iterates the same map -- misses it too. service.onReload learns the new session id but only renames the outgoing one, leaving the live session on its creation-time sessionName capability. onAfterTest now re-resolves the live session id and adopts it into sessionMap before flushing the name, while that session is still open. The naming block is gated on skipSessionName rather than skipSessionStatus and sits above the status gate: gating a name repair behind a status flag would skip it for setSessionStatus:false users, and adopting unconditionally would pull setSessionName:false users into sessionMap and start issuing them a status PUT per session where they previously had none. Adoption also records the test result, so the post-reload session's status is marked at teardown instead of being dropped by the old `if (sessionData)` guard. Steady state costs nothing extra -- appliedName de-dupes the flush when the session did not change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
✅ Automated review — no blocking findingsScope: the 2-file diff on this branch (
Bail-skipped tests — checked, unaffected
Worth separating out, though: on Verification backing this
Two limits on this verdict
|
What is this about?
Follow-up to #148 (shipped in 9.34.0), which moved App Automate / Automate session naming to fire per test from
automateModule.onBeforeTest. A residual case remained: a session replaced during a test is still never named.@wdio/mocha-frameworkbindsbeforeTestto the test function itself (wrapGlobalTestMethod), soonBeforeTestis the single per-test naming opportunity — and it has already passed by the time the test body callsbrowser.reloadSession(). The replacement session is never registered insessionMap, soflushSessionName()returns early on!sessionDataand theonAfterExecuteteardown sweep — which iterates that same map — misses it too.service.onReloadlearns the new session id but the only rename it issues targets the outgoing session. Net effect: the new session keeps its creation-timesessionNamecapability. A reload between tests is unaffected, because that runs beforebeforeTest.onAfterTestnow re-resolves the live session id and adopts it intosessionMapbefore flushing the name, while that session is still open.Two deliberate details:
skipSessionName, notskipSessionStatus, and sits above the status gate. Gating a name repair behind a status flag would silently skip it forsetSessionStatus: false+setSessionName: trueusers.skipSessionNameis set. Those users'sessionMapis empty today, soonAfterExecuteissues them no status calls at all; adopting unconditionally would have started sending one per session.Steady state costs nothing extra: with no mid-test reload the session id is unchanged and
appliedNamede-dupes the flush away.Also fixes a second effect on the same path — the post-reload session's
TestResultwas dropped by the oldif (sessionData)guard, soonAfterExecutenever marked its status either. It now carries its own pass/fail result.Verification (App Automate, iOS, WDIO 9 + Mocha, mid-test
reloadSession()):automation_session.name: "STATIC-CAP-NAME-should-be-overwritten"SDK-7270-after2-094653, both sessions carry the test title:automation_session.name: "SDK-7270 mid-test session reload - SUBJECT reloads the session mid-test"automateModule.test.ts35/35, including 4 new tests: mid-test-reload naming, steady-state de-dupe (asserts zero extra calls), guard independence underskipSessionStatus, and no new status traffic underskipSessionName.Known residual, out of scope here — and correctly so. A worker's initial session, when the suite discards it with a
reloadSession()before the first test runs, hosts no test at all. There is no title to apply, andsessionMapis reachable only fromonBeforeTest/onAfterTest— both of which require a test — so no naming path can or should touch it. On the reported build this takes statically-named sessions from 5 to 2, not to 0. That residual class is a suite-structure artifact rather than an SDK defect: in the same run, on the same build, the two workers that did not discard their initial session had it named normally. (The trigger for the discard sits in spec/helper code not present in the captured archive, so it is not attributed further here.)Cucumber is not reached by this path (
beforeScenario/afterScenarionever emitTestFrameworkState.TEST) — a pre-existing gap, neither helped nor harmed. Jasmine is covered.Related Jira task/s
SDK-7270
Release (mandatory for every PR — required for the
ready-for-reviewlabel)Version bump: (required — tick exactly one)
Release notes type: (optional)
Release notes (customer-facing): (optional but encouraged)
sessionNamecapability when a test reloads the session part-way through — for example a deep-link or app-relaunch step. Such sessions now show the test title, and also carry their own pass/fail result.Release notes (internal): (required — engineer-facing; what actually changed / why)
automateModule.onAfterTestnow re-resolves the live session id and adopts it intosessionMapbefore flushing the name, covering sessions created by areloadSession()inside the test body —onBeforeTest(bound per test function by@wdio/mocha-framework) has already run by then, so those sessions were never registered and neither the per-test flush nor theonAfterExecutesweep could reach them.skipSessionNameand moved above theskipSessionStatusearly return, so asetSessionStatus: falseuser still gets the name repair and asetSessionName: falseuser is not pulled intosessionMap(which would have added a status PUT per session where they previously had none).if (sessionData)guard.appliedNamede-dupes the second flush when the session did not change.Checklist
PR Validations
Run Tests: Comment RUN_TESTS to trigger sanity tests.