python+playwright-29expect 常用的断言方法
playwright 提供了一个 expect方法 用于断言
expect 使用
断言 | 描述 | 描述中文 |
---|---|---|
expect(locator).to_be_checked() | Checkbox is checked | 选中复选框 |
expect(locator).to_be_disabled() | Element is disabled | 元素已禁用 |
expect(locator).to_be_editable() | Element is enabled | 元素已启用 |
expect(locator).to_be_empty() | Container is empty | 容器为空 |
expect(locator).to_be_enabled() | Element is enabled | 元素已启用 |
expect(locator).to_be_focused() | Element is focused | 元素被聚焦 |
expect(locator).to_be_hidden() | Element is not visible | 元素不可见 |
expect(locator).to_be_visible() | Element is visible | 元素可见 |
expect(locator).to_contain_text() | Element contains text | 元素包含文本 |
expect(locator).to_have_attribute() | Element has a DOM attribute | 元素具有DOM属性 |
expect(locator).to_have_class() | Element has a class property | 元素具有类属性 |
expect(locator).to_have_count() | List has exact number of children | 列表中有确切的子项数 |
expect(locator).to_have_css() | Element has CSS property | 元素具有CSS属性 |
expect(locator).to_have_id() | Element has an ID | 元素具有ID |
expect(locator).to_have_js_property() | Element has a JavaScript property | 元素具有JavaScript属性 |
expect(locator).to_have_text() | Element matches text | 元素与文本匹配 |
expect(locator).to_have_value() | Input has a value | 输入有值 |
expect(locator).to_have_values() | Select has options selected | 选择已选择选项 |
expect(page).to_have_title() | Page has a title | 页面有标题 |
expect(page).to_have_url() | Page has a URL | 页面有URL |
expect(api_response).to_be_ok() | Response has an OK status | 响应状态为OK |
to_be_checked()使用示例
from playwright.sync_api import expect
locator = page.get_by_label("Subscribe to newsletter")
expect(locator).to_be_checked()
to_be_visible()使用示例
from playwright.sync_api import expect
locator = page.locator('.my-element')
expect(locator).to_be_visible()