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

アサーション

アサーションの一覧

アサーション説明
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_text()要素がテキストを含む
expect(locator).to_have_accessible_description()要素が一致するアクセシブルな説明を持つ
expect(locator).to_have_accessible_name()要素が一致するアクセシブルな名前を持つ
expect(locator).to_have_attribute()要素が DOM 属性を持つ
expect(locator).to_have_class()要素が 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(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)