-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[py][bidi]: add event handler support for browsing context #16101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
…y-bidi-bc-event
…y-bidi-bc-event
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds event handler support for the browsing context BiDi module in the Python Selenium bindings. It enables users to subscribe to various browsing context events and handle them with custom callback functions.
Key Changes
- Refactored event handling architecture from a simple dictionary to a class-based system with dedicated event classes for each event type
- Introduced
_EventManager
class to centralize event subscription management and callback tracking - Added comprehensive test coverage for all supported browsing context events including context creation/destruction, navigation events, user prompts, and downloads
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
File | Description |
---|---|
py/selenium/webdriver/common/bidi/browsing_context.py | Core implementation with new event classes, EventManager, and refactored event handling methods |
py/test/selenium/webdriver/common/bidi_browsing_context_tests.py | Comprehensive test suite covering all event types and edge cases including thread safety and multiple handlers |
Comments suppressed due to low confidence (1)
py/selenium/webdriver/common/bidi/browsing_context.py:544
- [nitpick] The error message in the ValueError exception could be more helpful by suggesting how to get available events. Consider mentioning the
get_event_names()
method.
if not event_config:
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to 086bc51
Previous suggestionsSuggestions
|
Persistent suggestions updated to latest commit 086bc51 |
|
||
def on_context_created(info): | ||
with event_lock: | ||
events_received.append(info) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a couple questions regarding test_event_handler_thread_safety(), and I apologize if any of them are dumb:
- If this test fails, is that an issue we should fix on our end, or something the developers should fix on the driver-side?
- Isn't append atomic? If so, would there not need to be another line in on_context_created() accessing the events_received list to fully ensure thread safety?
Do we import threading anywhere else, or is this a new import?EDIT: It's used in webserver.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shbenzer These are some good questions.
I inspected and realized that the test_event_handler_thread_safety()
test doesn't really tests thread safety, it just creates 3 contexts sequentially and receives their events in the list.
I wrote a test that tests it in a much better way and realized that we need to modify our _EventManager
class to support thread safety (basically add a thread lock - threading.Lock()
). So:
- Yes, thread safety needs to be supported on our end.
- Yes, currently it is atomic but we can add multiple operations (my latest commit does it)
The question now is do we add support for this? I think yes. I tried adding thread lock support for events and wrote a better thread test.
I will commit it for you to review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The EventManager code looks better. Lets break this big test into a few smaller ones so it's easier to diagnose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have divided the thread test into 4 tests, I used AI to do it (with some minor changes) since the logic is the same, so do review it :)
User description
🔗 Related Issues
💥 What does this PR do?
Adds support for event handlers for browsing context bidi module.
🔧 Implementation Notes
Replaces #15902 as discussed in #15902 (comment)
Usage:
Get a list of supported event strings:
Returns:
['context_created', 'context_destroyed', 'dom_content_loaded', 'download_will_begin', 'fragment_navigated', 'history_updated', 'load', 'navigation_aborted', 'navigation_committed', 'navigation_failed', 'navigation_started', 'user_prompt_closed', 'user_prompt_opened']
💡 Additional Considerations
🔄 Types of changes
PR Type
Enhancement
Description
Add event handler support for browsing context BiDi module
Implement 13 event classes for different browsing context events
Create
_EventManager
class for subscription and callback managementAdd comprehensive test coverage for all event types
Diagram Walkthrough
File Walkthrough
browsing_context.py
Implement event handler architecture for browsing context
py/selenium/webdriver/common/bidi/browsing_context.py
BrowsingContextEvent
with 13 specific event classes_EventManager
class for centralized event handlingget_event_names()
classmethod for available eventsbidi_browsing_context_tests.py
Add comprehensive event handler test coverage
py/test/selenium/webdriver/common/bidi_browsing_context_tests.py