Skip to content

Commit 6875f7c

Browse files
committed
Allow PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT setting to be 0
1 parent 0f1548f commit 6875f7c

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ TWISTED_REACTOR = "twisted.internet.asyncioreactor.AsyncioSelectorReactor"
9797
The arguments passed here take precedence over the ones defined in `PLAYWRIGHT_CONTEXT_ARGS`.
9898
See the docs for [`Browser.new_context`](https://playwright.dev/python/docs/api/class-browser#browsernew_contextkwargs).
9999

100-
* `PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT` (type `Optional[int]`, default `None`)
100+
* `PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT` (type `Optional[float]`, default `None`)
101101

102102
The timeout used when requesting pages by Playwright. If `None` or unset,
103103
the default value will be used (30000 ms at the time of writing this).

scrapy_playwright/handler.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import warnings
44
from collections import defaultdict
5+
from contextlib import suppress
56
from time import time
67
from typing import Callable, Dict, Optional, Type, TypeVar
78
from urllib.parse import urlparse
@@ -65,9 +66,12 @@ def __init__(self, crawler: Crawler) -> None:
6566

6667
self.browser_type: str = crawler.settings.get("PLAYWRIGHT_BROWSER_TYPE") or "chromium"
6768
self.launch_options: dict = crawler.settings.getdict("PLAYWRIGHT_LAUNCH_OPTIONS") or {}
68-
self.default_navigation_timeout: Optional[int] = (
69-
crawler.settings.getint("PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT") or None
70-
)
69+
self.default_navigation_timeout: Optional[float] = None
70+
if "PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT" in crawler.settings:
71+
with suppress(TypeError, ValueError):
72+
self.default_navigation_timeout = float(
73+
crawler.settings.get("PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT")
74+
)
7175

7276
default_context_kwargs: dict = {}
7377
if "PLAYWRIGHT_CONTEXT_ARGS" in crawler.settings:
@@ -115,7 +119,7 @@ async def _create_browser_context(self, name: str, context_kwargs: dict) -> Brow
115119
context.on("close", self._make_close_browser_context_callback(name))
116120
logger.debug("Browser context started: '%s'", name)
117121
self.stats.inc_value("playwright/context_count")
118-
if self.default_navigation_timeout:
122+
if self.default_navigation_timeout is not None:
119123
context.set_default_navigation_timeout(self.default_navigation_timeout)
120124
return context
121125

@@ -132,7 +136,7 @@ async def _create_page(self, request: Request) -> Page:
132136
page.on("request", _make_request_logger(context_name))
133137
page.on("request", self._increment_request_stats)
134138
self.stats.inc_value("playwright/page_count")
135-
if self.default_navigation_timeout:
139+
if self.default_navigation_timeout is not None:
136140
page.set_default_navigation_timeout(self.default_navigation_timeout)
137141
return page
138142

tests/test_playwright_requests.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,41 @@ async def test_page_coroutine_infinite_scroll(self):
117117
assert "playwright" in resp.flags
118118
assert len(resp.css("div.quote")) == 30
119119

120+
@pytest.mark.asyncio
121+
async def test_timeout_value(self):
122+
settings_dict = {
123+
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
124+
}
125+
async with make_handler(settings_dict) as handler:
126+
assert handler.default_navigation_timeout is None
127+
128+
settings_dict = {
129+
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
130+
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": None,
131+
}
132+
async with make_handler(settings_dict) as handler:
133+
assert handler.default_navigation_timeout is None
134+
135+
settings_dict = {
136+
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
137+
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 0,
138+
}
139+
async with make_handler(settings_dict) as handler:
140+
assert handler.default_navigation_timeout == 0
141+
142+
settings_dict = {
143+
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
144+
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 123,
145+
}
146+
async with make_handler(settings_dict) as handler:
147+
assert handler.default_navigation_timeout == 123
148+
settings_dict = {
149+
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
150+
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 0.5,
151+
}
152+
async with make_handler(settings_dict) as handler:
153+
assert handler.default_navigation_timeout == 0.5
154+
120155
@pytest.mark.asyncio
121156
async def test_timeout(self):
122157
settings_dict = {

0 commit comments

Comments
 (0)