NightwatchでCucumberJSを使用する
概要
Nightwatch 2では、Cucumber.jsを代替テストランナーとして直接使用するための統合サポートが導入されました。 Cucumberライブラリ自体(バージョン7.3以上)以外に必要なプラグインはありません。
Nightwatchもインストールされている同じプロジェクトで、以下を実行するだけです。
npm i @cucumber/cucumber --save-dev構成
NightwatchでCucumberJSを使用するには、test_runner構成プロパティを設定し、タイプをcucumberに設定する必要があります。 また、フィーチャーファイルが配置されているパスも設定する必要があります。
{
test_runner: {
// set cucumber as the runner
type: 'cucumber',
// define cucumber specific options
options: {
//set the feature path
feature_path: 'examples/cucumber-js/*/*.feature',
// start the webdriver session automatically (enabled by default)
auto_start_session: true,
// use parallel execution in Cucumber
// set number of workers to use (can also be defined in the cli as --parallel 2
parallel: 2
}
},
src_folders: ['examples/cucumber-js/features/step_definitions']
}テストの実行
例からCucumberテストを実行する最も簡単な方法は
npx nightwatch --env cucumber-jsCucumberスペックファイル/ステップ定義ファイルは、Nightwatch設定のsrc_folders、またはCLI引数として指定できます。
src_foldersが定義されている場合
npx nightwatchsrc_foldersが定義されていない場合
npx nightwatch examples/cucumber-js/features/step_definition並列実行
2つのワーカーを使用した並列実行
nightwatch examples/cucumber-js/features/step_definitions --parallel 2通常どおりに他のテストランナーオプションを使用します
npx nightwatch examples/cucumber-js/features/step_definitions --headlessWebDriverセッションの手動開始
Nightwatchがインスタンス化された後、WebDriverセッションを自動的に開始しない必要がある場合があります。 この目的のために、Nightwatchはthis.clientとして利用可能なインスタンスを提供します。これにはlaunchBrowser()メソッドが含まれています。
構成
{
test_runner: {
type: 'cucumber',
options: {
feature_path: 'examples/cucumber-js/*/*.feature',
auto_start_session: false
}
}
}次に、Nightwatchに--requireとして渡すことができる追加のセットアップファイルを使用できます。これはCucumberに転送されます。 追加のセットアップファイルでは、セッションが開始される前に実行する必要があるその他の操作を追加できます。
例 _extra_setup.js
Nightwatchによって自動的に閉じられるように、thisでbrowserを設定することを忘れないでください。 それ以外の場合は、独自のCucumber After()フックで.quit()を呼び出すことを忘れないでください。
const {Before} = require('@cucumber/cucumber');
Before(async function(testCase) {
if (!this.client) {
console.error('Nightwatch instance was not created.');
return;
}
this.client.updateCapabilities({
testCap: 'testing'
});
this.browser = await this.client.launchBrowser();
});追加のセットアップで実行
nightwatch examples/cucumber-js/features/step_definitions --require {/full/path/to/_extra_setup.js}Cucumber用のNightwatchセットアップファイル
また、NightwatchがCucumberランナーを初期化するために使用する組み込みのセットアップファイルを調べることができます。 これは、プロジェクトのルートフォルダの/cucumber-js/_setup_cucumber_runner.jsにあります。
レポート
統合されたCucumberテストランナーを使用する場合、出力を生成するためにCucumber フォーマッターを使用する必要があります。
Nightwatchは、--formatおよび--format-optionsCLI引数が存在する場合、Cucumberに転送します。
デフォルトでは、progressフォーマッターが使用されます。
たとえば
npx nightwatch --env cucumber-js --format @cucumber/pretty-formatterまたは
npx nightwatch --env cucumber-js --require cucumber.conf.js --format json:report/cucumber_report.json出力例
Firefoxでサンプルテストを実行すると、出力は次のようになります。 Nightwatchがインストールされているプロジェクトでこれを実行するだけです
npx nightwatch examples/cucumber-js/features/step_definition