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.33與 expect(locator).to_be_attached() 相反。
用法
expect(locator).not_to_be_attached()
expect(locator).not_to_be_attached(**kwargs)
參數
返回
not_to_be_checked
新增於: v1.20與 expect(locator).to_be_checked() 相反。
用法
expect(locator).not_to_be_checked()
expect(locator).not_to_be_checked(**kwargs)
參數
返回
not_to_be_disabled
新增於: v1.20與 expect(locator).to_be_disabled() 相反。
用法
expect(locator).not_to_be_disabled()
expect(locator).not_to_be_disabled(**kwargs)
參數
返回
not_to_be_editable
新增於: v1.20與 expect(locator).to_be_editable() 相反。
用法
expect(locator).not_to_be_editable()
expect(locator).not_to_be_editable(**kwargs)
參數
返回
not_to_be_empty
新增於: v1.20與 expect(locator).to_be_empty() 相反。
用法
expect(locator).not_to_be_empty()
expect(locator).not_to_be_empty(**kwargs)
參數
返回
not_to_be_enabled
新增於: v1.20與 expect(locator).to_be_enabled() 相反。
用法
expect(locator).not_to_be_enabled()
expect(locator).not_to_be_enabled(**kwargs)
參數
返回
not_to_be_focused
新增於: v1.20與 expect(locator).to_be_focused() 相反。
用法
expect(locator).not_to_be_focused()
expect(locator).not_to_be_focused(**kwargs)
參數
返回
not_to_be_hidden
新增於: v1.20與 expect(locator).to_be_hidden() 相反。
用法
expect(locator).not_to_be_hidden()
expect(locator).not_to_be_hidden(**kwargs)
參數
返回
not_to_be_in_viewport
新增於: v1.31與 expect(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.20與 expect(locator).to_be_visible() 相反。
用法
expect(locator).not_to_be_visible()
expect(locator).not_to_be_visible(**kwargs)
參數
返回
not_to_contain_text
新增於: v1.20與 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 選項優先於對應的正規表示式旗標 (如果指定)。
-
以毫秒為單位重試斷言的時間。預設為
5000
。 -
use_inner_text
bool (選填)新增於: v1.18#擷取 DOM 節點文字時,是否使用
element.innerText
而非element.textContent
。
返回
not_to_have_accessible_description
新增於: v1.44與 expect(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_name
新增於: v1.44與 expect(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.20與 expect(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 選項優先於對應的正規表示式旗標 (如果指定)。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
not_to_have_class
新增於: v1.20與 expect(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#預期的類別或 RegExp 或其列表。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
not_to_have_count
新增於: v1.20與 expect(locator).to_have_count() 相反。
用法
expect(locator).not_to_have_count(count)
expect(locator).not_to_have_count(count, **kwargs)
參數
返回
not_to_have_css
新增於: v1.20與 expect(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.20與 expect(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.20與 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)
參數
-
屬性名稱。
-
屬性值。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
not_to_have_role
新增於: v1.44與 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 角色。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
not_to_have_text
新增於: v1.20與 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 選項優先於對應的正規表示式旗標 (如果指定)。
-
以毫秒為單位重試斷言的時間。預設為
5000
。 -
use_inner_text
bool (選填)新增於: v1.18#擷取 DOM 節點文字時,是否使用
element.innerText
而非element.textContent
。
返回
not_to_have_value
新增於: v1.20與 expect(locator).to_have_value() 相反。
用法
expect(locator).not_to_have_value(value)
expect(locator).not_to_have_value(value, **kwargs)
參數
返回
not_to_have_values
新增於: v1.23與 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與 expect(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.33確保 Locator 指向的元素已連接到 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.20確保 Locator 指向已選取的輸入框。
用法
- 同步
- 非同步
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()
參數
-
提供要斷言的狀態。預設斷言輸入框為已選取。當 indeterminate 設定為 true 時,無法使用此選項。
-
indeterminate
bool (選填)新增於: v1.50#斷言元素處於不確定 (混合) 狀態。僅支援核取方塊和選項按鈕。當提供 checked 時,此選項不能為 true。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
to_be_disabled
新增於: v1.20確保 Locator 指向已停用的元素。如果元素具有 "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.20確保 Locator 指向可編輯的元素。
用法
- 同步
- 非同步
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.20確保 Locator 指向空的、可編輯的元素,或指向沒有文字內容的 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.20確保 Locator 指向已啟用的元素。
用法
- 同步
- 非同步
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.20確保 Locator 指向已聚焦的 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.20確保 Locator 要嘛沒有解析到任何 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.31確保 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)
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.20若要檢查列表中至少有一個元素可見,請使用 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_text
新增於: v1.20確保 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"))
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"])
參數
-
expected
str | Pattern | List[str] | List[Pattern] | List[str | Pattern]新增於: v1.18#預期的子字串、RegExp 或其列表。
-
ignore_case
bool (選填)新增於: v1.23#是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。
-
以毫秒為單位重試斷言的時間。預設為
5000
。 -
use_inner_text
bool (選填)新增於: v1.18#擷取 DOM 節點文字時,是否使用
element.innerText
而非element.textContent
。
返回
詳細資訊
當 expected
參數是字串時,Playwright 會在匹配之前,將實際文字和預期字串中的空格和換行符號正規化。當使用正規表示式時,實際文字會按原樣匹配。
to_have_accessible_description
新增於: v1.44用法
- 同步
- 非同步
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.50確保 Locator 指向的元素具有給定的 aria 錯誤訊息。
用法
- 同步
- 非同步
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.44用法
- 同步
- 非同步
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.20確保 Locator 指向的元素具有給定的屬性。
用法
- 同步
- 非同步
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 選項優先於對應的正規表示式旗標(如果指定)。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
to_have_class
新增於: v1.20確保 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.async_api import expect
locator = page.locator("#component")
await expect(locator).to_have_class(re.compile(r"(^|\\s)selected(\\s|$)"))
await 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"])
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#預期的類別或 RegExp 或其列表。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
to_have_count
新增於: v1.20確保 Locator 解析為確切數量的 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.20確保 Locator 解析為具有給定計算 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.20確保 Locator 指向的元素具有給定的 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.20確保 Locator 指向的元素具有給定的 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)
參數
-
屬性名稱。
-
屬性值。
-
以毫秒為單位重試斷言的時間。預設為
5000
。
返回
to_have_role
新增於: v1.44確保 Locator 指向的元素具有給定的 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.20確保 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, .*"))
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 解析為元素列表。
- 元素數量等於陣列中預期值的數量。
- 列表中的元素具有與預期陣列值匹配的文字,依序逐一匹配。
例如,考慮以下列表
<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#預期的字串或 RegExp 或其列表。
-
ignore_case
bool (選填)新增於: v1.23#是否執行不區分大小寫的匹配。ignore_case 選項優先於對應的正規表示式旗標(如果指定)。
-
以毫秒為單位重試斷言的時間。預設為
5000
。 -
use_inner_text
bool (選填)新增於: v1.18#擷取 DOM 節點文字時,是否使用
element.innerText
而非element.textContent
。
返回
詳細資訊
當 expected
參數是字串時,Playwright 會在匹配之前,將實際文字和預期字串中的空格和換行符號正規化。當使用正規表示式時,實際文字會按原樣匹配。
to_have_value
新增於: v1.20確保 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]"))
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.23確保 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")])
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")])
參數
返回
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?"
''')
參數
返回