Skip to content

Commit f17bf31

Browse files
authored
fix: Allow config change on PlaywrightCrawler (#1186)
### Description Make it possible to apply `Configuration` to the Playwright crawler. Added a test. ### Issues - Closes: #1185
1 parent bd4b940 commit f17bf31

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/crawlee/crawlers/_playwright/_playwright_crawler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pydantic import ValidationError
99
from typing_extensions import NotRequired, TypedDict, TypeVar
1010

11+
from crawlee import service_locator
1112
from crawlee._request import Request, RequestOptions
1213
from crawlee._utils.blocked import RETRY_CSS_SELECTORS
1314
from crawlee._utils.docs import docs_group
@@ -120,6 +121,10 @@ def __init__(
120121
This option should not be used if `browser_pool` is provided.
121122
kwargs: Additional keyword arguments to pass to the underlying `BasicCrawler`.
122123
"""
124+
configuration = kwargs.pop('configuration', None)
125+
if configuration is not None:
126+
service_locator.set_configuration(configuration)
127+
123128
if browser_pool:
124129
# Raise an exception if browser_pool is provided together with other browser-related arguments.
125130
if any(

tests/unit/crawlers/_playwright/test_playwright_crawler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import pytest
1313

14-
from crawlee import ConcurrencySettings, HttpHeaders, Request, RequestTransformAction, SkippedReason
14+
from crawlee import ConcurrencySettings, HttpHeaders, Request, RequestTransformAction, SkippedReason, service_locator
15+
from crawlee.configuration import Configuration
1516
from crawlee.crawlers import PlaywrightCrawler
1617
from crawlee.fingerprint_suite import (
1718
DefaultFingerprintGenerator,
@@ -689,3 +690,11 @@ async def request_handler(context: PlaywrightCrawlingContext) -> None:
689690
assert check_data['send_request']['user-agent'] == 'My User-Agent'
690691

691692
assert check_data['default'] != check_data['send_request']
693+
694+
695+
async def test_overwrite_configuration() -> None:
696+
"""Check that the configuration is allowed to be passed to the Playwrightcrawler."""
697+
configuration = Configuration(default_dataset_id='my_dataset_id')
698+
PlaywrightCrawler(configuration=configuration)
699+
used_configuration = service_locator.get_configuration()
700+
assert used_configuration is configuration

0 commit comments

Comments
 (0)