diff --git a/src/crawlee/crawlers/_basic/_basic_crawler.py b/src/crawlee/crawlers/_basic/_basic_crawler.py index 53c37416e0..2553467124 100644 --- a/src/crawlee/crawlers/_basic/_basic_crawler.py +++ b/src/crawlee/crawlers/_basic/_basic_crawler.py @@ -840,7 +840,7 @@ async def add_requests( wait_for_all_requests_to_be_added_timeout=wait_for_all_requests_to_be_added_timeout, ) - async def _use_state( + async def use_state( self, default_value: dict[str, JsonSerializable] | None = None, ) -> dict[str, JsonSerializable]: @@ -1421,7 +1421,7 @@ async def __run_task_function(self) -> None: add_requests=result.add_requests, push_data=result.push_data, get_key_value_store=result.get_key_value_store, - use_state=self._use_state, + use_state=self.use_state, log=self._logger, ) self._context_result_map[context] = result diff --git a/tests/unit/crawlers/_basic/test_basic_crawler.py b/tests/unit/crawlers/_basic/test_basic_crawler.py index 026f905275..c0f7b808e8 100644 --- a/tests/unit/crawlers/_basic/test_basic_crawler.py +++ b/tests/unit/crawlers/_basic/test_basic_crawler.py @@ -815,6 +815,20 @@ async def handler(context: BasicCrawlingContext) -> None: assert value == {'hello': 'world'} +async def test_crawler_use_state() -> None: + crawler = BasicCrawler() + + await crawler.use_state({'hello': 'world'}) + + @crawler.router.default_handler + async def handler(context: BasicCrawlingContext) -> None: + # The state set by the crawler must be available in the context of the request handler + state = await context.use_state() + assert state == {'hello': 'world'} + + await crawler.run(['https://hello.world']) + + async def test_context_use_state_crawlers_share_state() -> None: async def handler(context: BasicCrawlingContext) -> None: state = await context.use_state({'urls': []})