编写测试
简介
Playwright 测试很简单,它们
- 执行操作,并且
- 根据预期断言状态。
在执行操作之前,无需等待任何事情:Playwright 会自动等待各种可操作性检查通过,然后才执行每个操作。
在执行检查时也无需处理竞争条件——Playwright 断言的设计方式是描述需要最终满足的预期。
就是这样!这些设计选择让 Playwright 用户在他们的测试中完全忘记不稳定的超时和竞争检查。
您将学到
第一个测试
查看以下示例,了解如何编写测试。请注意文件名如何遵循 test_
前缀约定以及每个测试名称。
import re
from playwright.sync_api import Page, expect
def test_has_title(page: Page):
page.goto("https://playwright.net.cn/")
# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))
def test_get_started_link(page: Page):
page.goto("https://playwright.net.cn/")
# Click the get started link.
page.get_by_role("link", name="Get started").click()
# Expects page to have a heading with the name of Installation.
expect(page.get_by_role("heading", name="Installation")).to_be_visible()
操作
导航
大多数测试将从导航页面到 URL 开始。之后,测试将能够与页面元素进行交互。
page.goto("https://playwright.net.cn/")
Playwright 将等待页面达到加载状态,然后才继续。了解有关 page.goto() 选项的更多信息。
交互
执行操作从定位元素开始。Playwright 使用 Locators API 来实现这一点。Locators 表示随时在页面上查找元素的方式,了解有关可用不同类型定位器的更多信息。Playwright 将等待元素可操作,然后才执行操作,因此无需等待它可用。
# Create a locator.
get_started = page.get_by_role("link", name="Get started")
# Click it.
get_started.click()
在大多数情况下,它会写在一行中
page.get_by_role("link", name="Get started").click()
基本操作
这是最流行的 Playwright 操作列表。请注意,还有更多,因此请务必查看 Locator API 部分以了解更多信息。
操作 | 描述 |
---|---|
locator.check() | 选中输入复选框 |
locator.click() | 点击元素 |
locator.uncheck() | 取消选中输入复选框 |
locator.hover() | 鼠标悬停在元素上 |
locator.fill() | 填写表单字段,输入文本 |
locator.focus() | 聚焦元素 |
locator.press() | 按下单个键 |
locator.set_input_files() | 选择要上传的文件 |
locator.select_option() | 在下拉列表中选择选项 |
断言
Playwright 包含断言,它们将等待直到满足预期条件。使用这些断言可以使测试不不稳定且具有弹性。例如,此代码将等待页面获得包含“Playwright”的标题
import re
from playwright.sync_api import expect
expect(page).to_have_title(re.compile("Playwright"))
这是最流行的异步断言列表。请注意,还有更多需要熟悉
测试隔离
Playwright Pytest 插件基于测试夹具的概念,例如内置页面夹具,它被传递到您的测试中。由于浏览器上下文,页面在测试之间是隔离的,这相当于一个全新的浏览器配置文件,即使多个测试在单个浏览器中运行,每个测试也能获得一个全新的环境。
from playwright.sync_api import Page
def test_example_test(page: Page):
pass
# "page" belongs to an isolated BrowserContext, created for this specific test.
def test_another_test(page: Page):
pass
# "page" in this second test is completely isolated from the first test.
使用夹具
您可以使用各种夹具在测试之前或之后执行代码,并在它们之间共享对象。一个 function
作用域的夹具,例如带有 autouse,其行为类似于 beforeEach/afterEach。一个 module
作用域的夹具带有 autouse,其行为类似于 beforeAll/afterAll,它在所有测试之前和之后运行。
import pytest
from playwright.sync_api import Page, expect
@pytest.fixture(scope="function", autouse=True)
def before_each_after_each(page: Page):
print("before the test runs")
# Go to the starting url before each test.
page.goto("https://playwright.net.cn/")
yield
print("after the test runs")
def test_main_navigation(page: Page):
# Assertions use the expect API.
expect(page).to_have_url("https://playwright.net.cn/")