断言
断言列表
¥List of assertions
自定义 Expect 消息
¥Custom Expect Message
你可以指定自定义期望消息作为 expect
函数的第二个参数,例如:
¥You can specify a custom expect message as a second argument to the expect
function, for example:
expect(page.get_by_text("Name"), "should be logged in").to_be_visible()
当 Expect 失败时,错误将如下所示:
¥When expect fails, the error would look like this:
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
设置自定义超时
¥Setting a custom timeout
你可以为全局或每个断言指定自定义超时。默认超时时间为 5 秒。
¥You can specify a custom timeout for assertions either globally or per assertion. The default timeout is 5 seconds.
全局超时
¥Global timeout
conftest.py
from playwright.sync_api import expect
expect.set_options(timeout=10_000)
每个断言超时
¥Per assertion timeout
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)