[py] Add high-level BiDi network request handler API#17619
Merged
Conversation
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
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).
Member
Which doc? |
2 tasks
Member
Author
https://docs.google.com/document/d/1icS4UmN6-x6LMENux7d9MDVpD30OQ9ZcjNIYLL94vFo/edit?tab=t.pa5y6ld2l59k#heading=h.620lcimekkqc that was shared from @titusfortner |
This was referenced Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 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 matchingRequestnow exposesmethod,headers,cookies,body,resource_type, plusfail(),provide_response(status, headers, body)andset_url/set_method/set_headers/set_cookies/set_bodymutatorsinterceptsfield), so legacy handlers are unaffected🔧 Implementation Notes
py/private/_network_handlers.py, staged verbatim into the generatedselenium.webdriver.common.bidipackage viacreate-bidi-srcextra_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.bidi.network.Requeststays importable), registry init, and polymorphic dispatch inadd_request_handler/remove_request_handler.add_request_handler("before_request", cb, url_patterns=[...]), two-argremove_request_handler) are unchanged; unknown event-name strings still raiseValueError(a heuristic distinguishes URL globs from event-name tokens). All pre-existing unit tests pass unmodified in behavior.🤖 AI assistance
_network_handlers.py, manifest/BUILD glue, and unit tests, from the BiDi Protocol API Design doc💡 Additional Considerations
add_response_handler), callback-based authentication handlers withcancel(), extra-headers API, and Script module alignment.py/test/selenium/webdriver/common/bidi_network_tests.pyis a sensible follow-up; this PR covers the logic with 21 new unit tests (bazel test //py:unit→ 27/27 pass).🔄 Types of changes