跳到主要内容

实用预期条件库服务

wdio-wait-for 是一个第三方包,更多信息请参见 GitHub | npm

wdio-wait-for 是一个为 WebdriverIO 提供的 Node.js 库,它提供了一组常见条件,用于提供等待特定条件直到定义的任务完成的功能。

安装

要在项目中使用 wdio-wait-for,请运行:

npm i -D wdio-wait-for

如果你使用 Yarn,请运行:

yarn add --dev wdio-wait-for

API

示例

导入

CommonJS

如果你使用的是 WebdriverIO v7 及更低版本,并使用 CommonJS,你必须使用 require 来导入包,例如:

// import all methods
const EC = require('wdio-wait-for');

browser.waitUntil(EC.alertIsPresent(), { timeout: 5000, timeoutMsg: 'Failed, after waiting for the alert to be present' })
// import specific method
const { alertIsPresent } = require('wdio-wait-for');

browser.waitUntil(alertIsPresent(), { timeout: 5000, timeoutMsg: 'Failed, after waiting for the alert to be present' })

ESM

使用 TypeScript 或 WebdriverIO v8 及更高版本,你可以使用 import 语句导入所有辅助方法,例如:

// import all methods
import * as EC from 'wdio-wait-for';

browser.waitUntil(EC.elementToBeEnabled('input'), { timeout: 5000, timeoutMsg: 'Failed, after waiting for the element to be enabled' })

或者只导入特定的方法,例如:

// import specific method
import { elementToBeEnabled } from 'wdio-wait-for';

browser.waitUntil(elementToBeEnabled('input'), { timeout: 5000, timeoutMsg: 'Failed, after waiting for the element to be enabled' })

等待警告框

此代码片段展示了如何使用条件

browser.waitUntil(alertIsPresent(), { timeout: 5000, timeoutMsg: 'Failed, after waiting for the alert to be present' })

等待元素

此代码片段展示了如何使用条件来等待,例如等待特定数量的元素存在:

browser.waitUntil(numberOfElementsToBe('.links', 2), { timeout: 5000, timeoutMsg: 'Failed, after waiting for the 2 elements' })

许可证

MIT 许可

作者

Yevhen Laichenkov - elaichenkov@gmail.com
Christian Bromann - mail@bromann.dev

Welcome! How can I help?

WebdriverIO AI Copilot