Skip to content

Commit

Permalink
feat: update tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
m9810223 committed May 24, 2024
1 parent 732e0d1 commit 81e7dd4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pip install pytest-playwright-async

## Example

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

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

import pytest_asyncio
Expand All @@ -28,19 +28,19 @@ def event_loop(): # https://pytest-asyncio.readthedocs.io/en/latest/reference/f
loop = policy.new_event_loop()
yield loop
loop.close()

```

```py
# test_playwright.py
import pytest
# 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/')
assert (
await page_async.title()
== 'Fast and reliable end-to-end testing for modern web apps | Playwright'
)
assert 'Playwright' in await page_async.title()

```
9 changes: 9 additions & 0 deletions tests/test_for_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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/')
assert 'Playwright' in await page_async.title()
28 changes: 28 additions & 0 deletions tests/test_from_playwright_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
https://playwright.dev/python/docs/writing-tests#first-test
"""

import re

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/')

# Click the get started link.
await page_async.get_by_role('link', name='Get started').click()

# Expects page to have a heading with the name of Installation.
await expect(page_async.get_by_role('heading', name='Installation')).to_be_visible()
37 changes: 20 additions & 17 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@


URL = 'https://playwright.dev/'
TITLE = 'Fast and reliable end-to-end testing for modern web apps | Playwright'


def check_title(title):
assert 'Playwright' in title


@pytest.mark.asyncio
Expand All @@ -19,8 +22,8 @@ async def test_async_playwright():
async with await playwright.chromium.launch() as browser:
async with await browser.new_context() as context:
async with await context.new_page() as page:
await page.goto(URL, wait_until='networkidle')
assert await page.title() == TITLE
await page.goto(URL)
check_title(await page.title())


# https://playwright.dev/python/docs/test-runners#fixtures
Expand Down Expand Up @@ -78,8 +81,8 @@ async def test_browser_context_args_async(browser_context_args_async: t.Dict):
async with await playwright.chromium.launch() as browser:
async with await browser.new_context(**browser_context_args_async) as context:
async with await context.new_page() as page:
await page.goto(URL, wait_until='networkidle')
assert await page.title() == TITLE
await page.goto(URL)
check_title(await page.title())


@pytest.mark.asyncio
Expand All @@ -88,8 +91,8 @@ async def test_playwright_async(playwright_async: Playwright):
async with await playwright_async.chromium.launch() as browser:
async with await browser.new_context() as context:
async with await context.new_page() as page:
await page.goto(URL, wait_until='networkidle')
assert await page.title() == TITLE
await page.goto(URL)
check_title(await page.title())


@pytest.mark.asyncio
Expand All @@ -98,8 +101,8 @@ async def test_browser_type_async(browser_type_async: BrowserType):
async with await browser_type_async.launch() as browser:
async with await browser.new_context() as context:
async with await context.new_page() as page:
await page.goto(URL, wait_until='networkidle')
assert await page.title() == TITLE
await page.goto(URL)
check_title(await page.title())


@pytest.mark.asyncio
Expand All @@ -108,29 +111,29 @@ async def test_launch_browser_async(launch_browser_async: t.Callable[..., t.Awai
browser = await launch_browser_async()
async with await browser.new_context() as context:
async with await context.new_page() as page:
await page.goto(URL, wait_until='networkidle')
assert await page.title() == TITLE
await page.goto(URL)
check_title(await page.title())


@pytest.mark.asyncio
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:
async with await context.new_page() as page:
await page.goto(URL, wait_until='networkidle')
assert await page.title() == TITLE
await page.goto(URL)
check_title(await page.title())


@pytest.mark.asyncio
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, wait_until='networkidle')
assert await page.title() == TITLE
await page.goto(URL)
check_title(await page.title())


@pytest.mark.asyncio
async def test_page_async(page_async: Page):
print(f'\n{page_async = }')
await page_async.goto(URL, wait_until='networkidle')
assert await page_async.title() == TITLE
await page_async.goto(URL)
check_title(await page_async.title())

0 comments on commit 81e7dd4

Please sign in to comment.