模組
WebdriverIO 發布了多個 NPM 和其他註冊表中的模組,您可以使用這些模組來構建自己的自動化框架。 請參閱這裡有關 WebdriverIO 設定類型的更多文件。
webdriver
和 devtools
協定套件(webdriver
和 devtools
)公開了一個類別,該類別附加了以下靜態函數,可讓您啟動會話
newSession(options, modifier, userPrototype, customCommandWrapper)
使用特定功能啟動新的會話。 根據會話回應,將會提供來自不同協定的命令。
參數
options
: WebDriver 選項modifier
:允許在返回客戶端實例之前修改它的函數userPrototype
:允許擴展實例原型屬性物件customCommandWrapper
:允許包裝函數呼叫周圍的功能的函數
返回
- 瀏覽器物件
範例
const client = await WebDriver.newSession({
capabilities: { browserName: 'chrome' }
})
attachToSession(attachInstance, modifier, userPrototype, customCommandWrapper)
附加到正在執行的 WebDriver 或 DevTools 會話。
參數
attachInstance
:要將會話附加到的實例,或至少具有sessionId
屬性的物件(例如{ sessionId: 'xxx' }
)modifier
:允許在返回客戶端實例之前修改它的函數userPrototype
:允許擴展實例原型屬性物件customCommandWrapper
:允許包裝函數呼叫周圍的功能的函數
返回
- 瀏覽器物件
範例
const client = await WebDriver.newSession({...})
const clonedClient = await WebDriver.attachToSession(client)
reloadSession(instance)
重新載入給定提供的實例的會話。
參數
instance
:要重新載入的套件實例
範例
const client = await WebDriver.newSession({...})
await WebDriver.reloadSession(client)
webdriverio
與協定套件(webdriver
和 devtools
)類似,您也可以使用 WebdriverIO 套件 API 來管理會話。 API 可以使用 import { remote, attach, multiremote } from 'webdriverio
導入,並包含以下功能
remote(options, modifier)
啟動 WebdriverIO 會話。 實例包含所有命令,如同協定套件,但具有其他更高階的函數,請參閱API 文件。
參數
options
: WebdriverIO 選項modifier
:允許在返回客戶端實例之前修改它的函數
返回
- 瀏覽器物件
範例
import { remote } from 'webdriverio'
const browser = await remote({
capabilities: { browserName: 'chrome' }
})
attach(attachOptions)
附加到正在執行的 WebdriverIO 會話。
參數
attachOptions
:要將會話附加到的實例,或至少具有sessionId
屬性的物件(例如{ sessionId: 'xxx' }
)
返回
- 瀏覽器物件
範例
import { remote, attach } from 'webdriverio'
const browser = await remote({...})
const newBrowser = await attach(browser)
multiremote(multiremoteOptions)
啟動多遠端實例,讓您可以在單個實例中控制多個會話。 請查看我們的多遠端範例,瞭解具體的用例。
參數
multiremoteOptions
:一個物件,其鍵表示瀏覽器名稱及其WebdriverIO 選項。
返回
- 瀏覽器物件
範例
import { multiremote } from 'webdriverio'
const matrix = await multiremote({
myChromeBrowser: {
capabilities: { browserName: 'chrome' }
},
myFirefoxBrowser: {
capabilities: { browserName: 'firefox' }
}
})
await matrix.url('http://json.org')
await matrix.getInstance('browserA').url('https://google.com')
console.log(await matrix.getTitle())
// returns ['Google', 'JSON']
@wdio/cli
您也可以將測試執行器作為模組包含,並在任意環境中執行它,而不是呼叫 wdio
命令。 為此,您需要像這樣將 @wdio/cli
套件作為模組要求
- EcmaScript 模組
- CommonJS
import Launcher from '@wdio/cli'
const Launcher = require('@wdio/cli').default
之後,建立啟動器的實例,並執行測試。
Launcher(configPath, opts)
Launcher
類別建構子預期配置文件的 URL 和具有將覆蓋配置中設定的設定的 opts
物件。
參數
configPath
:要執行的wdio.conf.js
的路徑opts
:參數(<RunCommandArguments>
)以覆蓋配置檔案中的值
範例
const wdio = new Launcher(
'/path/to/my/wdio.conf.js',
{ spec: '/path/to/a/single/spec.e2e.js' }
)
wdio.run().then((exitCode) => {
process.exit(exitCode)
}, (error) => {
console.error('Launcher failed to start the test', error.stacktrace)
process.exit(1)
})
run
命令會傳回 Promise。 如果測試成功執行或失敗,則會解析該 Promise,如果啟動器無法啟動執行測試,則會拒絕該 Promise。
@wdio/browser-runner
當使用 WebdriverIO 的瀏覽器執行器執行單元或元件測試時,您可以匯入測試的模擬公用程式,例如
import { fn, spyOn, mock, unmock } from '@wdio/browser-runner'
下列具名匯出可用
fn
模擬函數,請參閱官方 Vitest 文件中的更多資訊。
spyOn
間諜函數,請參閱官方 Vitest 文件中的更多資訊。
mock
模擬檔案或相依性模組的方法。
參數
moduleName
:要模擬的檔案的相對路徑或模組名稱。factory
:傳回模擬值的函數(選用)
範例
mock('../src/constants.ts', () => ({
SOME_DEFAULT: 'mocked out'
}))
mock('lodash', (origModuleFactory) => {
const origModule = await origModuleFactory()
return {
...origModule,
pick: fn()
}
})
unmock
取消模擬(unmock)在手動模擬(__mocks__
)目錄中定義的依賴。
參數
moduleName
:要取消模擬的模組名稱。
範例
unmock('lodash')