Skip to content

feat: support network event listening on Page#37

Merged
padamson merged 5 commits intopadamson:mainfrom
AtaLuZiK:network-events
Mar 13, 2026
Merged

feat: support network event listening on Page#37
padamson merged 5 commits intopadamson:mainfrom
AtaLuZiK:network-events

Conversation

@AtaLuZiK
Copy link
Copy Markdown
Contributor

@AtaLuZiK AtaLuZiK commented Mar 9, 2026

  • Add on_request, on_response, on_request_finished, and on_request_failed methods to Page
  • Implement event dispatching logic in BrowserContext via dispatch_request_event and dispatch_response_event
  • Enhance ResponseObject with status, status_text, and url accessors
  • Refactor event subscription in BrowserContext to use Channel::update_subscription
  • Add update_subscription helper method to Channel
  • Add integration test test_page_support_network_events to verify network event handling

@padamson
Copy link
Copy Markdown
Owner

Thanks for this contribution! The implementation follows existing patterns well. A couple of design points to discuss:

Unconditional network event subscriptions

The PR subscribes to request, requestfailed, requestfinished, and response for every BrowserContext, even when no handlers are registered. This adds overhead for all users. Playwright-python handles this lazily, subscribing only when a handler is actually registered. Would you consider deferring subscription to when on_request/on_response/etc. are called? I'm happy to modify before merge if you'd like.

Test event ordering assumption

The test asserts exact event ordering with events.get(0), events.get(1), events.get(2). Since events are dispatched via tokio::spawn, the ordering between request, response, and requestFinished isn't strictly guaranteed. A set-based assertion (or at least a comment explaining why the order is expected to be deterministic) would make this more robust. Again, if you're ok with that, I can make the change before merge.

@AtaLuZiK
Copy link
Copy Markdown
Contributor Author

Thank you for the reminder and suggestions! Since I'm just getting started with playwright through my own toy project, I wasn't fully aware of some details. After reviewing the implementation in playwright-dotnet, I agree that subscriptions should be handled lazily. I've spent some time revising the code accordingly.

Regarding the test event ordering, that was an oversight on my part. I don't have a better solution at the moment (perhaps BrowserContext::on_event needs to be async?), so for now I can only sort the actual values before comparing them. I've added comments to explain this situation.

linmohc and others added 5 commits March 13, 2026 06:33
- Add `on_request`, `on_response`, `on_request_finished`, and `on_request_failed` methods to `Page`
- Enhance `ResponseObject` with `status`, `status_text`, and `url` accessors
- Add `update_subscription` helper method to `Channel`
- Add integration test `test_page_support_network_events` to verify network event handling
- Remove automatic subscription to network events in BrowserContext
- Update Page handlers to enable subscription only when the first handler is registered
- Move network event tests to page_event_network.rs
- Add test case for iframe network events
- Add frame_handler in TestServer for iframe testing
…ted tests have been moved to `crates/playwright/tests/integration/page_event_network.rs`
@padamson padamson enabled auto-merge (squash) March 13, 2026 10:50
@padamson padamson merged commit 8aafc72 into padamson:main Mar 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants