From 81e7dd41d7b129964338ed7359dded6a886e0a6f Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 May 2024 00:33:31 +0800 Subject: [PATCH] feat: update tests and docs --- README.md | 16 ++++++------- tests/test_for_readme.py | 9 ++++++++ tests/test_from_playwright_docs.py | 28 ++++++++++++++++++++++ tests/test_playwright.py | 37 ++++++++++++++++-------------- 4 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 tests/test_for_readme.py create mode 100644 tests/test_from_playwright_docs.py diff --git a/README.md b/README.md index 7261be1..f34508f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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() + ``` diff --git a/tests/test_for_readme.py b/tests/test_for_readme.py new file mode 100644 index 0000000..2f238e5 --- /dev/null +++ b/tests/test_for_readme.py @@ -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() diff --git a/tests/test_from_playwright_docs.py b/tests/test_from_playwright_docs.py new file mode 100644 index 0000000..6cdca49 --- /dev/null +++ b/tests/test_from_playwright_docs.py @@ -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() diff --git a/tests/test_playwright.py b/tests/test_playwright.py index dad345d..ea1c13e 100644 --- a/tests/test_playwright.py +++ b/tests/test_playwright.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -108,8 +111,8 @@ 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 @@ -117,20 +120,20 @@ 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())