Skip to content

Commit 82f0ed9

Browse files
committed
Test updates
Squashed commit of the following: commit 834095e593bcfec29b1e42745b71164bb481f414 Author: Eugenio Lacuesta <eugenio.lacuesta@gmail.com> Date: Mon Jun 15 16:10:46 2026 -0300 Fix NOQA comment commit 603ea0a Author: Eugenio Lacuesta <eugenio.lacuesta@gmail.com> Date: Mon Jun 15 15:53:23 2026 -0300 Linting commit 9bcf393 Author: Eugenio Lacuesta <eugenio.lacuesta@gmail.com> Date: Mon Jun 15 15:42:47 2026 -0300 Replace IsolatedAsyncioTestCase when not needed commit 7c80d8f Author: Eugenio Lacuesta <eugenio.lacuesta@gmail.com> Date: Mon Jun 15 15:22:43 2026 -0300 Merge test cases
1 parent 286ec53 commit 82f0ed9

6 files changed

Lines changed: 76 additions & 106 deletions

File tree

pylintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ disable=
33
attribute-defined-outside-init,
44
broad-except,
55
invalid-name,
6+
line-too-long,
67
missing-class-docstring,
78
missing-function-docstring,
89
missing-module-docstring,

tests/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,22 @@ def allow_windows(test_method):
3838
return test_method
3939

4040

41-
@asynccontextmanager
42-
async def make_handler(settings_dict: Optional[dict] = None):
43-
"""Convenience function to obtain an initialized handler and close it gracefully"""
41+
def create_handler(settings_dict: Optional[dict] = None):
4442
from scrapy_playwright.handler import ScrapyPlaywrightDownloadHandler
4543

4644
settings: dict = settings_dict or {}
4745
settings.setdefault("TELNETCONSOLE_ENABLED", False)
4846
crawler = get_crawler(settings_dict=settings)
4947
handler = ScrapyPlaywrightDownloadHandler(crawler=crawler)
48+
return handler
49+
50+
51+
@asynccontextmanager
52+
async def make_handler(settings_dict: Optional[dict] = None):
53+
"""Convenience function to obtain an initialized handler and close it gracefully"""
54+
from scrapy_playwright.handler import ScrapyPlaywrightDownloadHandler
55+
56+
handler: ScrapyPlaywrightDownloadHandler = create_handler(settings_dict)
5057
try:
5158
await handler._maybe_launch_in_thread()
5259
except: # noqa (E722), pylint: disable=bare-except

tests/tests_asyncio/test_page_methods.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import platform
33
import subprocess
44
from tempfile import NamedTemporaryFile
5-
from unittest import IsolatedAsyncioTestCase
5+
from unittest import IsolatedAsyncioTestCase, TestCase
66

77
import pytest
88
from scrapy import Spider, Request
@@ -24,9 +24,8 @@ def get_mimetype(file):
2424
).stdout.strip()
2525

2626

27-
class TestPageMethods(IsolatedAsyncioTestCase):
28-
@allow_windows
29-
async def test_page_methods(self):
27+
class TestPageMethods(TestCase):
28+
def test_page_methods(self):
3029
screenshot = PageMethod("screenshot", "foo", 123, path="/tmp/file", type="png")
3130
assert screenshot.method == "screenshot"
3231
assert screenshot.args == ("foo", 123)

tests/tests_asyncio/test_playwright_requests.py

Lines changed: 43 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -209,48 +209,28 @@ async def test_route_continue_exception(self, logger):
209209
await req_handler(route, playwright_request)
210210

211211
@allow_windows
212-
async def test_event_handler_dialog_callable(self):
212+
async def test_event_handler_dialog(self):
213213
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
214214
with StaticMockServer() as server:
215-
spider = DialogSpider()
216-
req = Request(
217-
url=server.urljoin("/index.html"),
218-
meta={
219-
"playwright": True,
220-
"playwright_page_methods": [
221-
# trigger an alert
222-
PageMethod("evaluate", "alert('foobar');"),
223-
],
224-
"playwright_page_event_handlers": {
225-
"dialog": spider.handle_dialog,
226-
},
227-
},
228-
)
229-
await handler._download_request(req, spider)
230-
231-
assert spider.dialog_message == "foobar"
232-
233-
@allow_windows
234-
async def test_event_handler_dialog_str(self):
235-
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
236-
with StaticMockServer() as server:
237-
spider = DialogSpider()
238-
req = Request(
239-
url=server.urljoin("/index.html"),
240-
meta={
241-
"playwright": True,
242-
"playwright_page_methods": [
243-
# trigger an alert
244-
PageMethod("evaluate", "alert('foobar');"),
245-
],
246-
"playwright_page_event_handlers": {
247-
"dialog": "handle_dialog",
215+
for use_callable in (True, False):
216+
spider = DialogSpider()
217+
req = Request(
218+
url=server.urljoin("/index.html"),
219+
meta={
220+
"playwright": True,
221+
"playwright_page_methods": [
222+
# trigger an alert
223+
PageMethod("evaluate", "alert('foobar');"),
224+
],
225+
"playwright_page_event_handlers": {
226+
"dialog": (
227+
spider.handle_dialog if use_callable else "handle_dialog"
228+
),
229+
},
248230
},
249-
},
250-
)
251-
await handler._download_request(req, spider)
252-
253-
assert spider.dialog_message == "foobar"
231+
)
232+
await handler._download_request(req, spider)
233+
assert spider.dialog_message == "foobar"
254234

255235
@allow_windows
256236
async def test_event_handler_dialog_missing(self):
@@ -435,56 +415,40 @@ async def test_logging_record_spider(self):
435415
assert any(getattr(rec, "spider", None) is spider for rec in self._caplog.records)
436416

437417
@allow_windows
438-
@patch("scrapy_playwright.handler._make_request_logger")
439-
async def test_request_logger_disabled(self, make_request_logger: MagicMock):
440-
self._caplog.set_level(logging.DEBUG + 1, "scrapy-playwright")
441-
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
442-
with MockServer() as server:
443-
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
444-
await handler._download_request(req, Spider("foo"))
445-
446-
debug_message = (
447-
f"[Context=default] Request: <{req.method} {req.url}> (resource type: document)"
448-
)
449-
assert not any(rec.message == debug_message for rec in self._caplog.records)
450-
make_request_logger.assert_not_called()
451-
452-
@allow_windows
453-
async def test_request_logger_enabled(self):
454-
self._caplog.set_level(logging.DEBUG, "scrapy-playwright")
418+
async def test_request_logger(self):
455419
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
456420
with MockServer() as server:
421+
with self._caplog.at_level(logging.DEBUG + 1, logger="scrapy-playwright"):
422+
with patch("scrapy_playwright.handler._make_request_logger") as mock_logger:
423+
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
424+
await handler._download_request(req, Spider("foo"))
425+
msg = f"[Context=default] Request: <{req.method} {req.url}> (resource type: document)" # noqa: E501
426+
assert not any(rec.message == msg for rec in self._caplog.records)
427+
mock_logger.assert_not_called()
428+
429+
self._caplog.set_level(logging.DEBUG, "scrapy-playwright")
457430
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
458431
await handler._download_request(req, Spider("foo"))
459-
460-
debug_message = (
461-
f"[Context=default] Request: <{req.method} {req.url}> (resource type: document)"
462-
)
463-
assert any(rec.message == debug_message for rec in self._caplog.records)
432+
msg = f"[Context=default] Request: <{req.method} {req.url}> (resource type: document)" # noqa: E501
433+
assert any(rec.message == msg for rec in self._caplog.records)
464434

465435
@allow_windows
466-
@patch("scrapy_playwright.handler._make_response_logger")
467-
async def test_response_logger_disabled(self, make_response_logger: MagicMock):
468-
self._caplog.set_level(logging.DEBUG + 1, "scrapy-playwright")
469-
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
470-
with MockServer() as server:
471-
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
472-
response = await handler._download_request(req, Spider("foo"))
473-
474-
debug_message = f"[Context=default] Response: <{response.status} {response.url}>"
475-
assert not any(rec.message == debug_message for rec in self._caplog.records)
476-
make_response_logger.assert_not_called()
477-
478-
@allow_windows
479-
async def test_response_logger_enabled(self):
480-
self._caplog.set_level(logging.DEBUG, "scrapy-playwright")
436+
async def test_response_logger(self):
481437
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
482438
with MockServer() as server:
439+
with self._caplog.at_level(logging.DEBUG + 1, logger="scrapy-playwright"):
440+
with patch("scrapy_playwright.handler._make_response_logger") as mock_logger:
441+
req = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
442+
response = await handler._download_request(req, Spider("foo"))
443+
debug_message = f"[Context=default] Response: <{response.status} {response.url}>"
444+
assert not any(rec.message == debug_message for rec in self._caplog.records)
445+
mock_logger.assert_not_called()
446+
447+
self._caplog.set_level(logging.DEBUG, "scrapy-playwright")
483448
request = Request(url=server.urljoin("/index.html"), meta={"playwright": True})
484449
response = await handler._download_request(request, Spider("foo"))
485-
486-
debug_message = f"[Context=default] Response: <{response.status} {response.url}>"
487-
assert any(rec.message == debug_message for rec in self._caplog.records)
450+
debug_message = f"[Context=default] Response: <{response.status} {response.url}>"
451+
assert any(rec.message == debug_message for rec in self._caplog.records)
488452

489453
@allow_windows
490454
async def test_download_file_ok(self):
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from unittest import IsolatedAsyncioTestCase
1+
from unittest import TestCase
22

33
import pytest
44
from scrapy.exceptions import NotSupported
55
from scrapy.settings import Settings
66

77
from scrapy_playwright.handler import Config
88

9-
from tests import allow_windows, make_handler
9+
from tests import create_handler
1010

1111

12-
class TestSettings(IsolatedAsyncioTestCase):
13-
async def test_settings_timeout_value(self):
12+
class TestSettings(TestCase):
13+
def test_settings_timeout_value(self):
1414
config = Config.from_settings(Settings({}))
1515
assert config.navigation_timeout_ms is None
1616

@@ -26,14 +26,14 @@ async def test_settings_timeout_value(self):
2626
config = Config.from_settings(Settings({"PLAYWRIGHT_DEFAULT_NAVIGATION_TIMEOUT": 0.5}))
2727
assert config.navigation_timeout_ms == 0.5
2828

29-
async def test_max_pages_per_context(self):
29+
def test_max_pages_per_context(self):
3030
config = Config.from_settings(Settings({"PLAYWRIGHT_MAX_PAGES_PER_CONTEXT": 1234}))
3131
assert config.max_pages_per_context == 1234
3232

3333
config = Config.from_settings(Settings({"CONCURRENT_REQUESTS": 9876}))
3434
assert config.max_pages_per_context == 9876
3535

36-
async def test_connect_remote_urls(self):
36+
def test_connect_remote_urls(self):
3737
with pytest.raises(NotSupported) as exc_info:
3838
Config.from_settings(
3939
Settings({"PLAYWRIGHT_CONNECT_URL": "asdf", "PLAYWRIGHT_CDP_URL": "qwerty"})
@@ -43,10 +43,9 @@ async def test_connect_remote_urls(self):
4343
== "Setting both PLAYWRIGHT_CDP_URL and PLAYWRIGHT_CONNECT_URL is not supported"
4444
)
4545

46-
@allow_windows
47-
async def test_max_contexts(self):
48-
async with make_handler({"PLAYWRIGHT_MAX_CONTEXTS": None}) as handler:
49-
assert not hasattr(handler, "context_semaphore")
46+
def test_max_contexts(self):
47+
handler = create_handler({"PLAYWRIGHT_MAX_CONTEXTS": None})
48+
assert not hasattr(handler, "context_semaphore")
5049

51-
async with make_handler({"PLAYWRIGHT_MAX_CONTEXTS": 1234}) as handler:
52-
assert handler.context_semaphore._value == 1234
50+
handler = create_handler({"PLAYWRIGHT_MAX_CONTEXTS": 1234})
51+
assert handler.context_semaphore._value == 1234

tests/tests_asyncio/test_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from decimal import Decimal
3-
from unittest import IsolatedAsyncioTestCase
3+
from unittest import IsolatedAsyncioTestCase, TestCase
44
from unittest.mock import AsyncMock
55

66
import pytest
@@ -71,7 +71,7 @@ async def test_get_page_content_reraise_unknown_exception(self):
7171
)
7272

