跳至主要内容

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

Added in: 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

返回


无需检查

Added in: 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

Added in: 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

返回


不可编辑

Added in: 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

返回


不为空

Added in: 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

Added in: 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

返回


不被聚焦

Added in: 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

返回


不隐藏

Added in: 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

返回


不在视口范围内

Added in: 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

Added in: 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

Added in: 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#

    预期的子字符串或正则表达式或其列表。

  • 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

Added in: 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_error_message

Added in: v1.50 locatorAssertions.not_to_have_accessible_error_message

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

参数

  • error_message str | Pattern#

    预期的可访问错误信息。

  • ignore_case bool (可选)#

    是否执行不区分大小写的匹配。如果指定了ignore_case选项,它将优先于相应的正则表达式标志。

  • timeout float (可选)#

    重试断言的时间,以毫秒为单位。默认为 5000

返回


not_to_have_accessible_name

Added in: 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

Added in: 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

Added in: 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)

参数

  • expected str | Pattern | List[str] | List[Pattern] | List[str | Pattern] 添加于: v1.18#

    预期的类或正则表达式或它们的列表。

  • timeout float (可选) 添加于: v1.18#

    重试断言的时间,以毫秒为单位。默认为 5000

返回


not_to_have_count

Added in: 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

Added in: 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

Added in: 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

Added in: 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

Added in: 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

Added in: 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#

    预期的字符串或正则表达式或它们的列表。

  • 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

Added in: 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

Added in: 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)

参数

返回


不匹配ARIA快照

Added in: 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

返回


待附加

Added in: v1.33 locatorAssertions.to_be_attached

确保Locator指向一个connected到Document或ShadowRoot的元素。

用法

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

参数

  • attached bool (可选)#

  • timeout float (可选)#

    重试断言的时间,以毫秒为单位。默认为 5000

返回


待检查

Added in: 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

Added in: v1.20 locatorAssertions.to_be_disabled

Ensures the Locator points to a disabled element. Element is disabled if it has "disabled" attribute or is disabled via 'aria-disabled'. Note that only native control elements such as HTML button, input, select, textarea, option, optgroup can be disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored by the browser.

用法

from playwright.sync_api import expect

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

参数

  • timeout float (可选) 添加于: v1.18#

    重试断言的时间,以毫秒为单位。默认为 5000

返回


to_be_editable

Added in: 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

Added in: 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

返回


待启用

Added in: 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

Added in: 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

返回


隐藏

Added in: 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

Added in: v1.31 locatorAssertions.to_be_in_viewport

确保Locator指向的元素与视口相交,依据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

Added in: 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

Added in: 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. 定位器解析为一组元素。
  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#

    预期的子字符串或正则表达式或其列表。

  • 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

Added in: 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

Added in: v1.50 locatorAssertions.to_have_accessible_error_message

确保Locator指向具有指定aria errormessage的元素。

用法

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

Added in: v1.44 locatorAssertions.to_have_accessible_name

确保Locator指向具有给定accessible name的元素。

用法

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

Added in: 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

Added in: 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")

当传入一个数组时,该方法会断言定位到的元素列表与预期的类值列表相匹配。每个元素的class属性将与数组中对应的字符串或正则表达式进行匹配:

from playwright.sync_api import expect

locator = page.locator("list > .component")
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

Added in: 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

Added in: 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

Added in: 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

Added in: 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

Added in: 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

Added in: 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. 定位器解析为一组元素。
  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#

    预期的字符串或正则表达式或它们的列表。

  • 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

Added in: 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

Added in: 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

Added in: 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

返回