Skip to content

Commit

Permalink
feat: support uvloop
Browse files Browse the repository at this point in the history
  • Loading branch information
m9810223 committed Sep 28, 2024
1 parent c870c1d commit 397d236
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 150 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ pip install pytest-playwright-async

[Here](https://github.com/m9810223/playwright-async-pytest/blob/master/tests) you can find more examples.

```py
# tests/conftest.py
---

import asyncio
```py
# tests/async/conftest.py

import nest_asyncio # pip install nest-asyncio
import pytest


@pytest.fixture(scope='session', autouse=True)
def event_loop():
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)
# pip install anyio
def anyio_backend():
return 'asyncio'
# install anyio
# install uvloop
@pytest.fixture(
scope='session',
params=[
# https://anyio.readthedocs.io/en/stable/testing.html#specifying-the-backends-to-run-on
pytest.param(('asyncio', {'use_uvloop': True}), id='asyncio+uvloop'),
pytest.param(('asyncio', {'use_uvloop': False}), id='asyncio'),
# pytest.param(('trio', {'restrict_keyboard_interrupt_to_checkpoints': True}), id='trio'),
],
autouse=True,
)
def anyio_backend(request):
return request.param

```

```py
# tests/test_for_readme.py
# tests/async/test_for_readme.py

from playwright.async_api import Page

Expand Down
41 changes: 40 additions & 1 deletion pdm.lock

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

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ distribution = true
dev = [
"pytest",
"ipython<8.13.1",
"nest-asyncio",
"anyio<4.6.0",
"nest-asyncio",
"uvloop",
]

[tool.pdm.scripts]
Expand Down
Empty file added tests/async/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions tests/async/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest


# install anyio
# install uvloop
@pytest.fixture(
scope='session',
params=[
# https://anyio.readthedocs.io/en/stable/testing.html#specifying-the-backends-to-run-on
pytest.param(('asyncio', {'use_uvloop': True}), id='asyncio+uvloop'),
pytest.param(('asyncio', {'use_uvloop': False}), id='asyncio'),
# pytest.param(('trio', {'restrict_keyboard_interrupt_to_checkpoints': True}), id='trio'),
],
autouse=True,
)
def anyio_backend(request):
return request.param
File renamed without changes.
File renamed without changes.
122 changes: 11 additions & 111 deletions tests/test_playwright.py → tests/async/test_playwright.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import asyncio
import typing as t

import nest_asyncio # install nest-asyncio
from playwright.async_api import Browser
from playwright.async_api import BrowserContext
from playwright.async_api import BrowserType
from playwright.async_api import Page
from playwright.async_api import Playwright
from playwright.async_api import async_playwright
from playwright.sync_api import Browser as SBrowser
from playwright.sync_api import BrowserContext as SBrowserContext
from playwright.sync_api import BrowserType as SBrowserType
from playwright.sync_api import Page as SPage
from playwright.sync_api import Playwright as SPlaywright
from playwright.sync_api import sync_playwright
import pytest


Expand All @@ -22,7 +18,15 @@ def check_title(title):
assert 'Playwright' in title


async def test_async_playwright():
@pytest.fixture
def event_loop():
loop = asyncio.get_event_loop_policy().new_event_loop()
nest_asyncio.apply(loop)
yield loop
loop.close()


async def test_async_playwright(event_loop):
async with async_playwright() as playwright:
async with await playwright.chromium.launch() as browser:
async with await browser.new_context() as context:
Expand All @@ -31,16 +35,6 @@ async def test_async_playwright():
check_title(await page.title())


@pytest.mark.skip(reason='Not working')
def test_sync_playwright():
with sync_playwright() as playwright:
with playwright.chromium.launch() as browser:
with browser.new_context() as context:
with context.new_page() as page:
page.goto(URL)
check_title(page.title())


# https://playwright.dev/python/docs/test-runners#fixtures


Expand All @@ -49,71 +43,36 @@ async def test_is_working_playwright_async(playwright_async: Playwright):
assert isinstance(playwright_async, Playwright)


def test_is_working_playwright_sync(playwright: SPlaywright):
print(f'\n{playwright = }')
assert isinstance(playwright, SPlaywright)


async def test_is_working_browser_async(browser_async: Browser):
print(f'\n{browser_async = }')
assert isinstance(browser_async, Browser)


def test_is_working_browser_sync(browser: SBrowser):
print(f'\n{browser = }')
assert isinstance(browser, SBrowser)


async def test_is_working_browser_name_async(browser_name: str):
print(f'\n{browser_name = }')
assert isinstance(browser_name, str)


def test_is_working_browser_name(browser_name: str):
print(f'\n{browser_name = }')
assert isinstance(browser_name, str)


async def test_is_working_browser_channel_async(browser_channel: t.Optional[str]):
print(f'\n{browser_channel = }')
assert isinstance(browser_channel, str) or browser_channel is None