7373

74-
class TestBodyEncoding(IsolatedAsyncioTestCase):
74+
class TestBodyEncoding(TestCase):
7575
@staticmethod
7676
def body_str(charset: str, content: str = "áéíóú") -> str:
7777
return f"""
@@ -86,7 +86,7 @@ def body_str(charset: str, content: str = "áéíóú") -> str:
8686
</html>
8787
""".strip()
8888

89-
async def test_encode_from_headers(self):
89+
def test_encode_from_headers(self):
9090
"""Charset declared in headers takes precedence"""
9191
text = self.body_str(charset="gb2312")
9292
body, encoding = _encode_body(
@@ -96,21 +96,21 @@ async def test_encode_from_headers(self):
9696
assert encoding == "cp1252"
9797
assert body == text.encode(encoding)
9898

99-
async def test_encode_from_body(self):
99+
def test_encode_from_body(self):
100100
"""No charset declared in headers, use the one declared in the body"""
101101
text = self.body_str(charset="gb2312")
102102
body, encoding = _encode_body(headers=Headers({}), text=text)
103103
assert encoding == "gb18030"
104104
assert body == text.encode(encoding)
105105

106-
async def test_encode_fallback_utf8(self):
106+
def test_encode_fallback_utf8(self):
107107
"""No charset declared, use utf-8 as fallback"""
108108
text = "<html>áéíóú</html>"
109109
body, encoding = _encode_body(headers=Headers(), text=text)
110110
assert encoding == "utf-8"
111111
assert body == text.encode(encoding)
112112

113-
async def test_encode_mismatch(self):
113+
def test_encode_mismatch(self):
114114
"""Charset declared in headers and body do not match, and the headers
115115
one fails to encode: use the one in the body (first one that works)
116116
"""
@@ -152,8 +152,8 @@ async def _awaitable_identity(x):
152152
assert await _maybe_await(1234) == 1234
153153

154154

155-
class TestGetFloatSetting(IsolatedAsyncioTestCase):
156-
async def test_get_float_setting(self):
155+
class TestGetFloatSetting(TestCase):
156+
def test_get_float_setting(self):
157157
settings = Settings(
158158
{
159159
"ZERO": 0,

0 commit comments

Comments
 (0)