斷言
斷言列表
自訂預期訊息
您可以指定自訂預期訊息作為 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)