[py] Add expect_* context managers for BiDi events#17653
Open
AutomatedTester wants to merge 1 commit into
Open
[py] Add expect_* context managers for BiDi events#17653AutomatedTester wants to merge 1 commit into
AutomatedTester wants to merge 1 commit into
Conversation
Adds a Subscription primitive to the BiDi event manager that registers
its event handler at creation time, so an event fired by an action
inside the with block cannot be missed:
with driver.network.expect_response("**/api/**") as response_info:
driver.find_element(By.ID, "load").click()
response = response_info.value
New high-level methods generated via the enhancements manifest:
- network.expect_request / network.expect_response (URL glob or
predicate filtering, observational Request/Response wrappers)
- script.expect_console_message (predicate over ConsoleMessage)
- browsing_context.expect_user_prompt (typed prompt params)
- browsing_context.expect_download, correlating downloadWillBegin and
downloadEnd by navigation id into a new Download object with path(),
save_as() and failure()
The event manager gains a raw=True option so expect_* handlers receive
the full wire-level params instead of the generated dataclasses that
drop fields like request/response.
Includes 25 unit tests and 6 browser-based integration tests.
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.
Motivation and Context
Waiting for a BiDi event today requires registering a callback, performing the action, polling with
WebDriverWait, and removing the callback by hand — and if the event fires between the action and the first poll registration, it can be missed entirely. This PR adds Playwright-styleexpect_*context managers that register the event handler before the triggering action runs, eliminating that race and the boilerplate:What's added
Core primitive (
py/private/_event_manager.py):Subscription— queue-based capture of the first matching event,.wait(timeout)/.value,cancel(), context-manager protocol that waits for the event on cleanwith-block exit (raisingTimeoutExceptionif it never arrives), idempotent lock-guarded detach._EventManager.expect(...)plus araw=Trueoption onadd_event_handlerso handlers can receive the full wire-level params dict where the generated event dataclasses drop fields (e.g.request/response). Not exposed on the generated modules' public API.High-level methods (generated via the enhancements manifest):
driver.network.expect_request(url_or_predicate)/expect_response(url_or_predicate)— URL glob (*,**,?) or predicate filtering, wrapping the existingRequest/Responseclasses (observational; no intercept is created).driver.script.expect_console_message(predicate)— built on the existingLogHandlerRegistry, capturesConsoleMessage.driver.browsing_context.expect_user_prompt(predicate)— captures typedUserPromptOpenedParameters; the result's.contextfeeds straight intohandle_user_prompt().driver.browsing_context.expect_download()— correlatesdownloadWillBegin/downloadEndby navigation id into a newDownloadobject withsuggested_filename,path(),save_as()andfailure().Types of changes
No existing public signatures changed; all additions are new methods.
Testing
py/test/unit/.../bidi_expect_events_tests.py) covering capture, glob/predicate filtering, timeout (includingtimeout=0), predicate exceptions, cached value, idempotent/concurrent cancel, detach-after-capture, cross-thread wait, and download correlation/cleanup.bidi_network_tests.py,bidi_script_tests.pyandbidi_browsing_context_tests.py— all passing on Chrome locally (expect_downloadisxfail_firefox, matching the existing download tests).bidi_network/bidi_scriptunit suites pass;./scripts/format.shclean.