斷言
斷言列表
自訂期望訊息
您可以指定一個自訂的預期訊息作為 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
設定 a custom timeout
您可以為斷言指定自訂的超時時間,無論是全域還是每個斷言。預設的超時時間為 5 秒。
全域 timeout
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)