Skip to content

Commit a2ea58f

Browse files
committed
fixes
1 parent 4473e0c commit a2ea58f

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

local-requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ flake8==4.0.1
88
pre-commit==2.19.0
99
Django==4.2.16
1010
pytest-xdist==2.5.0
11-
pytest-asyncio==0.21.1
11+
pytest-asyncio==0.24.0

pytest_playwright/asyncio.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import asyncio
1615
import shutil
1716
import os
1817
import sys
@@ -188,7 +187,7 @@ def browser_context_args(
188187

189188
return context_args
190189

191-
@pytest_asyncio.fixture()
190+
@pytest_asyncio.fixture(loop_scope="session")
192191
async def _artifacts_recorder(
193192
self,
194193
request: pytest.FixtureRequest,
@@ -207,19 +206,12 @@ async def _artifacts_recorder(
207206
)
208207
await artifacts_recorder.did_finish_test(failed)
209208

210-
@pytest_asyncio.fixture(scope="session")
209+
@pytest_asyncio.fixture(scope="session", loop_scope="session")
211210
async def playwright(self) -> AsyncGenerator[Playwright, None]:
212211
pw = await async_playwright().start()
213212
yield pw
214213
await pw.stop()
215214

216-
@pytest.fixture(scope="session")
217-
def event_loop(self) -> Generator[asyncio.AbstractEventLoop, None, None]:
218-
policy = asyncio.get_event_loop_policy()
219-
loop = policy.new_event_loop()
220-
yield loop
221-
loop.close()
222-
223215
@pytest.fixture(scope="session")
224216
def browser_type(self, playwright: Playwright, browser_name: str) -> BrowserType:
225217
return getattr(playwright, browser_name)
@@ -237,15 +229,15 @@ async def launch(**kwargs: Dict) -> Browser:
237229

238230
return launch
239231

240-
@pytest_asyncio.fixture(scope="session")
232+
@pytest_asyncio.fixture(scope="session", loop_scope="session")
241233
async def browser(
242234
self, launch_browser: Callable[[], Browser]
243235
) -> AsyncGenerator[Browser, None]:
244236
browser = await launch_browser()
245237
yield browser
246238
await browser.close()
247239

248-
@pytest_asyncio.fixture
240+
@pytest_asyncio.fixture(loop_scope="session")
249241
async def new_context(
250242
self,
251243
browser: Browser,
@@ -281,11 +273,11 @@ async def _close_wrapper(*args: Any, **kwargs: Any) -> None:
281273
for context in contexts.copy():
282274
await context.close()
283275

284-
@pytest_asyncio.fixture
276+
@pytest_asyncio.fixture(loop_scope="session")
285277
async def context(self, new_context: CreateContextCallback) -> BrowserContext:
286278
return await new_context()
287279

288-
@pytest_asyncio.fixture
280+
@pytest_asyncio.fixture(loop_scope="session")
289281
async def page(self, context: BrowserContext) -> Page:
290282
return await context.new_page()
291283

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ warn_unused_configs = True
1212
check_untyped_defs = True
1313
disallow_untyped_defs = True
1414
[tool:pytest]
15-
addopts = -p no:playwright --runpytest subprocess -vv
15+
addopts = -p no:playwright -p no:asyncio --runpytest subprocess -vv
1616
testpaths =
1717
tests
1818
[coverage:run]

tests/test_async.py

+52-18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@
1818
import pytest
1919

2020

21+
@pytest.fixture(autouse=True)
22+
def _set_default_ini_file(testdir: pytest.Testdir) -> None:
23+
testdir.makeconftest(
24+
"""
25+
import pytest
26+
27+
from pytest_asyncio import is_async_test
28+
29+
def pytest_collection_modifyitems(items):
30+
pytest_asyncio_tests = (item for item in items if is_async_test(item))
31+
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
32+
for async_test in pytest_asyncio_tests:
33+
async_test.add_marker(session_scope_marker, append=False)
34+
"""
35+
)
36+
37+
38+
def makeconftest(testdir: pytest.Testdir, content: str) -> None:
39+
lines = content.split("\n")
40+
spaces = [len(line) - len(line.lstrip()) for line in lines if line.strip()]
41+
min_spaces = min(spaces) if spaces else 0
42+
lines = [line[min_spaces:] for line in lines]
43+
44+
testdir.makeconftest(
45+
testdir.tmpdir.join("conftest.py").read_text("utf8") + "\n" + "\n".join(lines)
46+
)
47+
48+
2149
def test_default(testdir: pytest.Testdir) -> None:
2250
testdir.makepyfile(
2351
"""
@@ -112,14 +140,13 @@ async def test_multiple_browsers(page):
112140

113141

114142
def test_browser_context_args(testdir: pytest.Testdir) -> None:
115-
testdir.makeconftest(
143+
makeconftest(
144+
testdir,
116145
"""
117-
import pytest
118-
119146
@pytest.fixture(scope="session")
120147
def browser_context_args():
121148
return {"user_agent": "foobar"}
122-
"""
149+
""",
123150
)
124151
testdir.makepyfile(
125152
"""
@@ -134,14 +161,15 @@ async def test_browser_context_args(page):
134161

135162

136163
def test_user_defined_browser_context_args(testdir: pytest.Testdir) -> None:
137-
testdir.makeconftest(
164+
makeconftest(
165+
testdir,
138166
"""
139167
import pytest
140168
141169
@pytest.fixture(scope="session")
142170
def browser_context_args():
143171
return {"user_agent": "foobar"}
144-
"""
172+
""",
145173
)
146174
testdir.makepyfile(
147175
"""
@@ -159,14 +187,15 @@ async def test_browser_context_args(page):
159187

160188

161189
def test_user_defined_browser_context_args_clear_again(testdir: pytest.Testdir) -> None:
162-
testdir.makeconftest(
190+
makeconftest(
191+
testdir,
163192
"""
164193
import pytest
165194
166195
@pytest.fixture(scope="session")
167196
def browser_context_args():
168197
return {"user_agent": "foobar"}
169-
"""
198+
""",
170199
)
171200
testdir.makepyfile(
172201
"""
@@ -428,15 +457,16 @@ async def test_base_url(page):
428457

429458

430459
def test_browser_context_args_device(testdir: pytest.Testdir) -> None:
431-
testdir.makeconftest(
460+
makeconftest(
461+
testdir,
432462
"""
433463
import pytest
434464
435465
@pytest.fixture(scope="session")
436466
def browser_context_args(browser_context_args, playwright):
437467
iphone_11 = playwright.devices['iPhone 11 Pro']
438468
return {**browser_context_args, **iphone_11}
439-
"""
469+
""",
440470
)
441471
testdir.makepyfile(
442472
"""
@@ -451,7 +481,8 @@ async def test_browser_context_args(page):
451481

452482

453483
def test_launch_persistent_context_session(testdir: pytest.Testdir) -> None:
454-
testdir.makeconftest(
484+
makeconftest(
485+
testdir,
455486
"""
456487
import pytest_asyncio
457488
from playwright.sync_api import BrowserType
@@ -470,7 +501,7 @@ async def context(
470501
})
471502
yield context
472503
await context.close()
473-
"""
504+
""",
474505
)
475506
testdir.makepyfile(
476507
"""
@@ -485,7 +516,8 @@ async def test_browser_context_args(page):
485516

486517

487518
def test_context_page_on_session_level(testdir: pytest.Testdir) -> None:
488-
testdir.makeconftest(
519+
makeconftest(
520+
testdir,
489521
"""
490522
import pytest
491523
from playwright.sync_api import Browser, BrowserContext
@@ -509,7 +541,7 @@ async def page(
509541
):
510542
page = await context.new_page()
511543
yield page
512-
"""
544+
""",
513545
)
514546
testdir.makepyfile(
515547
"""
@@ -529,7 +561,8 @@ async def test_b(page):
529561

530562

531563
def test_launch_persistent_context_function(testdir: pytest.Testdir) -> None:
532-
testdir.makeconftest(
564+
makeconftest(
565+
testdir,
533566
"""
534567
import pytest
535568
from playwright.sync_api import BrowserType
@@ -549,7 +582,7 @@ async def context(
549582
})
550583
yield context
551584
await context.close()
552-
"""
585+
""",
553586
)
554587
testdir.makepyfile(
555588
"""
@@ -751,11 +784,12 @@ def _assert_folder_structure(root: str, expected: str) -> None:
751784

752785

753786
def test_is_able_to_set_expect_timeout_via_conftest(testdir: pytest.Testdir) -> None:
754-
testdir.makeconftest(
787+
makeconftest(
788+
testdir,
755789
"""
756790
from playwright.async_api import expect
757791
expect.set_options(timeout=1111)
758-
"""
792+
""",
759793
)
760794
testdir.makepyfile(
761795
"""

0 commit comments

Comments
 (0)