Skip to content

[rb] track the current window in the BiDi bridge so navigation skips the window handle round trip - #18374

Merged
titusfortner merged 1 commit into
SeleniumHQ:trunkfrom
titusfortner:rb-bidi-current-window
Nov 23, 2026
Merged

titusfortner merged 1 commit into
SeleniumHQ:trunkfrom
titusfortner:rb-bidi-current-window

Conversation

@titusfortner

Copy link
Copy Markdown
Member

🔗 Related Issues

Builds on #17785 (Ruby navigation over the generated BiDi protocol layer)

💥 What does this PR do?

  • Ruby BiDi navigation (get, back, forward, refresh) no longer asks the driver for the current window handle before every command; the BiDi bridge keeps it from session start and switch_to.window.
  • Navigating after the current window is closed still raises NoSuchWindowError, as in classic.

🔧 Implementation Notes

  • BiDi reports an unknown window or frame with classic's no such frame code, so each navigation method turns NoSuchFrameError into the NoSuchWindowError classic raises.
  • Alternatives considered: mapping no such frame to NoSuchWindowError for all BiDi commands (wrong when the missing thing is a realm, which BiDi reports with the same code), and making NoSuchFrameError a subclass of NoSuchWindowError (changes which rescue clauses catch classic frame errors).
  • Window tracking is covered by BiDiBridge unit specs; the closed-window case stays an integration spec so the classic run confirms the expected error.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s): Claude Code (Opus 5.5)
    • What was generated: design analysis, implementation, specs, and this description
    • I reviewed all AI output and can explain the change

💡 Additional Considerations

  • Tracking additional browsing contexts will be implemented as needed.

🔄 Types of changes

  • Cleanup (removes a classic round trip from each BiDi navigation; no behavior change)

@selenium-ci

Copy link
Copy Markdown
Member

/agentic_describe

@selenium-ci

Copy link
Copy Markdown
Member

/agentic_review

@selenium-ci selenium-ci added the C-rb Ruby Bindings label Nov 23, 2026
@qodo-code-review

qodo-code-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Cache the current window for Ruby BiDi navigation

✨ Enhancement 🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Track the current window at session start and after window switches, avoiding a handle request per
 navigation.
• Preserve classic NoSuchWindowError behavior when navigating after the current window closes.
• Add unit and integration coverage for window tracking and closed-window navigation.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed initialization, window-switch synchronization, and four navigation operations jointly implement one bounded user-visible behavior: controlling navigation/history for the currently selected WebDriver window.
  • TargetLocator and the underlying bridge are propagation hops for this same behavior, not independent capabilities; the source shows new-window and block-based window switching ultimately invoke the bridge switch operation.
Sources

rb/lib/selenium/webdriver/remote/bidi_bridge.rb[100-102]

        def switch_to_window(name)
          super.tap { @current_window = name }
        end
Behavioral complexity 🟡 Medium 🧩 Interacting branches

  • Before: each navigation/history/refresh operation obtained the current window handle at call time.
  • After: session creation establishes @current_window, successful switch_to_window updates it, and four operations consume the cached value; closed-context BiDi errors are additionally translated.
  • New correctness condition: the cached handle must remain synchronized with the browser's selected and still-existing window across session creation, explicit switches, new-window flows, block restoration, and window closure.
  • The window-switch branches and block restoration themselves are existing infrastructure; the materially new interaction is their state update with the BiDi navigation calls.
  • The error rescue is a narrow exception translation, not an independent algorithm or concurrency protocol.
Sources

rb/lib/selenium/webdriver/remote/bidi_bridge.rb[100-102]

        def switch_to_window(name)
          super.tap { @current_window = name }
        end
Potential damage 🟡 Medium 🩹 Recoverable

  • Within the affected capability, the most credible consequence is navigation/history/refresh degradation or, under an unverified cache-divergence condition, navigation in the wrong window.
  • Recovery does not require persistent-state repair: switching to a valid window updates the cache, and recreating the session restores initialization.
  • A code revert restores per-operation window-handle lookup, but would not compensate for an already-issued navigation to the wrong context.
  • The equal-importance baseline is used because no business-criticality declaration was supplied.
  • Recoverable by switching to a valid window or creating a new session; no persistent application data, queued work, or external financial/security effect is introduced by this bridge state.
  • If a wrong-window navigation has already occurred, the caller may need to navigate back or explicitly restore the intended page.
Sources

rb/lib/selenium/webdriver/remote/bidi_bridge.rb[100-102]

        def switch_to_window(name)
          super.tap { @current_window = name }
        end
Assessed commit d20bd61
Diagram

graph TD
  Session["Session creation"] --> Current["Current window"] --> Navigation["BiDi navigation"] --> Context["Browsing context"] --> Missing{"Context missing?"} --> Error["Window error"]
  Switch["Window switch"] --> Current
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Fetch the handle before every navigation
  • ➕ Avoids maintaining a cached handle.
  • ➖ Retains an HTTP round trip for every BiDi navigation command.
