メインコンテンツにスキップ

アサーション

アサーションのリスト

アサーション説明
expect(locator).to_be_attached()要素が添付されている
expect(locator).to_be_checked()チェックボックスがチェックされている
expect(locator).to_be_disabled()要素が無効になっている
expect(locator).to_be_editable()要素が編集可能である
expect(locator).to_be_empty()コンテナが空である
expect(locator).to_be_enabled()要素が有効である
expect(locator).to_be_focused()要素がフォーカスされている
expect(locator).to_be_hidden()要素が非表示である
expect(locator).to_be_in_viewport()要素がビューポートと交差している
expect(locator).to_be_visible()要素が表示されている
expect(locator).to_contain_class()要素に指定されたCSSクラスが含まれる
expect(locator).to_contain_text()要素にテキストが含まれている
expect(locator).to_have_accessible_description()要素に一致するアクセシブルな説明がある
expect(locator).to_have_accessible_name()要素に一致するアクセシブルな名前がある
expect(locator).to_have_attribute()要素が DOM 属性を持つ
expect(locator).to_have_class()要素がクラスプロパティを持つ
expect(locator).to_have_count()リストが正確な数の子要素を持つ
expect(locator).to_have_css()要素が CSS プロパティを持つ
expect(locator).to_have_id()要素が ID を持つ
expect(locator).to_have_js_property()要素が JavaScript プロパティを持つ
expect(locator).to_have_role()要素に特定のARIAロールがある
expect(locator).to_have_text()要素がテキストに一致する
expect(locator).to_have_value()入力に値がある
expect(locator).to_have_values()選択にオプションが選択されている
expect(locator).to_match_aria_snapshot()要素が提供されたAriaスナップショットに一致
expect(page).to_have_title()ページにタイトルがある
expect(page).to_have_url()ページに URL がある
expect(response).to_be_ok()レスポンスが OK ステータスを持つ

カスタムExpectメッセージ

カスタムExpectメッセージは、expect関数の2番目の引数として指定できます。例:

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)