Skip to content

Commit e2d8e77

Browse files
committed
fixes
1 parent 4473e0c commit e2d8e77

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
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
@@ -58,7 +58,6 @@
5858
_is_debugger_attached,
5959
)
6060

61-
6261
class CreateContextCallback(Protocol):
6362
def __call__(
6463
self,
@@ -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

+33-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@
1717

1818
import pytest
1919

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

2146
def test_default(testdir: pytest.Testdir) -> None:
2247
testdir.makepyfile(
@@ -112,10 +137,8 @@ async def test_multiple_browsers(page):
112137

113138

114139
def test_browser_context_args(testdir: pytest.Testdir) -> None:
115-
testdir.makeconftest(
140+
makeconftest(testdir,
116141
"""
117-
import pytest
118-
119142
@pytest.fixture(scope="session")
120143
def browser_context_args():
121144
return {"user_agent": "foobar"}
@@ -134,7 +157,7 @@ async def test_browser_context_args(page):
134157

135158

136159
def test_user_defined_browser_context_args(testdir: pytest.Testdir) -> None:
137-
testdir.makeconftest(
160+
makeconftest(testdir,
138161
"""
139162
import pytest
140163
@@ -159,7 +182,7 @@ async def test_browser_context_args(page):
159182

160183

161184
def test_user_defined_browser_context_args_clear_again(testdir: pytest.Testdir) -> None:
162-
testdir.makeconftest(
185+
makeconftest(testdir,
163186
"""
164187
import pytest
165188
@@ -428,7 +451,7 @@ async def test_base_url(page):
428451

429452

430453
def test_browser_context_args_device(testdir: pytest.Testdir) -> None:
431-
testdir.makeconftest(
454+
makeconftest(testdir,
432455
"""
433456
import pytest
434457
@@ -451,7 +474,7 @@ async def test_browser_context_args(page):
451474

452475

453476
def test_launch_persistent_context_session(testdir: pytest.Testdir) -> None:
454-
testdir.makeconftest(
477+
makeconftest(testdir,
455478
"""
456479
import pytest_asyncio
457480
from playwright.sync_api import BrowserType
@@ -485,7 +508,7 @@ async def test_browser_context_args(page):
485508

486509

487510
def test_context_page_on_session_level(testdir: pytest.Testdir) -> None:
488-
testdir.makeconftest(
511+
makeconftest(testdir,
489512
"""
490513
import pytest
491514
from playwright.sync_api import Browser, BrowserContext
@@ -529,7 +552,7 @@ async def test_b(page):
529552

530553

531554
def test_launch_persistent_context_function(testdir: pytest.Testdir) -> None:
532-
testdir.makeconftest(
555+
makeconftest(testdir,
533556
"""
534557
import pytest
535558
from playwright.sync_api import BrowserType
@@ -751,7 +774,7 @@ def _assert_folder_structure(root: str, expected: str) -> None:
751774

752775

753776
def test_is_able_to_set_expect_timeout_via_conftest(testdir: pytest.Testdir) -> None:
754-
testdir.makeconftest(
777+
makeconftest(testdir,
755778
"""
756779
from playwright.async_api import expect
757780
expect.set_options(timeout=1111)

0 commit comments

Comments
 (0)