自訂服務
您可以為 WDIO 測試執行器編寫自己的自訂服務,以客製化符合您的需求。
服務是為可重複使用的邏輯建立的附加元件,以簡化測試、管理您的測試套件並整合結果。服務可以存取在 wdio.conf.js
中可用的所有相同鉤子。
可以定義兩種服務類型:啟動器服務,它只能存取 onPrepare
、onWorkerStart
、onWorkerEnd
和 onComplete
鉤子,這些鉤子每個測試執行只執行一次;以及工作者服務,它可以存取所有其他鉤子,並且每個工作者都會執行。請注意,您無法在這兩種服務之間共享(全域)變數,因為工作者服務在不同的(工作者)程序中執行。
啟動器服務可以定義如下:
export default class CustomLauncherService {
// If a hook returns a promise, WebdriverIO will wait until that promise is resolved to continue.
async onPrepare(config, capabilities) {
// TODO: something before all workers launch
}
onComplete(exitCode, config, capabilities) {
// TODO: something after the workers shutdown
}
// custom service methods ...
}
而工作者服務應該看起來像這樣:
export default class CustomWorkerService {
/**
* `serviceOptions` contains all options specific to the service
* e.g. if defined as follows:
*
* ```
* services: [['custom', { foo: 'bar' }]]
* ```
*
* the `serviceOptions` parameter will be: `{ foo: 'bar' }`
*/
constructor (serviceOptions, capabilities, config) {
this.options = serviceOptions
}
/**
* this browser object is passed in here for the first time
*/
async before(config, capabilities, browser) {
this.browser = browser
// TODO: something before all tests are run, e.g.:
await this.browser.setWindowSize(1024, 768)
}
after(exitCode, config, capabilities) {
// TODO: something after all tests are run
}
beforeTest(test, context) {
// TODO: something before each Mocha/Jasmine test run
}
beforeScenario(test, context) {
// TODO: something before each Cucumber scenario run
}
// other hooks or custom service methods ...
}
建議透過建構子中傳入的參數來儲存瀏覽器物件。最後,將兩種工作者類型公開如下:
import CustomLauncherService from './launcher'
import CustomWorkerService from './service'
export default CustomWorkerService
export const launcher = CustomLauncherService
如果您使用的是 TypeScript,並且想要確保鉤子方法參數的類型安全,您可以將服務類別定義如下:
import type { Capabilities, Options, Services } from '@wdio/types'
export default class CustomWorkerService implements Services.ServiceInstance {
constructor (
private _options: MyServiceOptions,
private _capabilities: Capabilities.RemoteCapability,
private _config: Options.Testrunner
) {
// ...
}
// ...
}
服務錯誤處理
在服務鉤子期間拋出的錯誤將會被記錄,同時執行器會繼續執行。如果您的服務中的鉤子對於測試執行器的設定或拆解至關重要,則可以使用從 webdriverio
套件公開的 SevereServiceError
來停止執行器。
import { SevereServiceError } from 'webdriverio'
export default class CustomServiceLauncher {
async onPrepare(config, capabilities) {
// TODO: something critical for setup before all workers launch
throw new SevereServiceError('Something went wrong.')
}
// custom service methods ...
}
從模組匯入服務
為了使用這項服務,現在唯一要做的就是將其指派給 services
屬性。
修改您的 wdio.conf.js
檔案,使其看起來像這樣:
import CustomService from './service/my.custom.service'
export const config = {
// ...
services: [
/**
* use imported service class
*/
[CustomService, {
someOption: true
}],
/**
* use absolute path to service
*/
['/path/to/service.js', {
someOption: true
}]
],
// ...
}
在 NPM 上發佈服務
為了讓 WebdriverIO 社群更容易使用和發現服務,請遵循這些建議:
- 服務應使用此命名慣例:
wdio-*-service
- 使用 NPM 關鍵字:
wdio-plugin
、wdio-service
main
條目應export
服務的實例- 範例服務:
@wdio/sauce-service
遵循建議的命名模式允許按名稱新增服務
// Add wdio-custom-service
export const config = {
// ...
services: ['custom'],
// ...
}
將發佈的服務新增至 WDIO CLI 和文件
我們非常感謝每一個可以幫助其他人執行更好測試的新外掛程式!如果您已建立這樣的外掛程式,請考慮將其新增至我們的 CLI 和文件中,以便更容易找到它。
請提出包含以下變更的提取要求: