跳到主要内容

编写测试

简介

Playwright 测试很简单,它们

  • 执行操作,并且
  • 断言状态是否符合预期。

在执行操作之前,无需等待任何事情:Playwright 会自动等待各种可操作性检查通过,然后再执行每个操作。

在执行检查时,也无需处理竞争条件 - Playwright 断言的设计方式使其描述了最终需要满足的期望。

就是这样!这些设计选择使 Playwright 用户可以完全忘记测试中不稳定的超时和竞争性检查。

您将学习

第一个测试

查看以下示例,了解如何编写测试。请注意文件名如何遵循 test_ 前缀约定以及每个测试名称。

test_example.py
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 使用定位器 API来实现。定位器表示在任何时刻查找页面上的元素的方式,了解有关不同类型的可用定位器的更多信息。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 操作列表。请注意,还有更多操作,因此请务必查看定位器 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"))

这是最流行的异步断言列表。请注意,还有更多断言可供熟悉

断言描述
expect(locator).to_be_checked()复选框被选中
expect(locator).to_be_enabled()控件已启用
expect(locator).to_be_visible()元素可见
expect(locator).to_contain_text()元素包含文本
expect(locator).to_have_attribute()元素具有属性
expect(locator).to_have_count()元素列表具有给定长度
expect(locator).to_have_text()元素匹配文本
expect(locator).to_have_value()输入元素具有值
expect(page).to_have_title()页面具有标题
expect(page).to_have_url()页面具有 URL

测试隔离

Playwright Pytest 插件基于测试 fixture 的概念,例如内置的 page fixture,它被传递到您的测试中。由于浏览器上下文,页面在测试之间是隔离的,浏览器上下文等同于一个全新的浏览器配置文件,即使多个测试在单个浏览器中运行,每个测试都会获得一个全新的环境。

test_example.py
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.

使用 fixtures

您可以使用各种fixtures在测试之前或之后执行代码,并在它们之间共享对象。例如,具有 autouse 的 function 作用域的 fixture 的行为类似于 beforeEach/afterEach。而具有 autouse 的 module 作用域的 fixture 的行为类似于 beforeAll/afterAll,它在所有测试之前和之后运行。

test_example.py
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/")

下一步