断言
断言列表
自定义 Expect 消息
您可以为 expect
函数指定自定义 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 Actual value: None
E Call log:
E LocatorAssertions.to_be_visible with timeout 5000ms
E waiting for get_by_text("Name")
E waiting for 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)