LocatorAssertions
LocatorAssertions クラスは、テストで Locator の状態についてアサーションを行うために使用できるアサーションメソッドを提供します。
- 同期
- 非同期
from playwright.sync_api import Page, expect
def test_status_becomes_submitted(page: Page) -> None:
# ..
page.get_by_role("button").click()
expect(page.locator(".status")).to_have_text("Submitted")
from playwright.async_api import Page, expect
async def test_status_becomes_submitted(page: Page) -> None:
# ..
await page.get_by_role("button").click()
await expect(page.locator(".status")).to_have_text("Submitted")
メソッド
not_to_be_attached
追加バージョン: v1.33expect(locator).to_be_attached() の反対です。
使用法
expect(locator).not_to_be_attached()
expect(locator).not_to_be_attached(**kwargs)
引数
戻り値
not_to_be_checked
追加バージョン: v1.20expect(locator).to_be_checked() の反対です。
使用法
expect(locator).not_to_be_checked()
expect(locator).not_to_be_checked(**kwargs)
引数
戻り値
not_to_be_disabled
追加バージョン: v1.20expect(locator).to_be_disabled() の反対です。
使用法
expect(locator).not_to_be_disabled()
expect(locator).not_to_be_disabled(**kwargs)
引数
戻り値
not_to_be_editable
追加バージョン: v1.20expect(locator).to_be_editable() の反対です。
使用法
expect(locator).not_to_be_editable()
expect(locator).not_to_be_editable(**kwargs)
引数
戻り値
not_to_be_empty
追加バージョン: v1.20expect(locator).to_be_empty() の反対です。
使用法
expect(locator).not_to_be_empty()
expect(locator).not_to_be_empty(**kwargs)
引数
戻り値
not_to_be_enabled
追加バージョン: v1.20expect(locator).to_be_enabled() の反対です。
使用法
expect(locator).not_to_be_enabled()
expect(locator).not_to_be_enabled(**kwargs)
引数
戻り値
not_to_be_focused
追加バージョン: v1.20expect(locator).to_be_focused() の反対です。
使用法
expect(locator).not_to_be_focused()
expect(locator).not_to_be_focused(**kwargs)
引数
戻り値
not_to_be_hidden
追加バージョン: v1.20expect(locator).to_be_hidden() の反対です。
使用法
expect(locator).not_to_be_hidden()
expect(locator).not_to_be_hidden(**kwargs)
引数
戻り値
not_to_be_in_viewport
追加バージョン: v1.31expect(locator).to_be_in_viewport() の反対です。
使用法
expect(locator).not_to_be_in_viewport()
expect(locator).not_to_be_in_viewport(**kwargs)
引数
戻り値
not_to_be_visible
追加バージョン: v1.20expect(locator).to_be_visible() の反対です。
使用法
expect(locator).not_to_be_visible()
expect(locator).not_to_be_visible(**kwargs)
引数
戻り値
not_to_contain_class
追加バージョン: v1.52expect(locator).to_contain_class() の反対です。
使用法
expect(locator).not_to_contain_class(expected)
expect(locator).not_to_contain_class(expected, **kwargs)
引数
-
期待されるクラスまたは正規表現、またはそれらのリスト。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_contain_text
追加バージョン: v1.20expect(locator).to_contain_text() の反対です。
使用法
expect(locator).not_to_contain_text(expected)
expect(locator).not_to_contain_text(expected, **kwargs)
引数
-
expectedstr | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加: v1.18#期待される部分文字列または正規表現、またはそれらのリスト。
-
ignore_casebool (optional)追加日: v1.23#大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。 -
use_inner_textbool (optional)追加: v1.18#DOM ノードのテキストを取得するときに
element.textContentの代わりにelement.innerTextを使用するかどうか。
戻り値
not_to_have_accessible_description
追加バージョン: v1.44expect(locator).to_have_accessible_description() の反対です。
使用法
expect(locator).not_to_have_accessible_description(name)
expect(locator).not_to_have_accessible_description(name, **kwargs)
引数
-
期待されるアクセシブルな説明。
-
大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_have_accessible_error_message
追加バージョン: v1.50expect(locator).to_have_accessible_error_message() の反対です。
使用法
expect(locator).not_to_have_accessible_error_message(error_message)
expect(locator).not_to_have_accessible_error_message(error_message, **kwargs)
引数
-
期待されるアクセシブルなエラーメッセージ。
-
大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_have_accessible_name
追加バージョン: v1.44expect(locator).to_have_accessible_name() の反対です。
使用法
expect(locator).not_to_have_accessible_name(name)
expect(locator).not_to_have_accessible_name(name, **kwargs)
引数
-
期待されるアクセシブルな名前。
-
大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_have_attribute
追加バージョン: v1.20expect(locator).to_have_attribute() の反対です。
使用法
expect(locator).not_to_have_attribute(name, value)
expect(locator).not_to_have_attribute(name, value, **kwargs)
引数
-
属性名。
-
期待される属性値。
-
ignore_casebool (optional)追加されたバージョン: v1.40#大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_have_class
追加バージョン: v1.20expect(locator).to_have_class() の反対です。
使用法
expect(locator).not_to_have_class(expected)
expect(locator).not_to_have_class(expected, **kwargs)
引数
-
expectedstr | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加: v1.18#期待されるクラスまたは正規表現、またはそれらのリスト。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_have_count
追加バージョン: v1.20expect(locator).to_have_count() の反対です。
使用法
expect(locator).not_to_have_count(count)
expect(locator).not_to_have_count(count, **kwargs)
引数
戻り値
not_to_have_css
追加バージョン: v1.20expect(locator).to_have_css() の反対です。
使用法
expect(locator).not_to_have_css(name, value)
expect(locator).not_to_have_css(name, value, **kwargs)
引数
戻り値
not_to_have_id
追加バージョン: v1.20expect(locator).to_have_id() の反対です。
使用法
expect(locator).not_to_have_id(id)
expect(locator).not_to_have_id(id, **kwargs)
引数
戻り値
not_to_have_js_property
追加バージョン: v1.20expect(locator).to_have_js_property() の反対です。
使用法
expect(locator).not_to_have_js_property(name, value)
expect(locator).not_to_have_js_property(name, value, **kwargs)
引数
戻り値
not_to_have_role
追加バージョン: v1.44expect(locator).to_have_role() の反対です。
使用法
expect(locator).not_to_have_role(role)
expect(locator).not_to_have_role(role, **kwargs)
引数
-
role"alert" | "alertdialog" | "application" | "article" | "banner" | "blockquote" | "button" | "caption" | "cell" | "checkbox" | "code" | "columnheader" | "combobox" | "complementary" | "contentinfo" | "definition" | "deletion" | "dialog" | "directory" | "document" | "emphasis" | "feed" | "figure" | "form" | "generic" | "grid" | "gridcell" | "group" | "heading" | "img" | "insertion" | "link" | "list" | "listbox" | "listitem" | "log" | "main" | "marquee" | "math" | "meter" | "menu" | "menubar" | "menuitem" | "menuitemcheckbox" | "menuitemradio" | "navigation" | "none" | "note" | "option" | "paragraph" | "presentation" | "progressbar" | "radio" | "radiogroup" | "region" | "row" | "rowgroup" | "rowheader" | "scrollbar" | "search" | "searchbox" | "separator" | "slider" | "spinbutton" | "status" | "strong" | "subscript" | "superscript" | "switch" | "tab" | "table" | "tablist" | "tabpanel" | "term" | "textbox" | "time" | "timer" | "toolbar" | "tooltip" | "tree" | "treegrid" | "treeitem"#必須のariaロール。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_have_text
追加バージョン: v1.20expect(locator).to_have_text() の反対です。
使用法
expect(locator).not_to_have_text(expected)
expect(locator).not_to_have_text(expected, **kwargs)
引数
-
expectedstr | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加: v1.18#期待される文字列または正規表現、またはそれらのリスト。
-
ignore_casebool (optional)追加日: v1.23#大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。 -
use_inner_textbool (optional)追加: v1.18#DOM ノードのテキストを取得するときに
element.textContentの代わりにelement.innerTextを使用するかどうか。
戻り値
not_to_have_value
追加バージョン: v1.20expect(locator).to_have_value() の反対です。
使用法
expect(locator).not_to_have_value(value)
expect(locator).not_to_have_value(value, **kwargs)
引数
戻り値
not_to_have_values
追加日: v1.23expect(locator).to_have_values() の反対です。
使用法
expect(locator).not_to_have_values(values)
expect(locator).not_to_have_values(values, **kwargs)
引数
-
valuesList[str] | List[Pattern] | List[str | Pattern]#現在選択されている期待されるオプション。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
not_to_match_aria_snapshot
追加バージョン: v1.49expect(locator).to_match_aria_snapshot() の反対です。
使用法
expect(locator).not_to_match_aria_snapshot(expected)
expect(locator).not_to_match_aria_snapshot(expected, **kwargs)
引数
戻り値
to_be_attached
追加バージョン: v1.33Locator が、Document または ShadowRoot に 接続されている 要素を指すことを保証します。
使用法
- 同期
- 非同期
expect(page.get_by_text("Hidden text")).to_be_attached()
await expect(page.get_by_text("Hidden text")).to_be_attached()
引数
戻り値
to_be_checked
追加バージョン: v1.20Locator がチェックされた入力を指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.get_by_label("Subscribe to newsletter")
expect(locator).to_be_checked()
from playwright.async_api import expect
locator = page.get_by_label("Subscribe to newsletter")
await expect(locator).to_be_checked()
引数
-
checkedbool (optional)追加: v1.18#アサートする状態を提供します。デフォルトでは、入力がチェックされていることをアサートします。このオプションは、indeterminate が true に設定されている場合は使用できません。
-
indeterminatebool (optional)追加バージョン: v1.50#要素が不確定 (混在) 状態にあることをアサートします。チェックボックスとラジオボタンのみがサポートされています。このオプションは、checked が指定されている場合、true にすることはできません。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_be_disabled
追加バージョン: v1.20Locator が無効な要素を指していることを保証します。要素は "disabled" 属性を持つか、'aria-disabled' 経由で無効にされている場合に無効です。HTML の button、input、select、textarea、option、optgroup などのネイティブコントロール要素のみが "disabled" 属性を設定することで無効にできることに注意してください。他の要素の "disabled" 属性はブラウザによって無視されます。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_disabled()
from playwright.async_api import expect
locator = page.locator("button.submit")
await expect(locator).to_be_disabled()
引数
戻り値
to_be_editable
追加バージョン: v1.20Locator が編集可能な要素を指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.get_by_role("textbox")
expect(locator).to_be_editable()
from playwright.async_api import expect
locator = page.get_by_role("textbox")
await expect(locator).to_be_editable()
引数
戻り値
to_be_empty
追加バージョン: v1.20Locator が空の編集可能な要素、またはテキストがない DOM ノードを指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator("div.warning")
expect(locator).to_be_empty()
from playwright.async_api import expect
locator = page.locator("div.warning")
await expect(locator).to_be_empty()
引数
戻り値
to_be_enabled
追加バージョン: v1.20Locator が有効な要素を指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_enabled()
from playwright.async_api import expect
locator = page.locator("button.submit")
await expect(locator).to_be_enabled()
引数
戻り値
to_be_focused
追加バージョン: v1.20Locator がフォーカスされた DOM ノードを指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.get_by_role("textbox")
expect(locator).to_be_focused()
from playwright.async_api import expect
locator = page.get_by_role("textbox")
await expect(locator).to_be_focused()
引数
戻り値
to_be_hidden
追加バージョン: v1.20Locator が DOM ノードを解決しないか、非表示の ノードを解決することを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator('.my-element')
expect(locator).to_be_hidden()
from playwright.async_api import expect
locator = page.locator('.my-element')
await expect(locator).to_be_hidden()
引数
戻り値
to_be_in_viewport
追加バージョン: v1.31Locator が Intersection Observer API に従って、ビューポートと交差する要素を指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.get_by_role("button")
# Make sure at least some part of element intersects viewport.
expect(locator).to_be_in_viewport()
# Make sure element is fully outside of viewport.
expect(locator).not_to_be_in_viewport()
# Make sure that at least half of the element intersects viewport.
expect(locator).to_be_in_viewport(ratio=0.5)
from playwright.async_api import expect
locator = page.get_by_role("button")
# Make sure at least some part of element intersects viewport.
await expect(locator).to_be_in_viewport()
# Make sure element is fully outside of viewport.
await expect(locator).not_to_be_in_viewport()
# Make sure that at least half of the element intersects viewport.
await expect(locator).to_be_in_viewport(ratio=0.5)
引数
-
要素がビューポートと交差する最小比率。
0の場合、要素は任意の正の比率でビューポートと交差する必要があります。デフォルトは0です。 -
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_be_visible
追加バージョン: v1.20Locator が、アタッチされており、可視な DOM ノードを指していることを保証します。
リスト内の少なくとも1つの要素が表示されていることを確認するには、locator.first を使用します。
使用法
- 同期
- 非同期
# A specific element is visible.
expect(page.get_by_text("Welcome")).to_be_visible()
# At least one item in the list is visible.
expect(page.get_by_test_id("todo-item").first).to_be_visible()
# At least one of the two elements is visible, possibly both.
expect(
page.get_by_role("button", name="Sign in")
.or_(page.get_by_role("button", name="Sign up"))
.first
).to_be_visible()
# A specific element is visible.
await expect(page.get_by_text("Welcome")).to_be_visible()
# At least one item in the list is visible.
await expect(page.get_by_test_id("todo-item").first).to_be_visible()
# At least one of the two elements is visible, possibly both.
await expect(
page.get_by_role("button", name="Sign in")
.or_(page.get_by_role("button", name="Sign up"))
.first
).to_be_visible()
引数
戻り値
to_contain_class
追加バージョン: v1.52Locator が指定された CSS クラスを持つ要素を指していることを保証します。アサートされた値のすべてのクラス (スペースで区切られている) は、Element.classList に任意の順序で存在する必要があります。
使用法
<div class='middle selected row' id='component'></div>
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator("#component")
expect(locator).to_contain_class("middle selected row")
expect(locator).to_contain_class("selected")
expect(locator).to_contain_class("row middle")
from playwright.async_api import expect
locator = page.locator("#component")
await expect(locator).to_contain_class("middle selected row")
await expect(locator).to_contain_class("selected")
await expect(locator).to_contain_class("row middle")
配列が渡される場合、このメソッドは、特定された要素のリストが対応する期待されるクラスリストのリストと一致することをアサートします。各要素の class 属性は、配列内の対応するクラスと一致します。
<div class='list'>
<div class='component inactive'></div>
<div class='component active'></div>
<div class='component inactive'></div>
</div>
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator(".list > .component")
await expect(locator).to_contain_class(["inactive", "active", "inactive"])
from playwright.async_api import expect
locator = page.locator(".list > .component")
await expect(locator).to_contain_class(["inactive", "active", "inactive"])
引数
-
スペースで区切られた期待されるクラス名を含む文字列、または複数の要素をアサートするためのそのような文字列のリスト。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_contain_text
追加バージョン: v1.20Locator が指定されたテキストを含む要素を指していることを保証します。要素のテキストコンテンツを計算する際には、すべてのネストされた要素が考慮されます。値として正規表現を使用することもできます。
使用法
- 同期
- 非同期
import re
from playwright.sync_api import expect
locator = page.locator('.title')
expect(locator).to_contain_text("substring")
expect(locator).to_contain_text(re.compile(r"\d messages"))
import re
from playwright.async_api import expect
locator = page.locator('.title')
await expect(locator).to_contain_text("substring")
await expect(locator).to_contain_text(re.compile(r"\d messages"))
配列を期待値として渡した場合、期待値は次のとおりです。
- Locator は要素のリストに解決されます。
- このリストの**サブセット**の要素は、それぞれ期待される配列のテキストを含みます。
- 一致する要素のサブセットは、期待される配列と同じ順序になります。
- 期待される配列の各テキスト値は、リスト内のいずれかの要素によって一致します。
たとえば、次のリストを考えます。
<ul>
<li>Item Text 1</li>
<li>Item Text 2</li>
<li>Item Text 3</li>
</ul>
アサーションをどのように使用できるか見てみましょう。
- 同期
- 非同期
from playwright.sync_api import expect
# ✓ Contains the right items in the right order
expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3", "Text 4"])
# ✖ Wrong order
expect(page.locator("ul > li")).to_contain_text(["Text 3", "Text 2"])
# ✖ No item contains this text
expect(page.locator("ul > li")).to_contain_text(["Some 33"])
# ✖ Locator points to the outer list element, not to the list items
expect(page.locator("ul")).to_contain_text(["Text 3"])
from playwright.async_api import expect
# ✓ Contains the right items in the right order
await expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3", "Text 4"])
# ✖ Wrong order
await expect(page.locator("ul > li")).to_contain_text(["Text 3", "Text 2"])
# ✖ No item contains this text
await expect(page.locator("ul > li")).to_contain_text(["Some 33"])
# ✖ Locator points to the outer list element, not to the list items
await expect(page.locator("ul")).to_contain_text(["Text 3"])
引数
-
expectedstr | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加: v1.18#期待される部分文字列または正規表現、またはそれらのリスト。
-
ignore_casebool (optional)追加日: v1.23#大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。 -
use_inner_textbool (optional)追加: v1.18#DOM ノードのテキストを取得するときに
element.textContentの代わりにelement.innerTextを使用するかどうか。
戻り値
詳細
expected パラメータが文字列の場合、Playwright は、一致する前に、実際のテキストと期待される文字列の両方で空白と改行を正規化します。正規表現を使用する場合、実際のテキストはそのまま一致します。
to_have_accessible_description
追加バージョン: v1.44Locator が指定された アクセシブルな説明 を持つ要素を指していることを保証します。
使用法
- 同期
- 非同期
locator = page.get_by_test_id("save-button")
expect(locator).to_have_accessible_description("Save results to disk")
locator = page.get_by_test_id("save-button")
await expect(locator).to_have_accessible_description("Save results to disk")
引数
-
期待されるアクセシブルな説明。
-
大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_have_accessible_error_message
追加バージョン: v1.50Locator が指定された aria-errormessage を持つ要素を指していることを保証します。
使用法
- 同期
- 非同期
locator = page.get_by_test_id("username-input")
expect(locator).to_have_accessible_error_message("Username is required.")
locator = page.get_by_test_id("username-input")
await expect(locator).to_have_accessible_error_message("Username is required.")
引数
-
期待されるアクセシブルなエラーメッセージ。
-
大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_have_accessible_name
追加バージョン: v1.44Locator が指定された アクセシブルな名前 を持つ要素を指していることを保証します。
使用法
- 同期
- 非同期
locator = page.get_by_test_id("save-button")
expect(locator).to_have_accessible_name("Save to disk")
locator = page.get_by_test_id("save-button")
await expect(locator).to_have_accessible_name("Save to disk")
引数
-
期待されるアクセシブルな名前。
-
大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_have_attribute
追加バージョン: v1.20Locator が指定された属性を持つ要素を指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator("input")
expect(locator).to_have_attribute("type", "text")
from playwright.async_api import expect
locator = page.locator("input")
await expect(locator).to_have_attribute("type", "text")
引数
-
属性名。
-
期待される属性値。
-
ignore_casebool (optional)追加されたバージョン: v1.40#大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_have_class
追加バージョン: v1.20Locator が指定された CSS クラスを持つ要素を指していることを保証します。文字列が指定されている場合、要素の class 属性に完全に一致する必要があります。個々のクラスを照合するには expect(locator).to_contain_class() を使用してください。
使用法
<div class='middle selected row' id='component'></div>
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator("#component")
expect(locator).to_have_class("middle selected row")
expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)"))
from playwright.async_api import expect
locator = page.locator("#component")
await expect(locator).to_have_class("middle selected row")
await expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)"))
配列が渡される場合、このメソッドは、特定された要素のリストが、期待されるクラス値の対応するリストと一致することをアサートします。各要素のクラス属性は、配列内の対応する文字列または正規表現と一致します。
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator(".list > .component")
expect(locator).to_have_class(["component", "component selected", "component"])
from playwright.async_api import expect
locator = page.locator(".list > .component")
await expect(locator).to_have_class(["component", "component selected", "component"])
引数
-
expectedstr | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加: v1.18#期待されるクラスまたは正規表現、またはそれらのリスト。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_have_count
追加バージョン: v1.20Locator が正確な数の DOM ノードを解決することを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_count(3)
from playwright.async_api import expect
locator = page.locator("list > .component")
await expect(locator).to_have_count(3)
引数
戻り値
to_have_css
追加バージョン: v1.20Locator が、指定された計算済み CSS スタイルを持つ要素を解決することを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.get_by_role("button")
expect(locator).to_have_css("display", "flex")
from playwright.async_api import expect
locator = page.get_by_role("button")
await expect(locator).to_have_css("display", "flex")
引数
戻り値
to_have_id
追加バージョン: v1.20Locator が指定された DOM ノード ID を持つ要素を指していることを保証します。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.get_by_role("textbox")
expect(locator).to_have_id("lastname")
from playwright.async_api import expect
locator = page.get_by_role("textbox")
await expect(locator).to_have_id("lastname")
引数
戻り値
to_have_js_property
追加バージョン: v1.20Locator が指定された JavaScript プロパティを持つ要素を指していることを保証します。このプロパティは、プリミティブ型でもプレーンなシリアライズ可能な JavaScript オブジェクトでもかまいません。
使用法
- 同期
- 非同期
from playwright.sync_api import expect
locator = page.locator(".component")
expect(locator).to_have_js_property("loaded", True)
from playwright.async_api import expect
locator = page.locator(".component")
await expect(locator).to_have_js_property("loaded", True)
引数
戻り値
to_have_role
追加バージョン: v1.44Locator が指定された ARIA ロール を持つ要素を指していることを保証します。
ロールは文字列として一致し、ARIA ロール階層は無視されることに注意してください。たとえば、サブクラスロール "switch" を持つ要素に対してスーパークラスロール "checkbox" をアサートすると失敗します。
使用法
- 同期
- 非同期
locator = page.get_by_test_id("save-button")
expect(locator).to_have_role("button")
locator = page.get_by_test_id("save-button")
await expect(locator).to_have_role("button")
引数
-
role"alert" | "alertdialog" | "application" | "article" | "banner" | "blockquote" | "button" | "caption" | "cell" | "checkbox" | "code" | "columnheader" | "combobox" | "complementary" | "contentinfo" | "definition" | "deletion" | "dialog" | "directory" | "document" | "emphasis" | "feed" | "figure" | "form" | "generic" | "grid" | "gridcell" | "group" | "heading" | "img" | "insertion" | "link" | "list" | "listbox" | "listitem" | "log" | "main" | "marquee" | "math" | "meter" | "menu" | "menubar" | "menuitem" | "menuitemcheckbox" | "menuitemradio" | "navigation" | "none" | "note" | "option" | "paragraph" | "presentation" | "progressbar" | "radio" | "radiogroup" | "region" | "row" | "rowgroup" | "rowheader" | "scrollbar" | "search" | "searchbox" | "separator" | "slider" | "spinbutton" | "status" | "strong" | "subscript" | "superscript" | "switch" | "tab" | "table" | "tablist" | "tabpanel" | "term" | "textbox" | "time" | "timer" | "toolbar" | "tooltip" | "tree" | "treegrid" | "treeitem"#必須のariaロール。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_have_text
追加バージョン: v1.20Locator が指定されたテキストを持つ要素を指していることを保証します。要素のテキストコンテンツを計算する際には、すべてのネストされた要素が考慮されます。値として正規表現を使用することもできます。
使用法
- 同期
- 非同期
import re
from playwright.sync_api import expect
locator = page.locator(".title")
expect(locator).to_have_text(re.compile(r"Welcome, Test User"))
expect(locator).to_have_text(re.compile(r"Welcome, .*"))
import re
from playwright.async_api import expect
locator = page.locator(".title")
await expect(locator).to_have_text(re.compile(r"Welcome, Test User"))
await expect(locator).to_have_text(re.compile(r"Welcome, .*"))
配列を期待値として渡した場合、期待値は次のとおりです。
- Locator は要素のリストに解決されます。
- 要素の数は、配列内の期待値の数と等しくなります。
- リスト内の要素は、期待される配列の値を1つずつ順番に一致させます。
たとえば、次のリストを考えます。
<ul>
<li>Text 1</li>
<li>Text 2</li>
<li>Text 3</li>
</ul>
アサーションをどのように使用できるか見てみましょう。
- 同期
- 非同期
from playwright.sync_api import expect
# ✓ Has the right items in the right order
expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text 3"])
# ✖ Wrong order
expect(page.locator("ul > li")).to_have_text(["Text 3", "Text 2", "Text 1"])
# ✖ Last item does not match
expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text"])
# ✖ Locator points to the outer list element, not to the list items
expect(page.locator("ul")).to_have_text(["Text 1", "Text 2", "Text 3"])
from playwright.async_api import expect
# ✓ Has the right items in the right order
await expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text 3"])
# ✖ Wrong order
await expect(page.locator("ul > li")).to_have_text(["Text 3", "Text 2", "Text 1"])
# ✖ Last item does not match
await expect(page.locator("ul > li")).to_have_text(["Text 1", "Text 2", "Text"])
# ✖ Locator points to the outer list element, not to the list items
await expect(page.locator("ul")).to_have_text(["Text 1", "Text 2", "Text 3"])
引数
-
expectedstr | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加: v1.18#期待される文字列または正規表現、またはそれらのリスト。
-
ignore_casebool (optional)追加日: v1.23#大文字と小文字を区別しないマッチを実行するかどうか。ignore_case オプションは、指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeoutfloat (オプション)追加: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。 -
use_inner_textbool (optional)追加: v1.18#DOM ノードのテキストを取得するときに
element.textContentの代わりにelement.innerTextを使用するかどうか。
戻り値
詳細
expected パラメータが文字列の場合、Playwright は、一致する前に、実際のテキストと期待される文字列の両方で空白と改行を正規化します。正規表現を使用する場合、実際のテキストはそのまま一致します。
to_have_value
追加バージョン: v1.20Locator が指定された入力値を持つ要素を指していることを保証します。値として正規表現を使用することもできます。
使用法
- 同期
- 非同期
import re
from playwright.sync_api import expect
locator = page.locator("input[type=number]")
expect(locator).to_have_value(re.compile(r"[0-9]"))
import re
from playwright.async_api import expect
locator = page.locator("input[type=number]")
await expect(locator).to_have_value(re.compile(r"[0-9]"))
引数
戻り値
to_have_values
追加日: v1.23Locator が複数選択/コンボボックス (つまり、multiple 属性を持つ select) を指しており、指定された値が選択されていることを保証します。
使用法
たとえば、次の要素を考えます。
<select id="favorite-colors" multiple>
<option value="R">Red</option>
<option value="G">Green</option>
<option value="B">Blue</option>
</select>
- 同期
- 非同期
import re
from playwright.sync_api import expect
locator = page.locator("id=favorite-colors")
locator.select_option(["R", "G"])
expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")])
import re
from playwright.async_api import expect
locator = page.locator("id=favorite-colors")
await locator.select_option(["R", "G"])
await expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")])
引数
-
valuesList[str] | List[Pattern] | List[str | Pattern]#現在選択されている期待されるオプション。
-
アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000です。
戻り値
to_match_aria_snapshot
追加バージョン: v1.49ターゲット要素が指定された アクセシビリティスナップショット と一致することをアサートします。
使用法
- 同期
- 非同期
page.goto("https://demo.playwright.dev/todomvc/")
expect(page.locator('body')).to_match_aria_snapshot('''
- heading "todos"
- textbox "What needs to be done?"
''')
await page.goto("https://demo.playwright.dev/todomvc/")
await expect(page.locator('body')).to_match_aria_snapshot('''
- heading "todos"
- textbox "What needs to be done?"
''')
引数
戻り値