/
test_playwright.py
128 lines (99 loc) · 4.83 KB
/
test_playwright.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import typing as t
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
import pytest
URL = 'https://playwright.dev/'
TITLE = 'Fast and reliable end-to-end testing for modern web apps | Playwright'
@pytest.mark.asyncio
async def test_async_playwright():
async with async_playwright() as 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
@pytest.mark.asyncio
async def test_my_app_is_working( # https://playwright.dev/python/docs/test-runners#fixtures
playwright_async: Playwright,
browser_async: Browser,
browser_name: str,
browser_channel: t.Optional[str],
context_async: BrowserContext,
browser_context_args_async: dict,
page_async: Page,
):
print(f"\n{playwright_async = }")
assert type(playwright_async) == Playwright
print(f"\n{browser_async = }")
assert type(browser_async) == Browser
print(f"\n{browser_name = }")
print(f"\n{browser_channel = }")
print(f"\n{context_async = }")
assert type(context_async) == BrowserContext
print(f"\n{browser_context_args_async = }")
print(f"\n{page_async = }")
assert type(page_async) == Page
@pytest.mark.asyncio
async def test_browser_type_launch_args_async(browser_type_launch_args_async: t.Dict):
print(f"\n{browser_type_launch_args_async, type(browser_type_launch_args_async) = }")
async with async_playwright() as playwright:
async with await playwright.chromium.launch(**browser_type_launch_args_async) 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
@pytest.mark.asyncio
async def test_browser_context_args_async(browser_context_args_async: t.Dict):
print(f"\n{browser_context_args_async, type(browser_context_args_async) = }")
async with async_playwright() as playwright:
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
@pytest.mark.asyncio
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:
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
@pytest.mark.asyncio
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:
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
@pytest.mark.asyncio
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()
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
@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
@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
@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