Skip to content

Commit

Permalink
feat: use anyio and support sync
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The plugin now requires anyio and nest-asyncio.
  • Loading branch information
m9810223 committed Sep 26, 2024
1 parent 9c0c900 commit d03ad0c
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 72 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@ pip install pytest-playwright-async
# tests/conftest.py
import asyncio

import pytest_asyncio
import nest_asyncio
import pytest


@pytest_asyncio.fixture(scope='session')
@pytest.fixture(scope='session', autouse=True)
def event_loop(): # https://pytest-asyncio.readthedocs.io/en/latest/reference/fixtures.html#fixtures
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
nest_asyncio._patch_loop(loop) # *
yield loop
loop.close()


@pytest.fixture(scope='session', autouse=True)
def anyio_backend():
return 'asyncio'

```

```py
# tests/test_for_readme.py
from playwright.async_api import Page
import pytest


@pytest.mark.asyncio
async def test_page_async(page_async: Page):
print(f'\n{page_async = }')
await page_async.goto('https://playwright.dev/')
Expand Down
77 changes: 50 additions & 27 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ requires-python = ">=3.8"
dependencies = [
# https://pypi.org/project/pytest-playwright/
# https://github.com/microsoft/playwright-pytest/blob/v0.5.0/pytest_playwright/pytest_playwright.py
"pytest-playwright>=0.5.0",
"pytest-playwright>=0.5.2", # https://github.com/microsoft/playwright-pytest/pull/236/files#diff-51d99e132c467c9f729b6b633a15ce5444e52872456e8e551146e0c87a694d31R582
# https://github.com/microsoft/playwright-python/blob/main/local-requirements.txt
"pytest-asyncio",
"anyio",
"nest-asyncio",
]
description = "ASYNC Pytest plugin for Playwright"
license = { text = "MIT" }
Expand Down
21 changes: 10 additions & 11 deletions src/pytest_playwright_async/pytest_playwright_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
from playwright.async_api import ViewportSize
from playwright.async_api import async_playwright
import pytest
import pytest_asyncio
from pytest_playwright.pytest_playwright import _build_artifact_test_folder
from pytest_playwright.pytest_playwright import create_guid
from pytest_playwright.pytest_playwright import _create_guid
from pytest_playwright.pytest_playwright import slugify


Expand Down Expand Up @@ -60,7 +59,7 @@ def browser_context_args_async(
return context_args


@pytest_asyncio.fixture
@pytest.fixture
async def _artifacts_recorder_async(
request: pytest.FixtureRequest,
playwright_async: Playwright,
Expand All @@ -77,15 +76,15 @@ async def _artifacts_recorder_async(
await async_artifacts_recorder.did_finish_test(failed)


@pytest_asyncio.fixture(scope='session')
@pytest.fixture(scope='session')
async def playwright_async() -> AsyncGenerator[Playwright, None]:
apw = await async_playwright().start()
yield apw
await apw.stop()


@pytest.fixture(scope='session')
def browser_type_async(playwright_async: Playwright, browser_name: str) -> BrowserType:
async def browser_type_async(playwright_async: Playwright, browser_name: str) -> BrowserType:
return getattr(playwright_async, browser_name)


Expand All @@ -102,7 +101,7 @@ async def launch(**kwargs: Dict) -> Browser:
return launch


@pytest_asyncio.fixture(scope='session')
@pytest.fixture(scope='session')
async def browser_async(
launch_browser_async: Callable[..., Awaitable[Browser]],
) -> AsyncGenerator[Browser, None]:
Expand Down Expand Up @@ -151,7 +150,7 @@ async def __call__(
) -> BrowserContext: ...


@pytest_asyncio.fixture
@pytest.fixture
async def new_context_async(
browser_async: Browser,
browser_context_args_async: dict,
Expand Down Expand Up @@ -183,12 +182,12 @@ async def _close_wrapper(*args: Any, **kwargs: Any) -> None:
await context_async.close()


@pytest_asyncio.fixture
@pytest.fixture
async def context_async(new_context_async: AsyncCreateContextCallback) -> BrowserContext:
return await new_context_async()


@pytest_asyncio.fixture
@pytest.fixture
async def page_async(context_async: BrowserContext) -> Page:
return await context_async.new_page()

Expand Down Expand Up @@ -286,7 +285,7 @@ async def on_did_create_browser_context(self, context_async: BrowserContext) ->

async def on_will_close_browser_context(self, context_async: BrowserContext) -> None:
if self._capture_trace:
trace_path = Path(self._pw_artifacts_folder.name) / create_guid()
trace_path = Path(self._pw_artifacts_folder.name) / _create_guid()
await context_async.tracing.stop(path=trace_path)
self._traces.append(str(trace_path))
else:
Expand All @@ -295,7 +294,7 @@ async def on_will_close_browser_context(self, context_async: BrowserContext) ->
if self._pytestconfig.getoption('--screenshot') in ['on', 'only-on-failure']:
for page in context_async.pages:
try:
screenshot_path = Path(self._pw_artifacts_folder.name) / create_guid()
screenshot_path = Path(self._pw_artifacts_folder.name) / _create_guid()
await page.screenshot(
timeout=5000,
path=screenshot_path,
Expand Down
11 changes: 9 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import asyncio

import pytest_asyncio
import nest_asyncio
import pytest


@pytest_asyncio.fixture(scope='session')
@pytest.fixture(scope='session', autouse=True)
def event_loop(): # https://pytest-asyncio.readthedocs.io/en/latest/reference/fixtures.html#fixtures
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
nest_asyncio._patch_loop(loop) # *
yield loop
loop.close()


@pytest.fixture(scope='session', autouse=True)
def anyio_backend():
return 'asyncio'
2 changes: 0 additions & 2 deletions tests/test_for_readme.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from playwright.async_api import Page
import pytest


@pytest.mark.asyncio
async def test_page_async(page_async: Page):
print(f'\n{page_async = }')
await page_async.goto('https://playwright.dev/')
Expand Down
3 changes: 0 additions & 3 deletions tests/test_from_playwright_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@

from playwright.async_api import Page
from playwright.async_api import expect
import pytest


@pytest.mark.asyncio
async def test_has_title(page_async: Page):
await page_async.goto('https://playwright.dev/')

# Expect a title "to contain" a substring.
await expect(page_async).to_have_title(re.compile('Playwright'))


@pytest.mark.asyncio
async def test_get_started_link(page_async: Page):
await page_async.goto('https://playwright.dev/')

Expand Down
Loading

0 comments on commit d03ad0c

Please sign in to comment.