Skip to content

Commit 7002e0e

Browse files
committed
Test PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT setting
1 parent 8442e6b commit 7002e0e

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,5 @@ class ScrollSpider(scrapy.Spider):
228228
page = response.meta["playwright_page"]
229229
await page.screenshot(options={"path": "quotes.png", "fullPage": True})
230230
yield {"quote_count": len(response.css("div.quote"))} # quotes from several pages
231+
await page.close()
231232
```

tests/mockserver.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import sys
3+
import time
34
from http.server import HTTPServer, BaseHTTPRequestHandler
45
from pathlib import Path
56
from subprocess import Popen, PIPE
@@ -28,19 +29,31 @@ def urljoin(self, url):
2829
return urljoin("http://{}:{}".format(self.address, self.port), url)
2930

3031

31-
class _PostRequestHandler(BaseHTTPRequestHandler):
32+
class _RequestHandler(BaseHTTPRequestHandler):
3233
def do_POST(self):
34+
"""
35+
Echo back the request body
36+
"""
3337
content_length = int(self.headers["Content-Length"])
3438
body = self.rfile.read(content_length)
3539
self.send_response(200)
3640
self.end_headers()
3741
self.wfile.write(b"Request body: ")
3842
self.wfile.write(body)
3943

44+
def do_GET(self):
45+
"""
46+
Take a long time to reply
47+
"""
48+
time.sleep(3)
49+
self.send_response(200)
50+
self.end_headers()
51+
self.wfile.write(b"Hello world!")
52+
4053

41-
class PostMockServer:
54+
class MockServer:
4255
def __enter__(self):
43-
self.httpd = HTTPServer(("127.0.0.1", 0), _PostRequestHandler)
56+
self.httpd = HTTPServer(("127.0.0.1", 0), _RequestHandler)
4457
self.address, self.port = self.httpd.server_address
4558
self.thread = Thread(target=self.httpd.serve_forever)
4659
self.thread.start()

tests/test_playwright_requests.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from scrapy_playwright.handler import ScrapyPlaywrightDownloadHandler
1313
from scrapy_playwright.page import PageCoroutine as PageCoro
1414

15-
from tests.mockserver import PostMockServer, StaticMockServer
15+
from tests.mockserver import MockServer, StaticMockServer
1616

1717

1818
def get_mimetype(file):
@@ -57,7 +57,7 @@ async def test_post_request(self):
5757
)
5858
await handler._launch_browser()
5959

60-
with PostMockServer() as server:
60+
with MockServer() as server:
6161
req = FormRequest(
6262
server.urljoin("/"), meta={"playwright": True}, formdata={"foo": "bar"}
6363
)
@@ -132,6 +132,25 @@ async def test_page_coroutine_infinite_scroll(self):
132132

133133
await handler.browser.close()
134134

135+
@pytest.mark.asyncio
136+
async def test_timeout(self):
137+
handler = ScrapyPlaywrightDownloadHandler(
138+
get_crawler(
139+
settings_dict={
140+
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
141+
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 1000,
142+
}
143+
)
144+
)
145+
await handler._launch_browser()
146+
147+
with MockServer() as server:
148+
req = Request(server.urljoin("/index.html"), meta={"playwright": True})
149+
with pytest.raises(TimeoutError):
150+
await handler._download_request(req, Spider("foo"))
151+
152+
await handler.browser.close()
153+
135154
@pytest.mark.asyncio
136155
async def test_context_args(self):
137156
handler = ScrapyPlaywrightDownloadHandler(

0 commit comments

Comments
 (0)