[py] Implement add_request_handler
User description
Description
Implement add_request_handler as described in #13993.
Add CDDL structures generated from the BIDI specs
Add an asynchronous navigation function driver.network.get
Motivation and Context
#13993
Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist
- [x] I have read the contributing document.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
PR Type
Enhancement, Tests
Description
- Implemented a new feature to handle network requests using BiDi protocol in Selenium.
- Added multiple data classes for network operations with JSON serialization and deserialization.
- Integrated network request handling into the WebDriver class.
- Added tests to verify the functionality of the
add_request_handlermethod.
Changes walkthrough 📝
| Relevant files | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Enhancement |
| ||||||||
| Tests |
|
💡 PR-Agent usage: Comment
/help "your question"on any pull request to receive relevant information
PR Reviewer Guide 🔍
Here are some key observations to aid the review process:
| ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪ |
| 🧪 PR contains tests |
| 🔒 No security concerns identified |
| ⚡ Recommended focus areas for review Code Complexity Error Handling Test Coverage |
PR Code Suggestions ✨
Explore these optional code suggestions:
| Category | Suggestion | Score |
| Enhancement |
Expand test coverage with parameterized tests for different URL scenariosConsider adding more comprehensive test cases to cover different scenarios and edge py/test/selenium/webdriver/common/bidi_network_tests.py [22-40]
Suggestion importance[1-10]: 8Why: Expanding test coverage with parameterized tests is a strong suggestion that enhances the robustness of the test suite by covering multiple scenarios and edge cases, ensuring the functionality is thoroughly validated. | 8 |
Add error handling and logging to improve robustness of request handlingConsider adding error handling and logging to the py/selenium/webdriver/remote/network.py [33-41]
Suggestion importance[1-10]: 7Why: Adding error handling and logging to the | 7 | |
Implement lazy initialization with error checking for the Network objectConsider implementing lazy initialization for the py/selenium/webdriver/remote/webdriver.py [1085-1090]
Suggestion importance[1-10]: 6Why: The suggestion to add error checking for the WebSocket URL during lazy initialization of the | 6 | |
Implement proper resource management for network request handlersConsider using a context manager for the WebSocket connection to ensure proper py/selenium/webdriver/remote/network.py [22-31]
Suggestion importance[1-10]: 5Why: The suggestion to use a context manager for the WebSocket connection is already implemented in the existing code. However, the addition of a lambda function to remove the listener is a useful enhancement for resource management, though it is not critical. | 5 |
💡 Need additional feedback ? start a PR chat
Currently, the add_listener function is blocking, meaning that page can only be refreshed via the browser itself. However, when navigating using the browser, requests are properly intercepted and modified. I wanted to open the PR for feedback and suggestions.
It was driver.get that was the blocking operation. Added network.get for asynchronous navigation
bidi_network_tests.py was throwing errors because usage of | operator for merging dictionaries was introduced in Python 3.9 while the tests were using 3.8. It has been fixed
All local chrome- tests are passing
@VietND96 May the workflow be triggered again?
@AutomatedTester I would really appreciate your feedback on this. In particular, what are your thoughts on
- Using BiDi objects + websocket vs json + HTTP
- Ideally, there should be a generic class and each object would only have the type definitions, but I'm not sure where to put the generic class
- Using trio/nursery vs plain async functions
- I think being able to call
await add_request_handlerwould be a lot more elegant, but I think it would require more changes to existing modules
- I think being able to call
Remote tests were timing out due to requesting external URLs. Changing it to internal ones in pages solved it
Factored all the repeated functions into bidi.py class. Tests are still passing (locally)
run ./scripts/format.sh to help with your formatting
@shbenzer Aha! Thanks!
bidi_tests.py and upload_tests.py are flaky - I've got PRs up to fix them - so don't worry about those results
@shbenzer Perfect! That explains a lot :confetti_ball:
While adding support for multiple intercepts, noticed that "paused" cached requests are served without waiting for continue request. Not sure if this is intended (from browser end).
Since bidi network.setCacheBehavior is not implemented yet, will disable caching with cdp for now.
Closing this PR to open a new one (#14738) from a different account for better access management. Thank you for your feedbacks!