Skip to content

Enable BiDi support for connect-existing mode#45

Closed
MayCXC wants to merge 1 commit intomozilla:mainfrom
MayCXC:connect-existing-bidi
Closed

Enable BiDi support for connect-existing mode#45
MayCXC wants to merge 1 commit intomozilla:mainfrom
MayCXC:connect-existing-bidi

Conversation

@MayCXC
Copy link
Copy Markdown
Contributor

@MayCXC MayCXC commented Mar 27, 2026

Summary

--connect-existing mode currently disables all BiDi-dependent features (console events, network events) because selenium-webdriver's createSession() hangs when trying to auto-upgrade to BiDi with an existing Firefox instance.

This PR enables BiDi in connect-existing mode by:

  1. Requesting webSocketUrl: true in the session capabilities sent to geckodriver
  2. Reading the webSocketUrl from the session response (set by Firefox's Remote Agent)
  3. Adding a getBidi() method to GeckodriverHttpDriver that opens a WebSocket to that URL and provides the subscribe/socket interface that ConsoleEvents and NetworkEvents already use

The existing hasBidi check in index.ts ('getBidi' in driver) naturally picks up the new method, so console and network event collection work without further changes.

Behavior

  • Launch mode: No change. Continues to use selenium-webdriver with enableBidi().
  • Connect-existing with --remote-debugging-port: BiDi features (console, network) now work.
  • Connect-existing without --remote-debugging-port: getBidi() throws a clear error. All non-BiDi features continue to work as before.

Changes

  • src/firefox/core.ts: 82 insertions, 6 deletions (all in GeckodriverHttpDriver)

Test plan

  • tsc --noEmit passes
  • bun run build succeeds
  • Existing tests: 220 pass (16 pre-existing failures unrelated to this change)
  • Manual: --connect-existing with Firefox started via firefox --marionette --remote-debugging-port 9222, verify list_console_messages and list_network_requests return data

GeckodriverHttpDriver now requests webSocketUrl: true in the session
capabilities and exposes a getBidi() method that opens a WebSocket to
Firefox's Remote Agent. This allows ConsoleEvents and NetworkEvents
(which depend on BiDi subscriptions) to work when using
--connect-existing, without going through selenium-webdriver's
createSession() which hangs during BiDi auto-upgrade.

The BiDi WebSocket is opened lazily on the first getBidi() call and
closed on quit/kill. The subscribe() helper sends session.subscribe
commands and waits for the matching response. The socket property
gives ConsoleEvents and NetworkEvents direct access to the event
stream, matching the interface they already use on the Selenium
WebDriver path.

No behavior change for launch mode (which continues to use
selenium-webdriver with enableBidi). No behavior change for
connect-existing when Firefox was not started with
--remote-debugging-port (getBidi throws a clear error).
@juliandescottes
Copy link
Copy Markdown
Collaborator

Thanks, it's great to fix the limitation with --connect-existing!

I think there will be merge conflicts with #46, but it should be easy to reconcile.

@freema
Copy link
Copy Markdown
Collaborator

freema commented Mar 28, 2026

Hey @MayCXC, thanks for the PR! We just released 0.9.0 which included the PR #46 rebase, so there are merge conflicts now — but your changes are well-scoped to GeckodriverHttpDriver so the rebase should be straightforward.

The approach looks correct — we verified there's no simpler built-in path in selenium-webdriver for this. Could you rebase on current main?

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.

3 participants