跳到主要内容

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#

    期望的子字符串、正则表达式或它们的列表。

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

参数

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

    期望的类名、正则表达式或它们的列表。

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

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

返回值


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#

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

  • 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 指向一个被选中的 input 元素。

用法

from playwright.sync_api import expect

locator = page.get_by_label("Subscribe to newsletter")
expect(locator).to_be_checked()

参数

  • checked bool (可选)添加于版本: v1.18#

    提供要断言的状态。默认断言 input 元素为选中状态。当 indeterminate 设置为 true 时,不能使用此选项。

  • indeterminate bool (可选)添加于版本: v1.50#

    断言元素处于 indeterminate (混合) 状态。仅支持复选框和单选按钮。当提供 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 指向一个与视口相交的元素,根据 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#

    期望的子字符串、正则表达式或它们的列表。

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

当传递数组时,此方法断言定位到的元素列表与预期的类值列表相匹配。每个元素的 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

添加于版本: 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#

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

  • 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),并且已选择指定的 values。

用法

例如,给定以下元素

<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

返回值