Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aaf5f43

Browse files
committedJun 21, 2024··
Adapt all tests for Windows
1 parent 60a4e06 commit aaf5f43

9 files changed

+50
-9
lines changed
 

‎scrapy_playwright/_utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def get_event_loop(cls) -> asyncio.AbstractEventLoop:
117117
if not cls.loop.is_running():
118118
cls.thread = threading.Thread(target=cls.loop.run_forever, daemon=True)
119119
cls.thread.start()
120+
logger.info("Started loop on separate thread: %s", cls.loop)
120121
return cls.loop
121122

122123
@classmethod

‎tests/tests_asyncio/test_browser_contexts.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from scrapy import Spider, Request
1111
from scrapy_playwright.page import PageMethod
1212

13-
from tests import make_handler
13+
from tests import allow_windows, make_handler
1414
from tests.mockserver import StaticMockServer
1515

1616

1717
class MixinTestCaseMultipleContexts:
18+
@allow_windows
1819
async def test_context_kwargs(self):
1920
settings_dict = {
2021
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -37,6 +38,7 @@ async def test_context_kwargs(self):
3738
with pytest.raises(PlaywrightTimeoutError):
3839
await handler._download_request(req, Spider("foo"))
3940

41+
@allow_windows
4042
async def test_contexts_max_pages(self):
4143
settings = {
4244
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -71,6 +73,7 @@ async def test_contexts_max_pages(self):
7173

7274
assert handler.stats.get_value("playwright/page_count/max_concurrent") == 4
7375

76+
@allow_windows
7477
async def test_max_contexts(self):
7578
def cb_close_context(task):
7679
response = task.result()
@@ -105,6 +108,7 @@ def cb_close_context(task):
105108

106109
assert handler.stats.get_value("playwright/context_count/max_concurrent") == 4
107110

111+
@allow_windows
108112
async def test_contexts_startup(self):
109113
settings = {
110114
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -143,6 +147,7 @@ async def test_contexts_startup(self):
143147
assert cookie["value"] == "bar"
144148
assert cookie["domain"] == "example.org"
145149

150+
@allow_windows
146151
async def test_persistent_context(self):
147152
temp_dir = f"{tempfile.gettempdir()}/{uuid4()}"
148153
settings = {
@@ -161,6 +166,7 @@ async def test_persistent_context(self):
161166
assert handler.context_wrappers["persistent"].persistent
162167
assert not hasattr(handler, "browser")
163168

169+
@allow_windows
164170
async def test_mixed_persistent_contexts(self):
165171
temp_dir = f"{tempfile.gettempdir()}/{uuid4()}"
166172
settings = {
@@ -183,6 +189,7 @@ async def test_mixed_persistent_contexts(self):
183189
assert not handler.context_wrappers["non-persistent"].persistent
184190
assert isinstance(handler.browser, Browser)
185191

192+
@allow_windows
186193
async def test_contexts_dynamic(self):
187194
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
188195
assert len(handler.context_wrappers) == 0

‎tests/tests_asyncio/test_extensions.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import platform
12
from asyncio.subprocess import Process as AsyncioProcess
23
from unittest import IsolatedAsyncioTestCase
34
from unittest.mock import MagicMock, patch
@@ -34,6 +35,10 @@ class MockMemoryInfo:
3435
rss = 999
3536

3637

38+
@pytest.mark.skipif(
39+
platform.system() == "Windows",
40+
reason="resource stdlib module is not available on Windows",
41+
)
3742
@patch("scrapy.extensions.memusage.MailSender")
3843
class TestMemoryUsageExtension(IsolatedAsyncioTestCase):
3944
async def test_process_availability(self, _MailSender):

‎tests/tests_asyncio/test_headers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import pytest
66
from scrapy import Spider, Request
77

8-
from tests import make_handler
8+
from tests import allow_windows, make_handler
99
from tests.mockserver import MockServer
1010

1111

1212
class MixinProcessHeadersTestCase:
13+
@allow_windows
1314
async def test_user_agent(self):
1415
settings_dict = {
1516
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -39,6 +40,7 @@ async def test_user_agent(self):
3940
headers = {key.lower(): value for key, value in headers.items()}
4041
assert headers["user-agent"] == "foobar"
4142

43+
@allow_windows
4244
async def test_playwright_headers(self):
4345
"""Ignore Scrapy headers"""
4446
settings_dict = {
@@ -63,6 +65,7 @@ async def test_playwright_headers(self):
6365
assert "asdf" not in req.headers
6466
assert b"asdf" not in req.headers
6567

68+
@allow_windows
6669
async def test_use_custom_headers(self):
6770
"""Custom header processing function"""
6871

‎tests/tests_asyncio/test_page_methods.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from scrapy_playwright.page import PageMethod
1212

13-
from tests import make_handler, assert_correct_response
13+
from tests import allow_windows, make_handler, assert_correct_response
1414
from tests.mockserver import StaticMockServer
1515

1616

@@ -24,6 +24,7 @@ def get_mimetype(file):
2424

2525

2626
class TestPageMethods(IsolatedAsyncioTestCase):
27+
@allow_windows
2728
async def test_page_methods(self):
2829
screenshot = PageMethod("screenshot", "foo", 123, path="/tmp/file", type="png")
2930
assert screenshot.method == "screenshot"
@@ -39,6 +40,7 @@ def inject_fixtures(self, caplog):
3940
caplog.set_level(logging.DEBUG)
4041
self._caplog = caplog
4142

43+
@allow_windows
4244
async def test_page_non_page_method(self):
4345
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
4446
with StaticMockServer() as server:
@@ -63,6 +65,7 @@ async def test_page_non_page_method(self):
6365
f"Ignoring {repr(obj)}: expected PageMethod, got {repr(type(obj))}",
6466
) in self._caplog.record_tuples
6567

68+
@allow_windows
6669
async def test_page_mixed_page_methods(self):
6770
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
6871
with StaticMockServer() as server:
@@ -89,6 +92,7 @@ async def test_page_mixed_page_methods(self):
8992
assert not req.meta["playwright_page_methods"]["is_closed"].result
9093
assert req.meta["playwright_page_methods"]["title"].result == "Awesome site"
9194

95+
@allow_windows
9296
async def test_page_method_navigation(self):
9397
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
9498
with StaticMockServer() as server:
@@ -110,6 +114,7 @@ async def test_page_method_navigation(self):
110114
text = resp.css("p::text").get()
111115
assert text == "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
112116

117+
@allow_windows
113118
async def test_page_method_infinite_scroll(self):
114119
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
115120
with StaticMockServer() as server:
@@ -136,6 +141,7 @@ async def test_page_method_infinite_scroll(self):
136141
assert_correct_response(resp, req)
137142
assert len(resp.css("div.quote")) == 30
138143

144+
@allow_windows
139145
async def test_page_method_screenshot(self):
140146
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
141147
with NamedTemporaryFile(mode="w+b", delete=False) as png_file:
@@ -156,6 +162,7 @@ async def test_page_method_screenshot(self):
156162
if platform.system() != "Windows":
157163
assert get_mimetype(png_file) == "image/png"
158164

165+
@allow_windows
159166
async def test_page_method_pdf(self):
160167
if self.browser_type != "chromium":
161168
pytest.skip("PDF generation is supported only in Chromium")

‎tests/tests_asyncio/test_playwright_requests.py

+19
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ async def test_basic_response(self):
5555
assert resp.meta["playwright_page"].url == resp.url
5656
await resp.meta["playwright_page"].close()
5757

58+
@allow_windows
5859
async def test_post_request(self):
5960
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
6061
with MockServer() as server:
@@ -66,6 +67,7 @@ async def test_post_request(self):
6667
assert_correct_response(resp, req)
6768
assert "Request body: foo=bar" in resp.text
6869

70+
@allow_windows
6971
async def test_timeout_error(self):
7072
settings_dict = {
7173
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -83,6 +85,7 @@ async def test_timeout_error(self):
8385
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
8486
) in self._caplog.record_tuples
8587

88+
@allow_windows
8689
async def test_retry_page_content_still_navigating(self):
8790
if self.browser_type != "chromium":
8891
pytest.skip("Only Chromium seems to redirect meta tags within the same goto call")
@@ -104,6 +107,7 @@ async def test_retry_page_content_still_navigating(self):
104107
) in self._caplog.record_tuples
105108

106109
@patch("scrapy_playwright.handler.logger")
110+
@allow_windows
107111
async def test_route_continue_exception(self, logger):
108112
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
109113
scrapy_request = Request(url="https://example.org", method="GET")
@@ -154,6 +158,7 @@ async def test_route_continue_exception(self, logger):
154158
with pytest.raises(PlaywrightError):
155159
await req_handler(route, playwright_request)
156160

161+
@allow_windows
157162
async def test_event_handler_dialog_callable(self):
158163
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
159164
with StaticMockServer() as server:
@@ -175,6 +180,7 @@ async def test_event_handler_dialog_callable(self):
175180

176181
assert spider.dialog_message == "foobar"
177182

183+
@allow_windows
178184
async def test_event_handler_dialog_str(self):
179185
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
180186
with StaticMockServer() as server:
@@ -196,6 +202,7 @@ async def test_event_handler_dialog_str(self):
196202

197203
assert spider.dialog_message == "foobar"
198204

205+
@allow_windows
199206
async def test_event_handler_dialog_missing(self):
200207
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
201208
with StaticMockServer() as server:
@@ -219,6 +226,7 @@ async def test_event_handler_dialog_missing(self):
219226
) in self._caplog.record_tuples
220227
assert getattr(spider, "dialog_message", None) is None
221228

229+
@allow_windows
222230
async def test_response_attributes(self):
223231
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
224232
with MockServer() as server:
@@ -230,6 +238,7 @@ async def test_response_attributes(self):
230238

231239
assert response.ip_address == ip_address(server.address)
232240

241+
@allow_windows
233242
async def test_page_goto_kwargs_referer(self):
234243
if self.browser_type != "chromium":
235244
pytest.skip("referer as goto kwarg seems to work only with chromium :shrug:")
@@ -248,6 +257,7 @@ async def test_page_goto_kwargs_referer(self):
248257
headers = json.loads(response.css("pre::text").get())
249258
assert headers["Referer"] == fake_referer
250259

260+
@allow_windows
251261
async def test_navigation_returns_none(self):
252262
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
253263
with MockServer():
@@ -263,6 +273,7 @@ async def test_navigation_returns_none(self):
263273
assert not response.headers
264274
assert response.status == 200
265275

276+
@allow_windows
266277
async def test_abort_requests(self):
267278
async def should_abort_request_async(request):
268279
return request.resource_type == "image"
@@ -295,6 +306,7 @@ def should_abort_request_sync(request):
295306
assert handler.stats.get_value(f"{resp_prefix}/resource_type/image") is None
296307
assert handler.stats.get_value(f"{req_prefix}/aborted") == 3
297308

309+
@allow_windows
298310
async def test_page_initialization_ok(self):
299311
async def init_page(page, _request):
300312
await page.set_extra_http_headers({"Extra-Header": "Qwerty"})
@@ -315,6 +327,7 @@ async def init_page(page, _request):
315327
headers = {key.lower(): value for key, value in headers.items()}
316328
assert headers["extra-header"] == "Qwerty"
317329

330+
@allow_windows
318331
async def test_page_initialization_fail(self):
319332
async def init_page(page, _request, _missing):
320333
await page.set_extra_http_headers({"Extra-Header": "Qwerty"})
@@ -341,6 +354,7 @@ async def init_page(page, _request, _missing):
341354
assert f"[Context=default] Page init callback exception for {req!r}" in entry[2]
342355
assert "init_page() missing 1 required positional argument: '_missing'" in entry[2]
343356

357+
@allow_windows
344358
async def test_redirect(self):
345359
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
346360
with MockServer() as server:
@@ -358,6 +372,7 @@ async def test_redirect(self):
358372
server.urljoin("/redirect"),
359373
]
360374

375+
@allow_windows
361376
async def test_logging_record_spider(self):
362377
"""Make sure at least one log record has the spider as an attribute
363378
(records sent before opening the spider will not have it).
@@ -370,6 +385,7 @@ async def test_logging_record_spider(self):
370385

371386
assert any(getattr(rec, "spider", None) is spider for rec in self._caplog.records)
372387

388+
@allow_windows
373389
async def test_download_file(self):
374390
settings_dict = {
375391
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -385,6 +401,7 @@ async def test_download_file(self):
385401
assert response.body.startswith(b"%PDF-1.5")
386402
assert handler.stats.get_value("playwright/download_count") == 1
387403

404+
@allow_windows
388405
async def test_download_file_delay_ok(self):
389406
settings_dict = {
390407
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -401,6 +418,7 @@ async def test_download_file_delay_ok(self):
401418
assert response.body.startswith(b"%PDF-1.5")
402419
assert handler.stats.get_value("playwright/download_count") == 1
403420

421+
@allow_windows
404422
async def test_download_file_delay_error(self):
405423
settings_dict = {
406424
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
@@ -421,6 +439,7 @@ async def test_download_file_delay_error(self):
421439
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
422440
) in self._caplog.record_tuples
423441

442+
@allow_windows
424443
async def test_download_file_failure(self):
425444
if self.browser_type != "chromium":
426445
pytest.skip()

‎tests/tests_asyncio/test_remote.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from playwright.async_api import async_playwright
1111
from scrapy import Request, Spider
1212

13-
from tests import make_handler, assert_correct_response
13+
from tests import allow_windows, make_handler, assert_correct_response
1414
from tests.mockserver import StaticMockServer
1515

1616

@@ -61,6 +61,7 @@ def inject_fixtures(self, caplog):
6161
caplog.set_level(logging.DEBUG)
6262
self._caplog = caplog
6363

64+
@allow_windows
6465
async def test_devtools(self):
6566
async with remote_chromium() as devtools_url:
6667
settings_dict = {

‎tests/tests_asyncio/test_settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from scrapy_playwright.handler import Config
66

7-
from tests import make_handler
7+
from tests import allow_windows, make_handler
88

99

1010
class TestSettings(IsolatedAsyncioTestCase):
@@ -31,6 +31,7 @@ async def test_max_pages_per_context(self):
3131
config = Config.from_settings(Settings({"CONCURRENT_REQUESTS": 9876}))
3232
assert config.max_pages_per_context == 9876
3333

34+
@allow_windows
3435
async def test_max_contexts(self):
3536
async with make_handler({"PLAYWRIGHT_MAX_CONTEXTS": None}) as handler:
3637
assert not hasattr(handler, "context_semaphore")

‎tests/tests_twisted/test_mixed_requests.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
from tests.mockserver import StaticMockServer
1111

1212

13-
@pytest.mark.skipif(
14-
platform.system() == "Windows",
15-
reason="On Windows, If the task reaches two, it will be blocked.",
16-
)
13+
@pytest.mark.skipif(platform.system() == "Windows", reason="Gets stuck on Windows")
1714
class MixedRequestsTestCase(TestCase):
1815
"""
1916
This test case ensures the handler's 'download_request' method works as expected, and

0 commit comments

Comments
 (0)
Please sign in to comment.