2. Map all BiDi no-such-frame errors globally
  • ➕ Centralizes error translation.
  • ➖ Misclassifies missing realms, which can use the same error code.

Recommendation: Keep the PR's bridge-local tracking and navigation-specific error translation. It removes the repeated request without changing how unrelated BiDi errors or classic frame errors are classified.

Files changed (4) +72 / -4

Enhancement (2) +21 / -4
bidi_bridge.rbTrack the active window for BiDi navigation +17/-4

Track the active window for BiDi navigation

• Captures the window handle when a session starts and updates it after a successful window switch. Get, back, forward, and refresh use the tracked handle instead of requesting it each time, and translate missing-context frame errors to window errors.

rb/lib/selenium/webdriver/remote/bidi_bridge.rb

bidi_bridge.rbsDeclare tracked window state and switch override +4/-0

Declare tracked window state and switch override

• Adds the current-window instance variable and the BiDi bridge's window-switch method to its Ruby signature.

rb/sig/lib/selenium/webdriver/remote/bidi_bridge.rbs

Tests (2) +51 / -0
navigation_spec.rbVerify navigation errors after closing the current window +21/-0

Verify navigation errors after closing the current window

• Adds an integration scenario asserting that all four navigation operations raise NoSuchWindowError after the active tab closes. Restores the original window during cleanup.

rb/spec/integration/selenium/webdriver/navigation_spec.rb

bidi_bridge_spec.rbTest cached window selection and handle requests +30/-0

Test cached window selection and handle requests

• Verifies that navigation targets a switched-to window and that successive navigation calls request the window handle only once, during session creation.

rb/spec/unit/selenium/webdriver/remote/bidi_bridge_spec.rb

@qodo-code-review

qodo-code-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Window-error mapping lacks a comment explaining why ✗ Dismissed

Dismissed as Intentional

Description
The four navigation methods (get, go_back, go_forward, refresh) each rescue
Error::NoSuchFrameError and re-raise it as Error::NoSuchWindowError, which is a non-obvious
decision: BiDi reports an unknown window with classic's no such frame code, so the
reclassification is what keeps the classic error contract. Nothing at the rescue sites says this,
and the reasoning (including why the mapping is not applied to all BiDi commands, since a missing
realm uses the same code) lives only in the PR description. A later maintainer seeing a
NoSuchFrameError rescue that reclassifies it could reasonably remove it as a bug, reintroducing
the behavior difference from classic.
Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

Issue description
The BiDi navigation methods in the Ruby bridge rescue `Error::NoSuchFrameError` and re-raise `Error::NoSuchWindowError`. The reason (BiDi reports an unknown window with the classic `no such frame` code, and classic raises `NoSuchWindowError` for it) is not stated in code, so future maintainers may remove the mapping as a bug.

Fix Focus Areas
- rb/lib/selenium/webdriver/remote/bidi_bridge.rb[60-64]
- rb/lib/selenium/webdriver/remote/bidi_bridge.rb[66-72]

Recommended Fix
Add a short comment above the first rescue (in `get`) explaining that BiDi reports an unknown current window with `no such frame`, and that it is mapped to `NoSuchWindowError` to match classic. Optionally note that the mapping is deliberately not applied to all BiDi commands because a missing realm uses the same code. Keep the comment to the why; do not restate the rescue itself.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Navigation fails with a socket error after the last window closes ✗ Dismissed

Dismissed as Intentional

Description
get, go_back, go_forward, and refresh use the cached @current_window without checking
whether close has shut down the BiDi socket. When the last window is closed, navigation reaches
that closed socket and raises IOError instead of receiving a WebDriver window or session error, as
the previous window-handle lookup would have.
Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Navigation bypasses the window-handle request after the last window closes, so it sends a command through a closed BiDi socket and raises `IOError`.
## Fix Focus Areas
- rb/lib/selenium/webdriver/remote/bidi_bridge.rb[61-85]
- rb/lib/selenium/webdriver/remote/bidi_bridge.rb[96-98]
## Recommended Fix
Track when `close` closes the last window. On subsequent navigation, obtain the WebDriver window handle to let the remote end return its window or session error before attempting a BiDi command; retain cached-handle navigation while the socket is open. Add a last-window navigation regression test.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
✅ Linked PRs:
  🌐 PR #17785
Review mode: Auto: ⚖️ Balanced: Core navigation state and error semantics create meaningful runtime risk.

Grey Divider

Tip of the day
💡 Did you know, you can keep summaries lean with Findings visible per group, which tucks the rest behind a View link

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@titusfortner
titusfortner merged commit cfab900 into SeleniumHQ:trunk Nov 23, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-rb Ruby Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants