Skip to content

[py] Add high-level BiDi network request handler API#17619

Merged
AutomatedTester merged 3 commits into
trunkfrom
py-bidi-network-request-handlers
Jun 4, 2026
Merged

[py] Add high-level BiDi network request handler API#17619
AutomatedTester merged 3 commits into
trunkfrom
py-bidi-network-request-handlers

Conversation

@AutomatedTester
Copy link
Copy Markdown
Member

🔗 Related Issues

💥 What does this PR do?

Adds the doc-aligned high-level request interception API to driver.network (Phase 1 of the BiDi Protocol API Design):

  • add_request_handler(callback) / add_request_handler(url_patterns, callback) with glob URL patterns (*, **, ?) — translated to browser-side BiDi UrlPatterns where expressible, with automatic fallback to intercept-all plus Python-side matching
  • Request now exposes method, headers, cookies, body, resource_type, plus fail(), provide_response(status, headers, body) and set_url/set_method/set_headers/set_cookies/set_body mutators
  • After all matching handlers run, the outcome is reconciled into exactly one BiDi command per request: fail > provideResponse > continueRequest with mutations > continueRequest — observer-only handlers no longer stall the page, and handler exceptions are logged without hanging the request
  • Reconciliation only touches requests blocked by this registry's intercepts (checked via the event's intercepts field), so legacy handlers are unaffected

🔧 Implementation Notes

  • The implementation lives in a new static helper module py/private/_network_handlers.py, staged verbatim into the generated selenium.webdriver.common.bidi package via create-bidi-src extra_srcs (same mechanism as _event_manager.py). This keeps the logic lintable, formattable and unit-testable as real code instead of string blobs in the enhancement manifest.
  • The enhancement manifest now only carries thin glue: a re-export import (so bidi.network.Request stays importable), registry init, and polymorphic dispatch in add_request_handler/remove_request_handler.
  • Backwards compatibility: the legacy phase-based forms (add_request_handler("before_request", cb, url_patterns=[...]), two-arg remove_request_handler) are unchanged; unknown event-name strings still raise ValueError (a heuristic distinguishes URL globs from event-name tokens). All pre-existing unit tests pass unmodified in behavior.

🤖 AI assistance

  • AI assisted (complete below)
    • Tool(s): Claude Code
    • What was generated: _network_handlers.py, manifest/BUILD glue, and unit tests, from the BiDi Protocol API Design doc
    • I reviewed all AI output and can explain the change

💡 Additional Considerations

  • High-risk area: wire-level network interception semantics — new reconciliation applies to the new-style handlers only; legacy paths are preserved byte-for-byte.
  • Follow-up phases planned: response handlers (add_response_handler), callback-based authentication handlers with cancel(), extra-headers API, and Script module alignment.
  • Cross-binding parity (Java/Ruby/JS) for the doc-aligned naming is follow-up work.
  • A live-browser test in py/test/selenium/webdriver/common/bidi_network_tests.py is a sensible follow-up; this PR covers the logic with 21 new unit tests (bazel test //py:unit → 27/27 pass).

🔄 Types of changes

  • New feature (non-breaking change which adds functionality and tests!)

Adds the doc-aligned request interception API to driver.network:

- add_request_handler(callback) / add_request_handler(url_patterns, callback)
  with glob URL patterns (*, **, ?) translated to BiDi UrlPatterns where
  possible, falling back to Python-side matching
- Request exposes method, headers, cookies, body and resource_type, plus
  fail(), provide_response(status, headers, body) and
  set_url/set_method/set_headers/set_cookies/set_body mutators
- After all matching handlers run, the outcome is reconciled into a single
  BiDi command (fail > provideResponse > continueRequest with mutations >
  continueRequest), so observer-only handlers no longer stall the page
- Implementation lives in py/private/_network_handlers.py (staged into the
  generated bidi package via create-bidi-src extra_srcs) so it is lintable
  and unit-testable; the enhancement manifest only carries thin glue
- Legacy phase-based add_request_handler/remove_request_handler forms are
  unchanged and still pass their existing tests
@selenium-ci selenium-ci added C-py Python Bindings B-build Includes scripting, bazel and CI integrations labels Jun 3, 2026
Integration tests mirroring the BiDi API design doc examples: observing
requests, failing by URL pattern, stubbing responses, mutating headers and
URL, multi-handler reconciliation precedence, pattern scoping, and handler
removal. Verified locally against Chrome and Firefox (17 passed, 1 skipped
each).
@AutomatedTester AutomatedTester merged commit bcafedd into trunk Jun 4, 2026
39 of 41 checks passed
@AutomatedTester AutomatedTester deleted the py-bidi-network-request-handlers branch June 4, 2026 07:49
@diemol
Copy link
Copy Markdown
Member

diemol commented Jun 4, 2026

Adds the doc-aligned high-level request interception API to driver.network (Phase 1 of the BiDi Protocol API Design):

Which doc?

@AutomatedTester
Copy link
Copy Markdown
Member Author

Adds the doc-aligned high-level request interception API to driver.network (Phase 1 of the BiDi Protocol API Design):

Which doc?

https://docs.google.com/document/d/1icS4UmN6-x6LMENux7d9MDVpD30OQ9ZcjNIYLL94vFo/edit?tab=t.pa5y6ld2l59k#heading=h.620lcimekkqc that was shared from @titusfortner

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

Labels

B-build Includes scripting, bazel and CI integrations C-py Python Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants