跳至主要內容

測試執行器

WebdriverIO 配備了自己的測試執行器,以協助您儘快開始測試。它應該為您完成所有工作,允許整合到第三方服務,並協助您盡可能有效地執行測試。

WebdriverIO 的測試執行器以獨立的 NPM 套件 @wdio/cli 綑綁在一起。

像這樣安裝它

npm install @wdio/cli

若要查看命令列介面說明,請在您的終端機中輸入以下命令

$ npx wdio --help

wdio <command>

Commands:
wdio config Initialize WebdriverIO and setup configuration in
your current project.
wdio install <type> <name> Add a `reporter`, `service`, or `framework` to
your WebdriverIO project
wdio repl <option> [capabilities] Run WebDriver session in command line
wdio run <configPath> Run your WDIO configuration file to initialize
your tests.

Options:
--version Show version number [boolean]
--help Show help [boolean]

太棒了!現在您需要定義一個組態檔,其中設定所有關於您的測試、功能和設定的資訊。切換到組態檔區段以查看該檔案的外觀。

有了 wdio 組態輔助程式,生成您的組態檔非常容易。只需執行

$ npx wdio config

...它就會啟動輔助工具。

它會詢問您問題並在不到一分鐘的時間內為您產生組態檔。

WDIO configuration utility

一旦您設定了組態檔,就可以執行以下命令開始您的測試

npx wdio run wdio.conf.js

您也可以在沒有 run 命令的情況下初始化您的測試執行

npx wdio wdio.conf.js

就是這樣!現在,您可以透過全域變數 browser 存取 selenium 實例。

命令

wdio config

config 命令會執行 WebdriverIO 組態輔助程式。此輔助程式會詢問您一些關於您的 WebdriverIO 專案的問題,並根據您的答案建立 wdio.conf.js 檔案。

範例

wdio config

選項

--help            prints WebdriverIO help menu                                [boolean]
--npm Wether to install the packages using NPM instead of yarn [boolean]

wdio run

這是執行組態的預設命令。

run 命令會初始化您的 WebdriverIO 組態檔並執行您的測試。

範例

wdio run ./wdio.conf.js --watch

選項

--help                prints WebdriverIO help menu                   [boolean]
--version prints WebdriverIO version [boolean]
--hostname, -h automation driver host address [string]
--port, -p automation driver port [number]
--user, -u username if using a cloud service as automation backend
[string]
--key, -k corresponding access key to the user [string]
--watch watch specs for changes [boolean]
--logLevel, -l level of logging verbosity
[choices: "trace", "debug", "info", "warn", "error", "silent"]
--bail stop test runner after specific amount of tests have
failed [number]
--baseUrl shorten url command calls by setting a base url [string]
--waitforTimeout, -w timeout for all waitForXXX commands [number]
--framework, -f defines the framework (Mocha, Jasmine or Cucumber) to
run the specs [string]
--reporters, -r reporters to print out the results on stdout [array]
--suite overwrites the specs attribute and runs the defined
suite [array]
--spec run only a certain spec file - overrides specs piped
from stdin [array]
--exclude exclude spec file(s) from a run - overrides specs piped
from stdin [array]
--repeat Repeat specific specs and/or suites N times [number]
--mochaOpts Mocha options
--jasmineOpts Jasmine options
--cucumberOpts Cucumber options

注意:可以使用 tsx ENV Vars 輕鬆控制自動編譯。另請參閱TypeScript 文件

wdio install

install 命令允許您透過 CLI 將報告器和服務新增至您的 WebdriverIO 專案。

範例

wdio install service sauce # installs @wdio/sauce-service
wdio install reporter dot # installs @wdio/dot-reporter
wdio install framework mocha # installs @wdio/mocha-framework

如果您想要改用 yarn 安裝套件,您可以將 --yarn 標記傳遞給命令

wdio install service sauce --yarn

如果您的 WDIO 組態檔不在您正在工作的相同資料夾中,您也可以傳遞自訂組態路徑

wdio install service sauce --config="./path/to/wdio.conf.js"

支援服務清單

sauce
testingbot
firefox-profile
devtools
browserstack
appium
intercept
zafira-listener
reportportal
docker
wiremock
lambdatest
vite
nuxt

支援報告器清單

dot
spec
junit
allure
sumologic
concise
reportportal
video
html
json
mochawesome
timeline

支援框架清單

mocha
jasmine
cucumber

wdio repl

repl 命令允許啟動互動式命令列介面來執行 WebdriverIO 命令。它可以被用於測試目的,或僅是快速啟動 WebdriverIO 工作階段。

在本地 Chrome 中執行測試

wdio repl chrome

或在 Sauce Labs 上執行測試

wdio repl chrome -u $SAUCE_USERNAME -k $SAUCE_ACCESS_KEY

您可以套用與run 命令中相同的引數。

歡迎!我能如何幫您?

WebdriverIO AI Copilot