Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Nov 7, 2024
1 parent 4473e0c commit a2ea58f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion local-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ flake8==4.0.1
pre-commit==2.19.0
Django==4.2.16
pytest-xdist==2.5.0
pytest-asyncio==0.21.1
pytest-asyncio==0.24.0
20 changes: 6 additions & 14 deletions pytest_playwright/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
import shutil
import os
import sys
Expand Down Expand Up @@ -188,7 +187,7 @@ def browser_context_args(

return context_args

@pytest_asyncio.fixture()
@pytest_asyncio.fixture(loop_scope="session")
async def _artifacts_recorder(
self,
request: pytest.FixtureRequest,
Expand All @@ -207,19 +206,12 @@ async def _artifacts_recorder(
)
await artifacts_recorder.did_finish_test(failed)

@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def playwright(self) -> AsyncGenerator[Playwright, None]:
pw = await async_playwright().start()
yield pw
await pw.stop()

@pytest.fixture(scope="session")
def event_loop(self) -> Generator[asyncio.AbstractEventLoop, None, None]:
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
yield loop
loop.close()

@pytest.fixture(scope="session")
def browser_type(self, playwright: Playwright, browser_name: str) -> BrowserType:
return getattr(playwright, browser_name)
Expand All @@ -237,15 +229,15 @@ async def launch(**kwargs: Dict) -> Browser:

return launch

@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def browser(
self, launch_browser: Callable[[], Browser]
) -> AsyncGenerator[Browser, None]:
browser = await launch_browser()
yield browser
await browser.close()

@pytest_asyncio.fixture
@pytest_asyncio.fixture(loop_scope="session")
async def new_context(
self,
browser: Browser,
Expand Down Expand Up @@ -281,11 +273,11 @@ async def _close_wrapper(*args: Any, **kwargs: Any) -> None:
for context in contexts.copy():
await context.close()

@pytest_asyncio.fixture
@pytest_asyncio.fixture(loop_scope="session")
async def context(self, new_context: CreateContextCallback) -> BrowserContext:
return await new_context()

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

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ warn_unused_configs = True
check_untyped_defs = True
disallow_untyped_defs = True
[tool:pytest]
addopts = -p no:playwright --runpytest subprocess -vv
addopts = -p no:playwright -p no:asyncio --runpytest subprocess -vv
testpaths =
tests
[coverage:run]
Expand Down
70 changes: 52 additions & 18 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@
import pytest


@pytest.fixture(autouse=True)
def _set_default_ini_file(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
"""
import pytest
from pytest_asyncio import is_async_test
def pytest_collection_modifyitems(items):
pytest_asyncio_tests = (item for item in items if is_async_test(item))
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
for async_test in pytest_asyncio_tests:
async_test.add_marker(session_scope_marker, append=False)
"""
)


def makeconftest(testdir: pytest.Testdir, content: str) -> None:
lines = content.split("\n")
spaces = [len(line) - len(line.lstrip()) for line in lines if line.strip()]
min_spaces = min(spaces) if spaces else 0
lines = [line[min_spaces:] for line in lines]

testdir.makeconftest(
testdir.tmpdir.join("conftest.py").read_text("utf8") + "\n" + "\n".join(lines)
)


def test_default(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
Expand Down Expand Up @@ -112,14 +140,13 @@ async def test_multiple_browsers(page):


def test_browser_context_args(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
import pytest
@pytest.fixture(scope="session")
def browser_context_args():
return {"user_agent": "foobar"}
"""
""",
)
testdir.makepyfile(
"""
Expand All @@ -134,14 +161,15 @@ async def test_browser_context_args(page):


def test_user_defined_browser_context_args(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
import pytest
@pytest.fixture(scope="session")
def browser_context_args():
return {"user_agent": "foobar"}
"""
""",
)
testdir.makepyfile(
"""
Expand All @@ -159,14 +187,15 @@ async def test_browser_context_args(page):


def test_user_defined_browser_context_args_clear_again(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
import pytest
@pytest.fixture(scope="session")
def browser_context_args():
return {"user_agent": "foobar"}
"""
""",
)
testdir.makepyfile(
"""
Expand Down Expand Up @@ -428,15 +457,16 @@ async def test_base_url(page):


def test_browser_context_args_device(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
import pytest
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright):
iphone_11 = playwright.devices['iPhone 11 Pro']
return {**browser_context_args, **iphone_11}
"""
""",
)
testdir.makepyfile(
"""
Expand All @@ -451,7 +481,8 @@ async def test_browser_context_args(page):


def test_launch_persistent_context_session(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
import pytest_asyncio
from playwright.sync_api import BrowserType
Expand All @@ -470,7 +501,7 @@ async def context(
})
yield context
await context.close()
"""
""",
)
testdir.makepyfile(
"""
Expand All @@ -485,7 +516,8 @@ async def test_browser_context_args(page):


def test_context_page_on_session_level(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
import pytest
from playwright.sync_api import Browser, BrowserContext
Expand All @@ -509,7 +541,7 @@ async def page(
):
page = await context.new_page()
yield page
"""
""",
)
testdir.makepyfile(
"""
Expand All @@ -529,7 +561,8 @@ async def test_b(page):


def test_launch_persistent_context_function(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
import pytest
from playwright.sync_api import BrowserType
Expand All @@ -549,7 +582,7 @@ async def context(
})
yield context
await context.close()
"""
""",
)
testdir.makepyfile(
"""
Expand Down Expand Up @@ -751,11 +784,12 @@ def _assert_folder_structure(root: str, expected: str) -> None:


def test_is_able_to_set_expect_timeout_via_conftest(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
makeconftest(
testdir,
"""
from playwright.async_api import expect
expect.set_options(timeout=1111)
"""
""",
)
testdir.makepyfile(
"""
Expand Down

0 comments on commit a2ea58f

Please sign in to comment.