断言列表
断言 | 描述 |
---|---|
期望(定位器).to_be_checked() | 复选框已选中 |
期望(定位器).to_be_disabled() | 元素已禁用 |
期望(定位器).to_be_editable() | 元素是可编辑的 |
期望(定位器).to_be_empty() | 容器为空 |
期望(定位器).to_be_enabled() | 元素已启用 |
期望(定位器).to_be_focused() | 元素聚焦 |
期望(定位器).to_be_hidden() | 元素不可见 |
期望(定位器).to_be_visible() | 元素可见 |
期望(定位器).to_contain_text() | 元素包含文本 |
期望(定位器).to_have_attribute() | 元素具有 DOM 属性 |
期望(定位器).to_have_class() | 元素具有类属性 |
期望(定位器).to_have_count() | 列表包含确切的孩子数 |
期望(定位器).to_have_css() | 元素具有 CSS 属性 |
期望(定位器).to_have_id() | 元素具有 ID |
期望(定位器).to_have_js_property() | 元素具有 JavaScript 属性 |
期望(定位器).to_have_text() | 元素匹配文本 |
期望(定位器).to_have_value() | 输入具有值 |
期望(定位器).to_have_values() | 选择已选择的选项 |
期望(页面).to_have_title() | 页面有标题 |
期望(页面).to_have_url() | 网页包含网址 |
期望(响应).to_be_ok() | 响应具有“正常”状态 |
自定义预期消息
可以将自定义错误消息指定为函数的第二个参数,例如:expect
expect(page.get_by_text("Name"), "should be logged in").to_be_visible()
设置自定义超时
可以为全局断言或按断言指定自定义超时。默认超时为 5 秒。
全局超时
conftest.py
from playwright.sync_api import expect
expect.set_options(timeout=10_000)
每个断言超时
test_foobar.py
from playwright.sync_api import expect
def test_foobar(page: Page) -> None:
expect(page.get_by_text("Name")).to_be_visible(timeout=10_000)