断言
断言列表
自定义期望信息
你可以在 expect
函数中将自定义期望信息作为第二个参数指定,例如:
expect(page.get_by_text("Name"), "should be logged in").to_be_visible()
当 expect 失败时,错误信息会如下所示:
def test_foobar(page: Page) -> None:
> expect(page.get_by_text("Name"), "should be logged in").to_be_visible()
E AssertionError: should be logged in
E 实际值: None
E 调用日志:
E LocatorAssertions.to_be_visible with timeout 5000ms
E 等待 get_by_text("Name")
E 等待 get_by_text("Name")
tests/test_foobar.py:22: AssertionError
设置自定义超时时间
你可以为断言全局或单个断言设置自定义超时时间。默认超时时间为 5 秒。
全局超时时间
conftest.py
from playwright.sync_api import expect
expect.set_options(timeout=10_000)
单个断言超时时间
test_foobar.py
from playwright.sync_api import expect
def test_foobar(page: Page) -> None:
expect(page.get_by_text("Name")).to_be_visible(timeout=10_000)