def test_is_working_browser_channel(browser_channel: t.Optional[str]):
print(f'\n{browser_channel = }')
assert isinstance(browser_channel, str) or browser_channel is None


async def test_is_working_context_async(context_async: BrowserContext):
print(f'\n{context_async = }')
assert isinstance(context_async, BrowserContext)


def test_is_working_context_sync(context: SBrowserContext):
print(f'\n{context = }')
assert isinstance(context, SBrowserContext)


async def test_is_working_browser_context_args_async(browser_context_args_async: dict):
print(f'\n{browser_context_args_async = }')
assert isinstance(browser_context_args_async, dict)


def test_is_working_browser_context_args_sync(browser_context_args: dict):
print(f'\n{browser_context_args = }')
assert isinstance(browser_context_args, dict)


async def test_is_working_page_async(page_async: Page):
print(f'\n{page_async = }')
assert isinstance(page_async, Page)


def test_is_working_page_sync(page: SPage):
print(f'\n{page = }')
assert isinstance(page, SPage)


###


Expand All @@ -127,17 +86,6 @@ async def test_browser_context_args_async(browser_context_args_async: t.Dict):
check_title(await page.title())


@pytest.mark.skip(reason='Not working')
def test_browser_context_args_sync(browser_context_args: t.Dict):
print(f'\n{browser_context_args, type(browser_context_args) = }')
with sync_playwright() as playwright:
with playwright.chromium.launch() as browser:
with browser.new_context(**browser_context_args) as context:
with context.new_page() as page:
page.goto(URL)
check_title(page.title())


async def test_playwright_async(playwright_async: Playwright):
print(f'\n{playwright_async, type(playwright_async) = }')
async with await playwright_async.chromium.launch() as browser:
Expand All @@ -147,15 +95,6 @@ async def test_playwright_async(playwright_async: Playwright):
check_title(await page.title())


def test_playwright_sync(playwright: SPlaywright):
print(f'\n{playwright, type(playwright) = }')
with playwright.chromium.launch() as browser:
with browser.new_context() as context:
with context.new_page() as page:
page.goto(URL)
check_title(page.title())


async def test_browser_type_async(browser_type_async: BrowserType):
print(f'\n{browser_type_async, type(browser_type_async) = }')
async with await browser_type_async.launch() as browser:
Expand All @@ -165,15 +104,6 @@ async def test_browser_type_async(browser_type_async: BrowserType):
check_title(await page.title())


def test_browser_type_sync(browser_type: SBrowserType):
print(f'\n{browser_type, type(browser_type) = }')
with browser_type.launch() as browser:
with browser.new_context() as context:
with context.new_page() as page:
page.goto(URL)
check_title(page.title())


async def test_launch_browser_async(launch_browser_async: t.Callable[..., t.Awaitable[Browser]]):
print(f'\n{launch_browser_async, type(launch_browser_async) = }')
browser = await launch_browser_async()
Expand All @@ -183,15 +113,6 @@ async def test_launch_browser_async(launch_browser_async: t.Callable[..., t.Awai
check_title(await page.title())


def test_launch_browser_sync(launch_browser: t.Callable[..., SBrowser]):
print(f'\n{launch_browser, type(launch_browser) = }')
browser = launch_browser()
with browser.new_context() as context:
with context.new_page() as page:
page.goto(URL)
check_title(page.title())


async def test_browser_async(browser_async: Browser):
print(f'\n{browser_async, type(browser_async) = }')
async with await browser_async.new_context() as context:
Expand All @@ -200,35 +121,14 @@ async def test_browser_async(browser_async: Browser):
check_title(await page.title())


def test_browser_sync(browser: SBrowser):
print(f'\n{browser, type(browser) = }')
with browser.new_context() as context:
with context.new_page() as page:
page.goto(URL)
check_title(page.title())


async def test_context_async(context_async: BrowserContext):
print(f'\n{context_async = }')
async with await context_async.new_page() as page:
await page.goto(URL)
check_title(await page.title())


def test_context_sync(context: SBrowserContext):
print(f'\n{context = }')
with context.new_page() as page:
page.goto(URL)
check_title(page.title())


async def test_page_async(page_async: Page):
print(f'\n{page_async = }')
await page_async.goto(URL)
check_title(await page_async.title())


def test_page_sync(page: SPage):
print(f'\n{page = }')
page.goto(URL)
check_title(page.title())
19 changes: 0 additions & 19 deletions tests/conftest.py

This file was deleted.

Empty file added tests/sync/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions tests/sync/test_for_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from playwright.sync_api import Page


def test_page_async(page: Page):
print(f'\n{page = }')
page.goto('https://playwright.dev/')
assert 'Playwright' in page.title()
Loading

0 comments on commit 397d236

Please sign in to comment.