NightwatchでCucumberJSを使用する

概要

Nightwatch 2では、Cucumber.jsを代替テストランナーとして直接使用するための統合サポートが導入されました。 Cucumberライブラリ自体(バージョン7.3以上)以外に必要なプラグインはありません。

Nightwatchもインストールされている同じプロジェクトで、以下を実行するだけです。

npm i @cucumber/cucumber --save-dev

構成

NightwatchでCucumberJSを使用するには、test_runner構成プロパティを設定し、タイプをcucumberに設定する必要があります。 また、フィーチャーファイルが配置されているパスも設定する必要があります。

nightwatch.conf.js
{
  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-js

Cucumberスペックファイル/ステップ定義ファイルは、Nightwatch設定のsrc_folders、またはCLI引数として指定できます。

src_foldersが定義されている場合
npx nightwatch
src_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 --headless

WebDriverセッションの手動開始

Nightwatchがインスタンス化された後、WebDriverセッションを自動的に開始しない必要がある場合があります。 この目的のために、Nightwatchはthis.clientとして利用可能なインスタンスを提供します。これにはlaunchBrowser()メソッドが含まれています。

構成

nightwatch.conf.js
{
  test_runner: {
    type: 'cucumber',
    options: {
      feature_path: 'examples/cucumber-js/*/*.feature',
      auto_start_session: false
    }
  }
}

次に、Nightwatchに--requireとして渡すことができる追加のセットアップファイルを使用できます。これはCucumberに転送されます。 追加のセットアップファイルでは、セッションが開始される前に実行する必要があるその他の操作を追加できます。

例 _extra_setup.js

Nightwatchによって自動的に閉じられるように、thisbrowserを設定することを忘れないでください。 それ以外の場合は、独自のCucumber After()フックで.quit()を呼び出すことを忘れないでください。

_extra_setup.js
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レポーター(JUnit XMLレポートやグローバルカスタムレポーターなど)は利用できません。 主な理由は、レポートがCucumber CLIに委任されるためです。 独自の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
ℹ Connected to GeckoDriver on port 4444 (1740ms).
Using: firefox (92.0.1) on MAC (20.6.0).

.. ✔ Testing if the page title equals 'Rijksmuseum Amsterdam, home of the Dutch masters' (4ms) . ✔ Element <#rijksmuseum-app> was visible after 46 milliseconds. . ✔ Testing if element <.search-results> contains text 'Operation Night Watch' (1994ms) ... ✔ Testing if the page title equals 'Rijksmuseum Amsterdam, home of the Dutch masters' (8ms) . ✔ Element <#rijksmuseum-app> was visible after 49 milliseconds. . ✔ Testing if element <.search-results> contains text 'The Night Watch, Rembrandt van Rijn, 1642' (1427ms) .

2 scenarios (2 passed) 10 steps (10 passed) 0m13.024s (executing steps: 0m12.998s)