feat: Add page lifecycle hooks to BrowserPool#1791
Open
Mantisus wants to merge 2 commits intoapify:masterfrom
Open
feat: Add page lifecycle hooks to BrowserPool#1791Mantisus wants to merge 2 commits intoapify:masterfrom
BrowserPool#1791Mantisus wants to merge 2 commits intoapify:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds four page lifecycle hooks to BrowserPool — pre_page_create_hook, post_page_create_hook, pre_page_close_hook, and post_page_close_hook — registered as decorators, aligning Python's BrowserPool closer to the JS Crawlee browser pool API as described in issue #1741.
Changes:
- Four hook registration methods added to
BrowserPool, each appending user-provided async callables to internal lists that are executed at the corresponding lifecycle point. - Close hooks are implemented by monkey-patching
page.closevia_override_page_closeafter page creation. - Comprehensive tests covering individual hook invocation, execution order, argument validation, and multiple hook registration.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/crawlee/browsers/_browser_pool.py |
Adds hook storage lists, _execute_hooks helper, _override_page_close for close hooks, and four public hook registration methods with docstrings. |
tests/unit/browsers/test_browser_pool.py |
Adds six tests covering each hook type, execution order, and multiple hook registration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+386
to
+394
| if self._pre_page_close_hooks or self._post_page_close_hooks: | ||
| original_close = crawlee_page.page.close | ||
|
|
||
| async def close_with_hooks(*args: Any, **kwargs: Any) -> None: | ||
| await self._execute_hooks(self._pre_page_close_hooks, crawlee_page, browser_controller) | ||
| await original_close(*args, **kwargs) | ||
| await self._execute_hooks(self._post_page_close_hooks, crawlee_page.id, browser_controller) | ||
|
|
||
| crawlee_page.page.close: Callable[..., Awaitable[None]] = close_with_hooks |
Collaborator
Author
There was a problem hiding this comment.
It is not expected that a closing hook will be added after the page has already been opened.
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.
Description
Add four page lifecycle hooks to
BrowserPoolregistered as decorators:pre_page_create_hook— called before page creation;browser_new_context_optionsis mutable, so the hook can affect how the page context is configured.post_page_create_hook— called after page creation.pre_page_close_hook— called before page close.post_page_close_hook— called after page close.Issues
Testing
BrowserPool.