断言
断言列表
自定义期望消息
您可以将自定义期望消息作为 expect
函数的第二个参数指定,例如
expect(page.get_by_text("Name"), "should be logged in").to_be_visible()
当期望失败时,错误将如下所示
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)