断言
断言列表
自定义期望消息
你可以指定一个自定义的期望消息作为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)