跳到主要內容

實用的預期條件程式庫服務

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

歡迎!我能幫您什麼嗎?

WebdriverIO AI Copilot