forked from scrapy-plugins/scrapy-playwright
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_playwright_requests.py
More file actions
553 lines (486 loc) · 23.9 KB
/
test_playwright_requests.py
File metadata and controls
553 lines (486 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import asyncio
import json
import logging
import platform
from ipaddress import ip_address
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from playwright.async_api import (
Dialog,
Error as PlaywrightError,
Page as PlaywrightPage,
TimeoutError as PlaywrightTimeoutError,
)
from scrapy import Spider, Request, FormRequest
from scrapy_playwright.handler import DEFAULT_CONTEXT_NAME
from scrapy_playwright.page import PageMethod
from tests import allow_windows, make_handler, assert_correct_response
from tests.mockserver import MockServer, StaticMockServer
class DialogSpider(Spider):
"""A spider with a method to handle the "dialog" page event"""
name = "dialog"
def parse(self, **_kwargs) -> None:
return None
async def handle_dialog(self, dialog: Dialog) -> None:
self.dialog_message = dialog.message
await dialog.dismiss()
class MixinTestCase:
browser_type: str
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
caplog.set_level(logging.DEBUG)
self._caplog = caplog
@allow_windows
async def test_basic_response(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
meta = {"playwright": True, "playwright_include_page": True}
req = Request(server.urljoin("/index.html"), meta=meta)
resp = await handler._download_request(req, Spider("foo"))
assert_correct_response(resp, req)
assert resp.css("a::text").getall() == ["Lorem Ipsum", "Infinite Scroll"]
assert isinstance(resp.meta["playwright_page"], PlaywrightPage)
assert resp.meta["playwright_page"].url == resp.url
await resp.meta["playwright_page"].close()
@allow_windows
async def test_post_request(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
req = FormRequest(
server.urljoin("/"), meta={"playwright": True}, formdata={"foo": "bar"}
)
resp = await handler._download_request(req, Spider("foo"))
assert_correct_response(resp, req)
assert "Request body: foo=bar" in resp.text
@allow_windows
async def test_timeout_error(self):
settings_dict = {
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 100,
}
async with make_handler(settings_dict) as handler:
with MockServer() as server:
req = Request(server.urljoin("/headers?delay=1"), meta={"playwright": True})
with pytest.raises(PlaywrightTimeoutError) as excinfo:
await handler._download_request(req, Spider("foo"))
assert (
"scrapy-playwright",
logging.WARNING,
f"Closing page due to failed request: {req}"
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
) in self._caplog.record_tuples
@allow_windows
async def test_retry_page_content_still_navigating(self):
if self.browser_type != "chromium":
pytest.skip("Only Chromium seems to redirect meta tags within the same goto call")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
req = Request(server.urljoin("/redirect.html"), meta={"playwright": True})
resp = await handler._download_request(req, Spider("foo"))
assert resp.request is req
assert resp.url == server.urljoin("/index.html") # redirected
assert resp.status == 200
assert "playwright" in resp.flags
assert (
"scrapy-playwright",
logging.DEBUG,
f"Retrying to get content from page '{req.url}', error: 'Unable to retrieve"
" content because the page is navigating and changing the content.'",
) in self._caplog.record_tuples
@patch("scrapy_playwright.handler.logger")
@allow_windows
async def test_route_continue_exception(self, logger):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
scrapy_request = Request(url="https://example.org", method="GET")
spider = Spider("foo")
initial_request_done = asyncio.Event()
req_handler = handler._make_request_handler(
context_name=DEFAULT_CONTEXT_NAME,
method=scrapy_request.method,
url=scrapy_request.url,
headers=scrapy_request.headers,
body=None,
encoding="utf-8",
spider=spider,
initial_request_done=initial_request_done,
)
route = MagicMock()
playwright_request = AsyncMock()
playwright_request.url = scrapy_request.url
playwright_request.method = scrapy_request.method
playwright_request.is_navigation_request = MagicMock(return_value=True)
playwright_request.all_headers.return_value = {}
# safe error, only warn
ex = PlaywrightError("Target page, context or browser has been closed")
route.continue_.side_effect = ex
await req_handler(route, playwright_request)
logger.warning.assert_called_with(
"Failed processing Playwright request: <%s %s> exc_type=%s exc_msg=%s",
playwright_request.method,
playwright_request.url,
type(ex),
str(ex),
extra={
"spider": spider,
"context_name": DEFAULT_CONTEXT_NAME,
"scrapy_request_url": scrapy_request.url,
"scrapy_request_method": scrapy_request.method,
"playwright_request_url": playwright_request.url,
"playwright_request_method": playwright_request.method,
"exception": ex,
},
exc_info=True,
)
# unknown errors, re-raise
route.continue_.side_effect = ZeroDivisionError("asdf")
with pytest.raises(ZeroDivisionError):
await req_handler(route, playwright_request)
route.continue_.side_effect = PlaywrightError("qwerty")
with pytest.raises(PlaywrightError):
await req_handler(route, playwright_request)
@allow_windows
async def test_event_handler_dialog_callable(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
spider = DialogSpider()
req = Request(
url=server.urljoin("/index.html"),
meta={
"playwright": True,
"playwright_page_methods": [
# trigger an alert
PageMethod("evaluate", "alert('foobar');"),
],
"playwright_page_event_handlers": {
"dialog": spider.handle_dialog,
},
},
)
await handler._download_request(req, spider)
assert spider.dialog_message == "foobar"
@allow_windows
async def test_event_handler_dialog_str(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
spider = DialogSpider()
req = Request(
url=server.urljoin("/index.html"),
meta={
"playwright": True,
"playwright_page_methods": [
# trigger an alert
PageMethod("evaluate", "alert('foobar');"),
],
"playwright_page_event_handlers": {
"dialog": "handle_dialog",
},
},
)
await handler._download_request(req, spider)
assert spider.dialog_message == "foobar"
@allow_windows
async def test_event_handler_dialog_missing(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with StaticMockServer() as server:
spider = DialogSpider()
req = Request(
url=server.urljoin("/index.html"),
meta={
"playwright": True,
"playwright_page_event_handlers": {
"dialog": "missing_method",
},
},
)
await handler._download_request(req, spider)
assert (
"scrapy-playwright",
logging.WARNING,
"Spider 'dialog' does not have a 'missing_method' attribute,"
" ignoring handler for event 'dialog'",
) in self._caplog.record_tuples
assert getattr(spider, "dialog_message", None) is None
@allow_windows
async def test_response_attributes(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
req = Request(
url=server.urljoin(),
meta={"playwright": True},
)
response = await handler._download_request(req, Spider("spider_name"))
assert response.ip_address == ip_address(server.address)
@allow_windows
async def test_page_goto_kwargs_referer(self):
if self.browser_type != "chromium":
pytest.skip("referer as goto kwarg seems to work only with chromium :shrug:")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
fake_referer = server.urljoin("/fake/referer")
req = Request(
url=server.urljoin("/headers"),
meta={
"playwright": True,
"playwright_page_goto_kwargs": {"referer": fake_referer},
},
)
response = await handler._download_request(req, Spider("spider_name"))
headers = json.loads(response.css("pre::text").get())
assert headers["Referer"] == fake_referer
@allow_windows
async def test_navigation_returns_none(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer():
req = Request(url="about:blank", meta={"playwright": True})
response = await handler._download_request(req, Spider("spider_name"))
assert (
"scrapy-playwright",
logging.WARNING,
f"Navigating to {req!r} returned None, the response"
" will have empty headers and status 200",
) in self._caplog.record_tuples
assert not response.headers
assert response.status == 200
@allow_windows
async def test_abort_requests(self):
async def should_abort_request_async(request):
return request.resource_type == "image"
def should_abort_request_sync(request):
return request.resource_type == "image"
for predicate in (
lambda request: request.resource_type == "image",
should_abort_request_async,
should_abort_request_sync,
):
settings_dict = {
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
"PLAYWRIGHT_ABORT_REQUEST": predicate,
}
async with make_handler(settings_dict) as handler:
with StaticMockServer() as server:
req = Request(
url=server.urljoin("/gallery.html"),
meta={"playwright": True},
)
await handler._download_request(req, Spider("foo"))
req_prefix = "playwright/request_count"
resp_prefix = "playwright/response_count"
assert handler.stats.get_value(f"{req_prefix}/resource_type/document") == 1
assert handler.stats.get_value(f"{req_prefix}/resource_type/image") == 3
assert handler.stats.get_value(f"{resp_prefix}/resource_type/document") == 1
assert handler.stats.get_value(f"{resp_prefix}/resource_type/image") is None
assert handler.stats.get_value(f"{req_prefix}/aborted") == 3
@allow_windows
async def test_page_initialization_ok(self):
async def init_page(page, _request):
await page.set_extra_http_headers({"Extra-Header": "Qwerty"})
settings_dict = {
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
"PLAYWRIGHT_PROCESS_REQUEST_HEADERS": None,
}
async with make_handler(settings_dict) as handler:
with MockServer() as server:
req = Request(
url=server.urljoin("/headers"),
meta={"playwright": True, "playwright_page_init_callback": init_page},
)
response = await handler._download_request(req, Spider("spider_name"))
assert response.status == 200
headers = json.loads(response.css("pre::text").get())
headers = {key.lower(): value for key, value in headers.items()}
assert headers["extra-header"] == "Qwerty"
@allow_windows
async def test_page_initialization_fail(self):
async def init_page(page, _request, _missing):
await page.set_extra_http_headers({"Extra-Header": "Qwerty"})
settings_dict = {
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
"PLAYWRIGHT_PROCESS_REQUEST_HEADERS": None,
}
async with make_handler(settings_dict) as handler:
with MockServer() as server:
req = Request(
url=server.urljoin("/headers"),
meta={"playwright": True, "playwright_page_init_callback": init_page},
)
response = await handler._download_request(req, Spider("spider_name"))
assert response.status == 200
headers = json.loads(response.css("pre::text").get())
headers = {key.lower(): value for key, value in headers.items()}
assert "extra-header" not in headers
for entry in self._caplog.record_tuples:
if "Page init callback exception for" in entry[2]:
assert entry[0] == "scrapy-playwright"
assert entry[1] == logging.WARNING
assert f"[Context=default] Page init callback exception for {req!r}" in entry[2]
assert "init_page() missing 1 required positional argument: '_missing'" in entry[2]
@allow_windows
async def test_redirect(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
req = Request(
url=server.urljoin("/redirect2"),
meta={"playwright": True},
)
response = await handler._download_request(req, Spider("spider_name"))
assert response.url == server.urljoin("/headers")
assert response.meta["redirect_times"] == 2
assert response.meta["redirect_reasons"] == [302, 301]
assert response.meta["redirect_urls"] == [
server.urljoin("/redirect2"),
server.urljoin("/redirect"),
]
@allow_windows
async def test_logging_record_spider(self):
"""Make sure at least one log record has the spider as an attribute
(records sent before opening the spider will not have it).
"""
spider = Spider("spider_name")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
await handler._download_request(req, spider)
assert any(getattr(rec, "spider", None) is spider for rec in self._caplog.records)
@allow_windows
@patch("scrapy_playwright.handler._make_request_logger")
async def test_request_logger_disabled(self, make_request_logger: MagicMock):
self._caplog.set_level(logging.DEBUG + 1, "scrapy-playwright")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
await handler._download_request(req, Spider("foo"))
debug_message = (
f"[Context=default] Request: <{req.method} {req.url}> (resource type: document)"
)
assert not any(rec.message == debug_message for rec in self._caplog.records)
make_request_logger.assert_not_called()
@allow_windows
async def test_request_logger_enabled(self):
self._caplog.set_level(logging.DEBUG, "scrapy-playwright")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
await handler._download_request(req, Spider("foo"))
debug_message = (
f"[Context=default] Request: <{req.method} {req.url}> (resource type: document)"
)
assert any(rec.message == debug_message for rec in self._caplog.records)
@allow_windows
@patch("scrapy_playwright.handler._make_response_logger")
async def test_response_logger_disabled(self, make_response_logger: MagicMock):
self._caplog.set_level(logging.DEBUG + 1, "scrapy-playwright")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
response = await handler._download_request(req, Spider("foo"))
debug_message = f"[Context=default] Response: <{response.status} {response.url}>"
assert not any(rec.message == debug_message for rec in self._caplog.records)
make_response_logger.assert_not_called()
@allow_windows
async def test_response_logger_enabled(self):
self._caplog.set_level(logging.DEBUG, "scrapy-playwright")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
request = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
response = await handler._download_request(request, Spider("foo"))
debug_message = f"[Context=default] Response: <{response.status} {response.url}>"
assert any(rec.message == debug_message for rec in self._caplog.records)
@allow_windows
async def test_download_file_ok(self):
settings_dict = {
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
}
async with make_handler(settings_dict) as handler:
with MockServer() as server:
request = Request(
url=server.urljoin("mancha.pdf"),
meta={"playwright": True},
)
response = await handler._download_request(request, Spider("foo"))
assert response.meta["playwright_suggested_filename"] == "mancha.pdf"
assert response.body.startswith(b"%PDF-1.5")
assert response.headers.get("Content-Type") == b"application/pdf"
assert handler.stats.get_value("playwright/download_count") == 1
@allow_windows
async def test_download_file_delay_ok(self):
settings_dict = {
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 0,
}
async with make_handler(settings_dict) as handler:
with MockServer() as server:
request = Request(
url=server.urljoin("/mancha.pdf?delay=1"),
meta={"playwright": True},
)
response = await handler._download_request(request, Spider("foo"))
assert response.meta["playwright_suggested_filename"] == "mancha.pdf"
assert response.body.startswith(b"%PDF-1.5")
assert handler.stats.get_value("playwright/download_count") == 1
@allow_windows
async def test_download_file_delay_error(self):
settings_dict = {
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 10,
}
async with make_handler(settings_dict) as handler:
with MockServer() as server:
request = Request(
url=server.urljoin("/mancha.pdf?delay=1"),
meta={"playwright": True},
)
with pytest.raises(PlaywrightError) as excinfo:
await handler._download_request(request, Spider("foo"))
assert (
"scrapy-playwright",
logging.WARNING,
f"Closing page due to failed request: {request}"
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
) in self._caplog.record_tuples
@allow_windows
async def test_download_file_failure(self):
if self.browser_type != "chromium":
pytest.skip()
async def cancel_download(download):
await download.cancel()
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
request = Request(
url=server.urljoin("/mancha.pdf?content_length_multiplier=1000"),
meta={
"playwright": True,
"playwright_event_handlers": {"download": cancel_download},
},
)
with pytest.raises(RuntimeError) as excinfo:
await handler._download_request(request, Spider("foo"))
assert (
"scrapy-playwright",
logging.WARNING,
f"Closing page due to failed request: {request}"
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
) in self._caplog.record_tuples
@allow_windows
async def test_fail_status_204(self):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
request = Request(
url=server.urljoin("/status/204"),
meta={"playwright": True},
)
with pytest.raises(PlaywrightError) as excinfo:
await handler._download_request(request, Spider("foo"))
assert (
"scrapy-playwright",
logging.WARNING,
f"Closing page due to failed request: {request}"
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
) in self._caplog.record_tuples
class TestCaseChromium(IsolatedAsyncioTestCase, MixinTestCase):
browser_type = "chromium"
class TestCaseFirefox(IsolatedAsyncioTestCase, MixinTestCase):
browser_type = "firefox"
@pytest.mark.skipif(platform.system() != "Darwin", reason="Test WebKit only on Darwin")
class TestCaseWebkit(IsolatedAsyncioTestCase, MixinTestCase):
browser_type = "webkit"