Skip to content

feat(network): support network mocking on Firefox via WebDriver BiDi … - #199

Merged
shossain786 merged 3 commits into
seleniumboot:masterfrom
PUSHPAK-JAISWAL:bidi-network-mocking
Oct 25, 2026
Merged

shossain786 merged 3 commits into
seleniumboot:masterfrom
PUSHPAK-JAISWAL:bidi-network-mocking

Conversation

@PUSHPAK-JAISWAL

Copy link
Copy Markdown
Contributor

What & why

Closes #196

networkMock().stub(...) previously worked exclusively on Chrome and Edge via CDP (Fetch.enable). On Firefox, stubs were silently ignored because driver instanceof ChromiumDriver evaluated to false, causing tests relying on mocked APIs to produce false positives or fail incorrectly.

This PR wires up WebDriver BiDi network interception (org.openqa.selenium.bidi.module.Network) under the hood when the driver implements HasBiDi (including Firefox and BiDi-enabled Chromium). If BiDi is unavailable or unconfigured (such as Chrome sessions without a BiDi WebSocket), it cleanly falls back to CDP for ChromiumDriver. Unsupported drivers (such as Safari) log a warning and continue without breaking.

Key changes:

  • Integrated org.openqa.selenium.bidi.module.Network with BEFORE_REQUEST_SENT interception phase.
  • Handled request matching, custom HTTP status, response body via BytesValue, content-type header, and delays.
  • Handled non-matching requests with bidiNetwork.continueRequest(...) and ignored unblocked events.
  • Updated clear() to remove BiDi intercepts, close BiDi network listeners, and disable CDP fetch sessions.
  • Added getters to StubBuilder for inspection while preserving the entire existing fluent API contract.
  • Added comprehensive unit tests in NetworkMockTest covering BiDi matching/non-matching/delay/clear, CDP fallback on BiDi exception, direct CDP path, unsupported browser warnings, null driver safety, and cross-browser consistency (sameStubTest_passesOnChromeAndFirefox).
  • Updated CHANGELOG.md and docs-site/docs/changelog.md.

Type of change

  • Bug fix
  • New feature (opt-in, aligned with the framework philosophy)
  • Documentation
  • Internal refactor / test coverage

Checklist

  • All existing tests pass (mvn test: 823 passed, 0 failures, 0 skipped)
  • New behaviour is covered by unit tests (26 unit tests in NetworkMockTest)
  • No new external dependencies introduced without prior discussion
  • Code follows existing conventions (no new frameworks, minimal abstraction)
  • No breaking changes to @SeleniumBootApi public APIs (or a major version bump is justified below)
  • Docs updated if user-facing behaviour changed (docs-site/)

Notes for reviewers

  • Zero Breaking API Changes: networkMock().stub(...), returnJson(...), returnBody(...), returnStatus(...), delay(...), and clear() retain their exact signatures and behavior.
  • BiDi First with CDP Fallback: Drivers implementing HasBiDi are given preference for BiDi interception. If bidiFactory.create(driver) throws (such as BiDiException: Check if this browser version supports BiDi and if the 'webSocketUrl: true' capability is set), it catches the error and cleanly falls back to CDP for ChromiumDriver.
  • Testing Approach: Added package-private BiDiNetworkFactory hook (bidiFactory = Network::new by default) so the entire BiDi lifecycle, request interception, header/body construction, delay, and teardown can be verified with Mockito without spinning up real browser sessions.

@shossain786

Copy link
Copy Markdown
Contributor

Thanks @PUSHPAK-JAISWAL, the BiDi-first / CDP-fallback design looks right. Before merge:

  1. Changelog: entries are under v3.9.0, which is already released. Please move them to a new section. Also drop "removing the hard dependency on a single pinned CDP version"; the fallback still uses the v153 classes.
  2. NetworkMock is @SeleniumBootApi: please don't expose the test hooks (setBiDiFactory, resetBiDiFactory, isAttached, isBiDiActive, isCdpActive, getStubs) as public API. Package-private or a test-support class instead.
  3. Everything is tested with mocks. Could you run a stub once on real Firefox and confirm it works?
  4. Handler errors are logged at FINE only, so a failed request could hang silently. Consider logging at WARNING.

@PUSHPAK-JAISWAL

Copy link
Copy Markdown
Contributor Author

Changelog placement: Entries moved under a new Unreleased section; dropped reference to removing pinned CDP version since fallback still uses CDP v153 classes.
Public API cleanliness: All test hooks (setBiDiFactory, resetBiDiFactory, isAttached, isBiDiActive, isCdpActive, getStubs, and BiDiNetworkFactory) are package-private. Added NetworkMockTestSupport under src/test/java package com.seleniumboot.network for test access without exposing internals to @SeleniumBootApi.
Handler error visibility: Request handler exceptions in handleBiDiRequest and handleCdpRequest are logged at WARNING with the offending URL.
Live Firefox Verification: Added FirefoxNetworkMockLiveTest which executes stubs on real headless Firefox using WebDriver BiDi. Both JSON response stubbing and HTTP status code stubbing were verified live and passed (skips gracefully if Firefox is not present).

@shossain786
shossain786 merged commit 7fa892f into seleniumboot:master Oct 25, 2026
2 checks passed
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.

Network mocking on Firefox via BiDi

2 participants