跳到主要內容

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")

方法

not_to_be_attached

新增於: v1.33 locatorAssertions.not_to_be_attached

expect(locator).to_be_attached() 相反。

用法

expect(locator).not_to_be_attached()
expect(locator).not_to_be_attached(**kwargs)

參數

  • attached bool (選填)#

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_checked

新增於: v1.20 locatorAssertions.not_to_be_checked

expect(locator).to_be_checked() 相反。

用法

expect(locator).not_to_be_checked()
expect(locator).not_to_be_checked(**kwargs)

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_disabled

新增於: v1.20 locatorAssertions.not_to_be_disabled

expect(locator).to_be_disabled() 相反。

用法

expect(locator).not_to_be_disabled()
expect(locator).not_to_be_disabled(**kwargs)

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_editable

新增於: v1.20 locatorAssertions.not_to_be_editable

expect(locator).to_be_editable() 相反。

用法

expect(locator).not_to_be_editable()
expect(locator).not_to_be_editable(**kwargs)

參數

  • editable bool (選填)新增於: v1.26#

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_empty

新增於: v1.20 locatorAssertions.not_to_be_empty

expect(locator).to_be_empty() 相反。

用法

expect(locator).not_to_be_empty()
expect(locator).not_to_be_empty(**kwargs)

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_enabled

新增於: v1.20 locatorAssertions.not_to_be_enabled

expect(locator).to_be_enabled() 相反。

用法

expect(locator).not_to_be_enabled()
expect(locator).not_to_be_enabled(**kwargs)

參數

  • enabled bool (選填)新增於: v1.26#

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_focused

新增於: v1.20 locatorAssertions.not_to_be_focused

expect(locator).to_be_focused() 相反。

用法

expect(locator).not_to_be_focused()
expect(locator).not_to_be_focused(**kwargs)

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_hidden

新增於: v1.20 locatorAssertions.not_to_be_hidden

expect(locator).to_be_hidden() 相反。

用法

expect(locator).not_to_be_hidden()
expect(locator).not_to_be_hidden(**kwargs)

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_in_viewport

新增於: v1.31 locatorAssertions.not_to_be_in_viewport

expect(locator).to_be_in_viewport() 相反。

用法

expect(locator).not_to_be_in_viewport()
expect(locator).not_to_be_in_viewport(**kwargs)

參數

  • ratio float (選填)#

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_be_visible

新增於: v1.20 locatorAssertions.not_to_be_visible

expect(locator).to_be_visible() 相反。

用法

expect(locator).not_to_be_visible()
expect(locator).not_to_be_visible(**kwargs)

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

  • visible bool (選填)新增於: v1.26#

返回


not_to_contain_text

新增於: v1.20 locatorAssertions.not_to_contain_text

