2
2
import platform
3
3
import subprocess
4
4
from tempfile import NamedTemporaryFile
5
+ from unittest import IsolatedAsyncioTestCase
5
6
6
7
import pytest
7
8
from scrapy import Spider , Request
@@ -22,19 +23,25 @@ def get_mimetype(file):
22
23
).stdout .strip ()
23
24
24
25
25
- @pytest .mark .asyncio
26
- async def test_page_methods ():
27
- screenshot = PageMethod ("screenshot" , "foo" , 123 , path = "/tmp/file" , type = "png" )
28
- assert screenshot .method == "screenshot"
29
- assert screenshot .args == ("foo" , 123 )
30
- assert screenshot .kwargs == {"path" : "/tmp/file" , "type" : "png" }
31
- assert screenshot .result is None
32
- assert str (screenshot ) == "<PageMethod for method 'screenshot'>"
26
+ class TestPageMethods (IsolatedAsyncioTestCase ):
27
+ @pytest .mark .asyncio
28
+ async def test_page_methods (self ):
29
+ screenshot = PageMethod ("screenshot" , "foo" , 123 , path = "/tmp/file" , type = "png" )
30
+ assert screenshot .method == "screenshot"
31
+ assert screenshot .args == ("foo" , 123 )
32
+ assert screenshot .kwargs == {"path" : "/tmp/file" , "type" : "png" }
33
+ assert screenshot .result is None
34
+ assert str (screenshot ) == "<PageMethod for method 'screenshot'>"
33
35
34
36
35
37
class MixinPageMethodTestCase :
38
+ @pytest .fixture (autouse = True )
39
+ def inject_fixtures (self , caplog ):
40
+ caplog .set_level (logging .DEBUG )
41
+ self ._caplog = caplog
42
+
36
43
@pytest .mark .asyncio
37
- async def test_page_non_page_method (self , caplog ):
44
+ async def test_page_non_page_method (self ):
38
45
async with make_handler ({"PLAYWRIGHT_BROWSER_TYPE" : self .browser_type }) as handler :
39
46
with StaticMockServer () as server :
40
47
req = Request (
@@ -56,10 +63,10 @@ async def test_page_non_page_method(self, caplog):
56
63
"scrapy-playwright" ,
57
64
logging .WARNING ,
58
65
f"Ignoring { repr (obj )} : expected PageMethod, got { repr (type (obj ))} " ,
59
- ) in caplog .record_tuples
66
+ ) in self . _caplog .record_tuples
60
67
61
68
@pytest .mark .asyncio
62
- async def test_page_mixed_page_methods (self , caplog ):
69
+ async def test_page_mixed_page_methods (self ):
63
70
async with make_handler ({"PLAYWRIGHT_BROWSER_TYPE" : self .browser_type }) as handler :
64
71
with StaticMockServer () as server :
65
72
req = Request (
@@ -81,7 +88,7 @@ async def test_page_mixed_page_methods(self, caplog):
81
88
"scrapy-playwright" ,
82
89
logging .WARNING ,
83
90
f"Ignoring { repr (does_not_exist )} : could not find method" ,
84
- ) in caplog .record_tuples
91
+ ) in self . _caplog .record_tuples
85
92
assert not req .meta ["playwright_page_methods" ]["is_closed" ].result
86
93
assert req .meta ["playwright_page_methods" ]["title" ].result == "Awesome site"
87
94
@@ -178,14 +185,14 @@ async def test_page_method_pdf(self):
178
185
assert get_mimetype (pdf_file ) == "application/pdf"
179
186
180
187
181
- class TestPageMethodChromium (MixinPageMethodTestCase ):
188
+ class TestPageMethodChromium (IsolatedAsyncioTestCase , MixinPageMethodTestCase ):
182
189
browser_type = "chromium"
183
190
184
191
185
- class TestPageMethodFirefox (MixinPageMethodTestCase ):
192
+ class TestPageMethodFirefox (IsolatedAsyncioTestCase , MixinPageMethodTestCase ):
186
193
browser_type = "firefox"
187
194
188
195
189
196
@pytest .mark .skipif (platform .system () != "Darwin" , reason = "Test WebKit only on Darwin" )
190
- class TestPageMethodWebkit (MixinPageMethodTestCase ):
197
+ class TestPageMethodWebkit (IsolatedAsyncioTestCase , MixinPageMethodTestCase ):
191
198
browser_type = "webkit"
0 commit comments