Skip to content

Capture a cucumber scenario's last page before the user's After hooks run - #635

Open
vishnuv688 wants to merge 3 commits into
mainfrom
fix/631-cucumber-final-capture
Open

vishnuv688 wants to merge 3 commits into
mainfrom
fix/631-cucumber-final-capture

Conversation

@vishnuv688

@vishnuv688 vishnuv688 commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

What & why

In a WebdriverIO cucumber run, the page a scenario ended on was captured after the user's After hooks had run. Cucumber runs After hooks in reverse order of definition, and WebdriverIO's cucumber framework registers its afterScenario hook before it loads the step files, so the user's hook runs first. The cucumber example's After(() => browser.reloadSession()) therefore gave the service a new session, and the last action and every assertion after it showed a black about:blank capture. A failing scenario had no picture of its failure.

  • The capture now runs at the scenario's last step. afterStep runs the end-of-scenario capture when the step is the scenario's last, or when it failed (cucumber skips the rest), while the scenario's session is still alive. The existing finalized-slot check makes afterScenario's capture a no-op after it.
  • A replaced session is never captured. The service records the session each action ran on, and every end-of-test capture (afterScenario, afterTest, and the worker-end pass in after()) is skipped when the browser's session is no longer the last action's, so a new session's blank page never stands in for the test's last state. A mocha afterEach that reloads is covered, and a test that reloads mid-way and carries on is captured on its new session.

Closes #631

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Polish (an improvement to an existing feature)
  • Breaking change (existing behavior changes for users)
  • Documentation
  • Internal (build, CI, dependencies, tooling)

Packages touched

  • shared (types and contracts)
  • core (framework-agnostic capture/reporting)
  • elements (published element/snapshot API: @wdio/elements)
  • service (WebdriverIO adapter)
  • nightwatch-devtools (Nightwatch adapter)
  • selenium-devtools (Selenium adapter)
  • selenium-devtools-py (Selenium Python adapter)
  • backend (server)
  • app (UI)
  • script (page-injected runtime)
  • trace (Trace mode)

Capability matrix

  • packages/conformance/src/features.ts updated, and tables regenerated with pnpm docs:capabilities, if this adds or changes user-facing behaviour

Not applicable: per-action captures are listed already; this fixes which page a scenario's last capture shows.

Notes for reviewers

  • Other adapters: Selenium JS ends each Gherkin step in its own AfterStep hook, so it already captures before the user's After hooks; Nightwatch orders its pre-quit capture first. Only the WebdriverIO service needed the change.
  • Verified in a real browser. DEVTOOLS_MODE=trace DEVTOOLS_SPECS=all pnpm demo:wdio against the-internet.herokuapp.com, Chrome 158: at the failed assertion of login-fail.feature, the trace's snapshot is the login page with the "Your username is invalid!" banner in its tree, and its screenshot shows the red banner over the login form. Before, both were about:blank and a black frame. Debugging the same trace through the trace query tools returned that screenshot.
  • Tests: a scenario whose last step captures its final page and whose After hook's reloadSession triggers no second capture; a failed step capturing; a step before the last not capturing; afterTest after a session replacement capturing nothing; a test that reloads and carries on capturing its last action; after() at worker end capturing nothing from a session the last test reloaded.
  • Checks run: pnpm test (347 files, 4730 tests), pnpm typecheck and lint (0 errors; the four warnings in packages/service/src/index.ts are its size and long functions, already listed as debt) pass.

Screenshots / recordings

Not applicable: no UI change.

🤖 Generated with Claude Code

… run

Cucumber runs the user's After hooks before the service's afterScenario,
so the end-of-scenario capture ran after a hook had ended or reloaded the
session. The example's reloadSession left the last action and every
assertion after it showing a blank about:blank page, and a failing
scenario had no picture of its failure. The capture now runs in afterStep
at the scenario's last step, or at a failed one, while the scenario's
session is still alive. A test whose session a hook replaced takes no
end-of-test capture from the new session.

Closes #631
@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
devtools Ready Ready Preview Nov 30, 2026 9:05am UTC

Request Review

@vishnuv688 vishnuv688 self-assigned this Nov 30, 2026
@greptile-apps

greptile-apps Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; all previous capture issues are addressed.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; all previous capture issues are addressed.

Summary

The PR captures Cucumber's final page before the user's After hooks can replace the session.

  • Cucumber traces keep the page from their last or failed step.
  • Final snapshots only come from the session that ran their action.

Reviews (3) · Last reviewed commit: "Check the final capture against the sess..." · Reviewed by Greptile

Comment thread packages/service/src/index.ts Outdated
Comment on lines +602 to +606
if (this.#testSessionId && sessionId !== this.#testSessionId) {
log.info(
'The session was replaced after the test ran, so its last page is not captured again'
)
return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Blank capture returns at teardown

When the last Mocha test reloads its session in afterEach, afterTest skips capture without filling #finalizedSlot. But after() still calls #finalizePerScenario() directly. It captures the replacement session's blank page at the test's last action timestamp, so session and spec traces can still show the wrong final page. Apply the session check to worker-end capture too, while preserving standalone capture.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 217da69: the session check moved into #finalizePerScenario itself, so the worker-end pass in after() gets it too. A session with no action, the standalone path, has nothing to compare and is still captured. Test: "takes no capture at worker end from a session the last test reloaded" in trace-action-capture.test.ts.

Comment thread packages/service/src/index.ts Outdated
Comment on lines +601 to +602
const sessionId = this.#browser?.sessionId
if (this.#testSessionId && sessionId !== this.#testSessionId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Reload hides the final result

The check also skips valid captures when a Mocha or Jasmine test reloads and then continues. A test can call reloadSession(), open a page, and click submit. onReload restores capture, but neither it nor later commands update #testSessionId. afterTest then skips the submit result and writes the test's artifacts with an older frame. Track which session owns the last action rather than treating every session change as teardown.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 217da69: the service now records the session each action ran on (in #afterCommand, for commands that map to a row, so reloadSession itself does not count), and skips a final capture only when the browser's session is no longer the last action's. A test that reloads, opens a page and submits is captured on its new session. Test: "captures the last action of a test that reloaded its session and carried on".

Comparing with the session a test started on skipped a valid capture
when a test reloaded and carried on, and the worker-end pass in after()
still captured a blank page when the last test's afterEach reloaded.
The service now records the session each action ran on, and every
end-of-test capture, at test end and at worker end, is skipped only when
the browser's session is no longer that one.
Comment on lines +1021 to +1023
if (this.#isActionCommand(command)) {
this.#lastActionSessionId = this.#browser.sessionId
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Final picture uses wrong session

A test can click on session A, call reloadSession(), then call getTitle() on session B. The read updates #lastActionSessionId to B, so the session check passes. But #finalizePerScenario skips reads when choosing the last page-changing command and captures B's blank page at A's click timestamp. The trace then shows the wrong result for that click.

Track the session alongside the command selected for the final capture, rather than updating one shared field for every mapped command.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 20f75ad: the service now keeps the session of the last page-changing action beside the last action's, and #finalCaptureTarget returns the action the final capture is of together with the session that action ran on, which is what the check compares. Clicking on A, reloading, then reading on B no longer lets the click be captured on B. Test: "takes no capture of a click on a session a later read ran after a reload".

The final capture is of the test's last page-changing action, but the
session it was checked against was the last action's of any kind, so a
read on a reloaded session let a click from the session before be
captured on the new blank page. The service now keeps the session of the
last page-changing action beside the last action's, and checks the one
belonging to the action the capture is of.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebdriverIO cucumber: the final capture runs after the user's After hooks, showing about:blank

1 participant