メインコンテンツへスキップ

カスタムサービス

WDIOテストランナー用に独自のカスタムサービスを作成し、ニーズに合わせてカスタマイズできます。

サービスは、テストを簡素化し、テストスイートを管理し、結果を統合するための再利用可能なロジックのために作成されたアドオンです。サービスは、wdio.conf.jsで利用可能なすべてのフックにアクセスできます。

定義できるサービスには2つのタイプがあります。1つは、テスト実行ごとに1回だけ実行されるonPrepareonWorkerStartonWorkerEnd、および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-pluginwdio-service
  • mainエントリは、サービスのインスタンスをexportする必要があります。
  • サービスの例:@wdio/sauce-service

推奨される命名パターンに従うことで、名前でサービスを追加できます。

// Add wdio-custom-service
export const config = {
// ...
services: ['custom'],
// ...
}

公開されたサービスをWDIO CLIおよびドキュメントに追加する

他の人がより良いテストを実行するのに役立つ新しいプラグインを心から歓迎します。そのようなプラグインを作成した場合は、見つけやすくするために、CLIとドキュメントに追加することを検討してください。

次の変更を含むプルリクエストを提出してください

ようこそ!何かお手伝いできることはありますか?

WebdriverIO AI Copilot