expect(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#

    預期的子字串、RegExp 或其列表。

  • ignore_case bool (選填)新增於: v1.23#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標 (如果指定)。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

  • use_inner_text bool (選填)新增於: v1.18#

    擷取 DOM 節點文字時,是否使用 element.innerText 而非 element.textContent

返回


not_to_have_accessible_description

新增於: v1.44 locatorAssertions.not_to_have_accessible_description

expect(locator).to_have_accessible_description() 相反。

用法

expect(locator).not_to_have_accessible_description(name)
expect(locator).not_to_have_accessible_description(name, **kwargs)

參數

  • description str | Pattern#

    預期的可存取描述。

  • ignore_case bool (選填)#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標 (如果指定)。

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_have_accessible_name

新增於: v1.44 locatorAssertions.not_to_have_accessible_name

expect(locator).to_have_accessible_name() 相反。

用法

expect(locator).not_to_have_accessible_name(name)
expect(locator).not_to_have_accessible_name(name, **kwargs)

參數

  • name str | Pattern#

    預期的可存取名稱。

  • ignore_case bool (選填)#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標 (如果指定)。

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_have_attribute

新增於: v1.20 locatorAssertions.not_to_have_attribute

expect(locator).to_have_attribute() 相反。

用法

expect(locator).not_to_have_attribute(name, value)
expect(locator).not_to_have_attribute(name, value, **kwargs)

參數

  • name str新增於: v1.18#

    屬性名稱。

  • value str | Pattern新增於: v1.18#

    預期的屬性值。

  • ignore_case bool (選填)新增於: v1.40#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標 (如果指定)。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_have_class

新增於: v1.20 locatorAssertions.not_to_have_class

expect(locator).to_have_class() 相反。

用法

expect(locator).not_to_have_class(expected)
expect(locator).not_to_have_class(expected, **kwargs)

參數

返回


not_to_have_count

新增於: v1.20 locatorAssertions.not_to_have_count

expect(locator).to_have_count() 相反。

用法

expect(locator).not_to_have_count(count)
expect(locator).not_to_have_count(count, **kwargs)

參數

  • count int新增於: v1.18#

    預期的計數。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_have_css

新增於: v1.20 locatorAssertions.not_to_have_css

expect(locator).to_have_css() 相反。

用法

expect(locator).not_to_have_css(name, value)
expect(locator).not_to_have_css(name, value, **kwargs)

參數

  • name str新增於: v1.18#

    CSS 屬性名稱。

  • value str | Pattern新增於: v1.18#

    CSS 屬性值。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_have_id

新增於: v1.20 locatorAssertions.not_to_have_id

expect(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.20 locatorAssertions.not_to_have_js_property

expect(locator).to_have_js_property() 相反。

用法

expect(locator).not_to_have_js_property(name, value)
expect(locator).not_to_have_js_property(name, value, **kwargs)

參數

  • name str新增於: v1.18#

    屬性名稱。

  • value Any新增於: v1.18#

    屬性值。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_have_role

新增於: v1.44 locatorAssertions.not_to_have_role

expect(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 角色。

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


not_to_have_text

新增於: v1.20 locatorAssertions.not_to_have_text

expect(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#

    預期的字串或 RegExp 或其列表。

  • ignore_case bool (選填)新增於: v1.23#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標 (如果指定)。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

  • use_inner_text bool (選填)新增於: v1.18#

    擷取 DOM 節點文字時,是否使用 element.innerText 而非 element.textContent

返回


not_to_have_value

新增於: v1.20 locatorAssertions.not_to_have_value

expect(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.23 locatorAssertions.not_to_have_values

expect(locator).to_have_values() 相反。

用法

expect(locator).not_to_have_values(values)
expect(locator).not_to_have_values(values, **kwargs)

參數

返回


not_to_match_aria_snapshot

新增於: v1.49 locatorAssertions.not_to_match_aria_snapshot

expect(locator).to_match_aria_snapshot() 相反。

用法

expect(locator).not_to_match_aria_snapshot(expected)
expect(locator).not_to_match_aria_snapshot(expected, **kwargs)

參數

  • expected str#

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_attached

新增於: v1.33 locatorAssertions.to_be_attached

確保 Locator 指向的元素已連接到 Document 或 ShadowRoot。

用法

expect(page.get_by_text("Hidden text")).to_be_attached()

參數

  • attached bool (選填)#

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_checked

新增於: v1.20 locatorAssertions.to_be_checked

確保 Locator 指向已選取的輸入框。

用法

from playwright.sync_api import expect

locator = page.get_by_label("Subscribe to newsletter")
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.20 locatorAssertions.to_be_disabled

確保 Locator 指向已停用的元素。如果元素具有 "disabled" 屬性或透過 'aria-disabled' 停用,則該元素會停用。請注意,只有原生控制項元素 (例如 HTML buttoninputselecttextareaoptionoptgroup) 可以透過設定 "disabled" 屬性來停用。瀏覽器會忽略其他元素上的 "disabled" 屬性。

用法

from playwright.sync_api import expect

locator = page.locator("button.submit")
expect(locator).to_be_disabled()

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_editable

新增於: v1.20 locatorAssertions.to_be_editable

確保 Locator 指向可編輯的元素。

用法

from playwright.sync_api import expect

locator = page.get_by_role("textbox")
expect(locator).to_be_editable()

參數

  • editable bool (選填)新增於: v1.26#

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_empty

新增於: v1.20 locatorAssertions.to_be_empty

確保 Locator 指向空的、可編輯的元素,或指向沒有文字內容的 DOM 節點。

用法

from playwright.sync_api import expect

locator = page.locator("div.warning")
expect(locator).to_be_empty()

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_enabled

新增於: v1.20 locatorAssertions.to_be_enabled

確保 Locator 指向已啟用的元素。

用法

from playwright.sync_api import expect

locator = page.locator("button.submit")
expect(locator).to_be_enabled()

參數

  • enabled bool (選填)新增於: v1.26#

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_focused

新增於: v1.20 locatorAssertions.to_be_focused

確保 Locator 指向已聚焦的 DOM 節點。

用法

from playwright.sync_api import expect

locator = page.get_by_role("textbox")
expect(locator).to_be_focused()

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_hidden

新增於: v1.20 locatorAssertions.to_be_hidden

確保 Locator 要嘛沒有解析到任何 DOM 節點,要嘛就是解析到一個不可見的節點。

用法

from playwright.sync_api import expect

locator = page.locator('.my-element')
expect(locator).to_be_hidden()

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_in_viewport

新增於: v1.31 locatorAssertions.to_be_in_viewport

確保 Locator 指向的元素與 viewport 相交,根據 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)

參數

  • ratio float (選填)#

    元素與視窗相交的最小比例。如果等於 0,則元素應以任何正比例與視窗相交。預設為 0

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_be_visible

新增於: v1.20 locatorAssertions.to_be_visible

確保 Locator 指向已附加且可見 的 DOM 節點。

若要檢查列表中至少有一個元素可見,請使用 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()

參數

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

  • visible bool (選填)新增於: v1.26#

返回


to_contain_text

新增於: v1.20 locatorAssertions.to_contain_text

確保 Locator 指向的元素包含給定的文字。計算元素文字內容時,將考慮所有巢狀元素。您也可以為值使用正規表示式。

用法

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"))

如果您傳遞陣列作為預期值,則預期如下:

  1. Locator 解析為元素列表。
  2. 來自此列表子集的元素分別包含預期陣列中的文字。
  3. 匹配的元素子集與預期陣列具有相同的順序。
  4. 預期陣列中的每個文字值都與列表中的某些元素匹配。

例如,考慮以下列表

<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"])

參數

  • expected str | Pattern | List[str] | List[Pattern] | List[str | Pattern]新增於: v1.18#

    預期的子字串、RegExp 或其列表。

  • ignore_case bool (選填)新增於: v1.23#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

  • use_inner_text bool (選填)新增於: v1.18#

    擷取 DOM 節點文字時,是否使用 element.innerText 而非 element.textContent

返回

詳細資訊

expected 參數是字串時,Playwright 會在匹配之前,將實際文字和預期字串中的空格和換行符號正規化。當使用正規表示式時,實際文字會按原樣匹配。


to_have_accessible_description

新增於: v1.44 locatorAssertions.to_have_accessible_description

確保 Locator 指向的元素具有給定的可存取的描述

用法

locator = page.get_by_test_id("save-button")
expect(locator).to_have_accessible_description("Save results to disk")

參數

  • description str | Pattern#

    預期的可存取描述。

  • ignore_case bool (選填)#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_accessible_error_message

新增於: v1.50 locatorAssertions.to_have_accessible_error_message

確保 Locator 指向的元素具有給定的 aria 錯誤訊息

用法

locator = page.get_by_test_id("username-input")
expect(locator).to_have_accessible_error_message("Username is required.")

參數

  • error_message str | Pattern#

    預期的可存取錯誤訊息。

  • ignore_case bool (選填)#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_accessible_name

新增於: v1.44 locatorAssertions.to_have_accessible_name

確保 Locator 指向的元素具有給定的可存取的名稱

用法

locator = page.get_by_test_id("save-button")
expect(locator).to_have_accessible_name("Save to disk")

參數

  • name str | Pattern#

    預期的可存取名稱。

  • ignore_case bool (選填)#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_attribute

新增於: v1.20 locatorAssertions.to_have_attribute

確保 Locator 指向的元素具有給定的屬性。

用法

from playwright.sync_api import expect

locator = page.locator("input")
expect(locator).to_have_attribute("type", "text")

參數

  • name str新增於: v1.18#

    屬性名稱。

  • value str | Pattern新增於: v1.18#

    預期的屬性值。

  • ignore_case bool (選填)新增於: v1.40#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_class

新增於: v1.20 locatorAssertions.to_have_class

確保 Locator 指向的元素具有給定的 CSS 類別。當提供字串時,它必須完全匹配元素的 class 屬性。若要匹配個別類別或執行部分匹配,請使用正規表示式

用法

<div class='middle selected row' id='component'></div>
from playwright.sync_api import expect

locator = page.locator("#component")
expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)"))
expect(locator).to_have_class("middle selected row")

當傳遞陣列時,此方法會斷言找到的元素列表與預期的類別值列表相對應。每個元素的類別屬性都與陣列中對應的字串或正規表示式匹配

from playwright.sync_api import expect

locator = page.locator("list > .component")
expect(locator).to_have_class(["component", "component selected", "component"])

參數

返回


to_have_count

新增於: v1.20 locatorAssertions.to_have_count

確保 Locator 解析為確切數量的 DOM 節點。

用法

from playwright.sync_api import expect

locator = page.locator("list > .component")
expect(locator).to_have_count(3)

參數

  • count int新增於: v1.18#

    預期的計數。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_css

新增於: v1.20 locatorAssertions.to_have_css

確保 Locator 解析為具有給定計算 CSS 樣式的元素。

用法

from playwright.sync_api import expect

locator = page.get_by_role("button")
expect(locator).to_have_css("display", "flex")

參數

  • name str新增於: v1.18#

    CSS 屬性名稱。

  • value str | Pattern新增於: v1.18#

    CSS 屬性值。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_id

新增於: v1.20 locatorAssertions.to_have_id

確保 Locator 指向的元素具有給定的 DOM 節點 ID。

用法

from playwright.sync_api import expect

locator = page.get_by_role("textbox")
expect(locator).to_have_id("lastname")

參數

  • id str | Pattern新增於: v1.18#

    元素 ID。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_js_property

新增於: v1.20 locatorAssertions.to_have_js_property

確保 Locator 指向的元素具有給定的 JavaScript 屬性。請注意,此屬性可以是原始類型,也可以是純可序列化的 JavaScript 物件。

用法

from playwright.sync_api import expect

locator = page.locator(".component")
expect(locator).to_have_js_property("loaded", True)

參數

  • name str新增於: v1.18#

    屬性名稱。

  • value Any新增於: v1.18#

    屬性值。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_role

新增於: v1.44 locatorAssertions.to_have_role

確保 Locator 指向的元素具有給定的 ARIA 角色

請注意,角色是作為字串匹配的,忽略 ARIA 角色層次結構。例如,在具有子類別角色 "switch" 的元素上斷言超類別角色 "checkbox" 將會失敗。

用法

locator = page.get_by_test_id("save-button")
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 角色。

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_text

新增於: v1.20 locatorAssertions.to_have_text

確保 Locator 指向的元素具有給定的文字。計算元素文字內容時,將考慮所有巢狀元素。您也可以為值使用正規表示式。

用法

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, .*"))

如果您傳遞陣列作為預期值,則預期如下:

  1. Locator 解析為元素列表。
  2. 元素數量等於陣列中預期值的數量。
  3. 列表中的元素具有與預期陣列值匹配的文字,依序逐一匹配。

例如,考慮以下列表

<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"])

參數

  • expected str | Pattern | List[str] | List[Pattern] | List[str | Pattern]新增於: v1.18#

    預期的字串或 RegExp 或其列表。

  • ignore_case bool (選填)新增於: v1.23#

    是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

  • use_inner_text bool (選填)新增於: v1.18#

    擷取 DOM 節點文字時,是否使用 element.innerText 而非 element.textContent

返回

詳細資訊

expected 參數是字串時,Playwright 會在匹配之前,將實際文字和預期字串中的空格和換行符號正規化。當使用正規表示式時,實際文字會按原樣匹配。


to_have_value

新增於: v1.20 locatorAssertions.to_have_value

確保 Locator 指向的元素具有給定的輸入值。您也可以為值使用正規表示式。

用法

import re
from playwright.sync_api import expect

locator = page.locator("input[type=number]")
expect(locator).to_have_value(re.compile(r"[0-9]"))

參數

  • value str | Pattern新增於: v1.18#

    預期的值。

  • timeout float (選填)新增於: v1.18#

    以毫秒為單位重試斷言的時間。預設為 5000

返回


to_have_values

新增於: v1.23 locatorAssertions.to_have_values

確保 Locator 指向多選/組合方塊(即具有 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")])

參數

返回


to_match_aria_snapshot

新增於: v1.49 locatorAssertions.to_match_aria_snapshot

斷言目標元素與給定的可存取性快照匹配。

用法

page.goto("https://demo.playwright.dev/todomvc/")
expect(page.locator('body')).to_match_aria_snapshot('''
- heading "todos"
- textbox "What needs to be done?"
''')

參數

  • expected str#

  • timeout float (選填)#

    以毫秒為單位重試斷言的時間。預設為 5000

返回