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

アサーション

アサーションのリスト

アサーション説明
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()要素にクラスプロパティがある
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`関数の2番目の引数としてカスタム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)