斷言
斷言列表
自訂 Expect 訊息
您可以將自訂 expect 訊息指定為 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)