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)
引数
-
expected
str | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加バージョン: v1.18#期待される部分文字列または正規表現、あるいはそれらのリスト。
-
ignore_case
bool (オプション)追加バージョン: v1.23#大文字と小文字を区別しない一致を実行するかどうか。ignore_case オプションが指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。 -
use_inner_text
bool (オプション)追加バージョン: 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)
引数
-
属性名。
-
value
str | Pattern追加バージョン: v1.18#期待される属性値。
-
ignore_case
bool (オプション)追加バージョン: v1.40#大文字と小文字を区別しない一致を実行するかどうか。ignore_case オプションが指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeout
float (オプション)追加バージョン: 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)
引数
-
expected
str | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加バージョン: v1.18#期待されるクラスまたは正規表現、あるいはそれらのリスト。
-
timeout
float (オプション)追加バージョン: 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)
引数
-
CSSプロパティ名。
-
value
str | Pattern追加バージョン: v1.18#CSSプロパティ値。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。
戻り値
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)
引数
-
id
str | Pattern追加バージョン: v1.18#要素ID。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。
戻り値
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)
引数
-
expected
str | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加バージョン: v1.18#期待される文字列または正規表現、あるいはそれらのリスト。
-
ignore_case
bool (オプション)追加バージョン: v1.23#大文字と小文字を区別しない一致を実行するかどうか。ignore_case オプションが指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。 -
use_inner_text
bool (オプション)追加バージョン: 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)
引数
-
value
str | Pattern追加バージョン: v1.18#期待される値。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。
戻り値
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)
引数
-
values
List[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()
引数
-
checked
bool (オプション)追加バージョン: v1.18#アサートする状態を提供します。デフォルトでは入力がチェックされていることをアサートします。このオプションは、indeterminate が true に設定されている場合には使用できません。
-
indeterminate
bool (オプション)追加バージョン: v1.50#要素が不確定(混合)状態であることをアサートします。チェックボックスとラジオボタンでのみサポートされています。このオプションは、checked が指定されている場合には true にできません。
-
timeout
float (オプション)追加バージョン: 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>
<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"))
期待値として配列を渡す場合、期待される動作は次のとおりです。
- ロケーターは要素のリストに解決されます。
- このリストのサブセットからの要素が、期待される配列からのテキストをそれぞれ含みます。
- 一致する要素のサブセットは、期待される配列と同じ順序を持っています。
- 期待される配列からの各テキスト値は、リストから何らかの要素によって一致します。
例として、次のリストを考えてみましょう。
<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"])
引数
-
expected
str | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加バージョン: v1.18#期待される部分文字列または正規表現、あるいはそれらのリスト。
-
ignore_case
bool (オプション)追加バージョン: v1.23#大文字と小文字を区別しない一致を実行するかどうか。ignore_case オプションが指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。 -
use_inner_text
bool (オプション)追加バージョン: 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")
引数
-
属性名。
-
value
str | Pattern追加バージョン: v1.18#期待される属性値。
-
ignore_case
bool (オプション)追加バージョン: v1.40#大文字と小文字を区別しない一致を実行するかどうか。ignore_case オプションが指定されている場合、対応する正規表現フラグよりも優先されます。
-
timeout
float (オプション)追加バージョン: 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|$)"))
配列が渡された場合、このメソッドは、特定された要素のリストが対応する期待されるクラス値のリストと一致することをアサートします。各要素のclass属性は、配列内の対応する文字列または正規表現と照合されます。
- 同期
- 非同期
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"])
引数
-
expected
str | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加バージョン: v1.18#期待されるクラスまたは正規表現、あるいはそれらのリスト。
-
timeout
float (オプション)追加バージョン: 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")
引数
-
CSSプロパティ名。
-
value
str | Pattern追加バージョン: v1.18#CSSプロパティ値。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。
戻り値
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")
引数
-
id
str | Pattern追加バージョン: v1.18#要素ID。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。
戻り値
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, .*"))
期待値として配列を渡す場合、期待される動作は次のとおりです。
- ロケーターは要素のリストに解決されます。
- 要素の数は、配列内の期待される値の数と等しくなります。
- リスト内の要素は、期待される配列の値と、順に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"])
引数
-
expected
str | Pattern | List[str] | List[Pattern] | List[str | Pattern]追加バージョン: v1.18#期待される文字列または正規表現、あるいはそれらのリスト。
-
ignore_case
bool (オプション)追加バージョン: v1.23#大文字と小文字を区別しない一致を実行するかどうか。ignore_caseオプションは、指定された場合、対応する正規表現フラグよりも優先されます。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。 -
use_inner_text
bool (オプション)追加バージョン: 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]"))
引数
-
value
str | Pattern追加バージョン: v1.18#期待される値。
-
timeout
float (オプション)追加バージョン: v1.18#アサーションを再試行する時間(ミリ秒単位)。デフォルトは
5000
です。
戻り値
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")])
引数
-
values
List[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?"
''')
引数
戻り値