自动等待
介绍
Playwright 在对元素执行操作之前,会进行一系列可操作性检查,以确保这些操作按预期执行。 它会自动等待所有相关的检查通过,然后才执行请求的操作。 如果在给定的 timeout
内,所需的检查未通过,则操作将失败并抛出 TimeoutError
。
例如,对于 Locator.click(),Playwright 将确保
以下是为每个操作执行的完整可操作性检查列表
操作 | 可见的 | 稳定的 | 接收事件 | 启用的 | 可编辑的 |
---|---|---|---|---|---|
Locator.check() | 是 | 是 | 是 | 是 | - |
Locator.click() | 是 | 是 | 是 | 是 | - |
Locator.dblclick() | 是 | 是 | 是 | 是 | - |
Locator.setChecked() | 是 | 是 | 是 | 是 | - |
Locator.tap() | 是 | 是 | 是 | 是 | - |
Locator.uncheck() | 是 | 是 | 是 | 是 | - |
Locator.hover() | 是 | 是 | 是 | - | - |
Locator.dragTo() | 是 | 是 | 是 | - | - |
Locator.screenshot() | 是 | 是 | - | - | - |
Locator.fill() | 是 | - | - | 是 | 是 |
Locator.clear() | 是 | - | - | 是 | 是 |
Locator.selectOption() | 是 | - | - | 是 | - |
Locator.selectText() | 是 | - | - | - | - |
Locator.scrollIntoViewIfNeeded() | - | 是 | - | - | - |
Locator.blur() | - | - | - | - | - |
Locator.dispatchEvent() | - | - | - | - | - |
Locator.focus() | - | - | - | - | - |
Locator.press() | - | - | - | - | - |
Locator.pressSequentially() | - | - | - | - | - |
Locator.setInputFiles() | - | - | - | - | - |
强制操作
某些操作(如 Locator.click())支持 force
选项,该选项禁用非必要的可操作性检查。例如,向 Locator.click() 方法传递 truthy force
将不会检查目标元素是否实际接收点击事件。
断言
Playwright 包含自动重试断言,它通过等待条件满足来消除不稳定性,类似于操作前的自动等待。
断言 | 描述 |
---|---|
assertThat(locator).isAttached() | 元素已附加 |
assertThat(locator).isChecked() | 复选框已选中 |
assertThat(locator).isDisabled() | 元素已禁用 |
assertThat(locator).isEditable() | 元素可编辑 |
assertThat(locator).isEmpty() | 容器为空 |
assertThat(locator).isEnabled() | 元素已启用 |
assertThat(locator).isFocused() | 元素已聚焦 |
assertThat(locator).isHidden() | 元素不可见 |
assertThat(locator).isInViewport() | 元素与视口相交 |
assertThat(locator).isVisible() | 元素可见 |
assertThat(locator).containsText() | 元素包含文本 |
assertThat(locator).hasAttribute() | 元素具有 DOM 属性 |
assertThat(locator).hasClass() | 元素具有 class 属性 |
assertThat(locator).hasCount() | 列表具有确切数量的子元素 |
assertThat(locator).hasCSS() | 元素具有 CSS 属性 |
assertThat(locator).hasId() | 元素具有 ID |
assertThat(locator).hasJSProperty() | 元素具有 JavaScript 属性 |
assertThat(locator).hasText() | 元素匹配文本 |
assertThat(locator).hasValue() | 输入框具有值 |
assertThat(locator).hasValues() | 选择框已选择选项 |
assertThat(page).hasTitle() | 页面具有标题 |
assertThat(page).hasURL() | 页面具有 URL |
assertThat(response).isOK() | 响应具有 OK 状态 |
在 断言指南 中了解更多。
可见的
当元素具有非空的边界框并且没有 visibility:hidden
计算样式时,该元素被认为是可见的。
请注意,根据此定义
- 零尺寸的元素 不被 认为是可见的。
- 具有
display:none
的元素 不被 认为是可见的。 - 具有
opacity:0
的元素 被 认为是可见的。
稳定的
当元素在至少两个连续的动画帧中保持相同的边界框时,该元素被认为是稳定的。
启用的
当元素 未被禁用 时,该元素被认为是启用的。
当元素 禁用 时
- 它是一个带有
[disabled]
属性的<button>
、<select>
、<input>
、<textarea>
、<option>
或<optgroup>
; - 它是一个
<button>
、<select>
、<input>
、<textarea>
、<option>
或<optgroup>
,并且是带有[disabled]
属性的<fieldset>
的一部分; - 它是具有
[aria-disabled=true]
属性的元素的后代。
可编辑的
当元素 已启用 且 未只读 时,该元素被认为是可编辑的。
当元素 只读 时
- 它是一个带有
[readonly]
属性的<select>
、<input>
或<textarea>
; - 它具有
[aria-readonly=true]
属性和一个支持它的 aria 角色。
接收事件
当元素是操作点处指针事件的命中目标时,该元素被认为正在接收指针事件。 例如,当点击点 (10;10) 时,Playwright 会检查是否其他元素(通常是覆盖层)将改为捕获 (10;10) 的点击。
例如,考虑这样一种情况,无论何时调用 Locator.click(),Playwright 都会点击“注册”按钮
- 页面正在检查用户名是否唯一,并且“注册”按钮已禁用;
- 在与服务器检查后,禁用的“注册”按钮被替换为另一个现在已启用的按钮。