CucumberJS JSON レポーター
wdio-cucumberjs-json-reporter はサードパーティパッケージです。詳細については、GitHub | npm を参照してください。
WebdriverIO v8 以降用の CucumberJS JSON ファイルを作成する WDIO レポーター。
何をするか
このレポーターは、テスト対象の各機能に対して**Cucumber JSON ファイル**を生成します。JSON ファイルは、たとえば multiple-cucumber-html-reporter のような、使用したいレポートで使用できます。
また、実行中のインスタンスに関するメタデータをフィーチャーファイルに追加し、最後に、JSON 出力に添付ファイルを追加する機会も提供します。
インストール
最も簡単な方法は、`wdio-cucumberjs-json-reporter` を `package.json` の devDependency として保持することです。
{
"devDependencies": {
"wdio-cucumberjs-json-reporter": "^5.0.0"
}
}
次のコマンドで簡単に行えます
npm install wdio-cucumberjs-json-reporter --save-dev
これにより、自動的に `package.json` に追加されます
`WebdriverIO` のインストール方法については、こちらを参照してください。
設定
wdio.conf.js ファイルで出力ディレクトリと言語を設定します
export const config = {
// ...
reporters: [
// Like this with the default options, see the options below
'cucumberjs-json',
// OR like this if you want to set the folder and the language
[ 'cucumberjs-json', {
jsonFolder: '.tmp/new/',
language: 'en',
},
],
],
// ...
}
レポーターを追加する両方の方法を使用しないでください。これは単なる例です!
オプション
jsonFolder
- タイプ:
String
- 必須: いいえ
- デフォルト:
.tmp/json/
このレポートによって生成された JSON ファイルが保存されるディレクトリで、スクリプトが開始された場所からの相対パスです。
注:`npm run test` のようにコマンドラインから npm スクリプトを使用する場合、`jsonFolder` はスクリプトが実行されるパスからの相対パスになります。プロジェクトのルートから実行すると、プロジェクトのルートにも `jsonFolder` が作成されます。
language
- タイプ:
String
- 必須: いいえ
- デフォルト:
en
Gherkin シナリオが記述されている言語(デフォルトは英語)。言語コードとそのキーワードのリストは、こちらにあります。
disableHooks
- タイプ:
boolean
- 必須: いいえ
- デフォルト:
false
このプロパティが true
に設定されている場合、フックの詳細は生成に含まれません。
reportFilePerRetry
- タイプ:
boolean
- 必須: いいえ
- デフォルト:
true
このプロパティが false
に設定されている場合、スペックが再試行されると、レポートは前の試行からの既存のレポートファイルに追加されます。
例: ['cucumberjs-json', { jsonFolder: '.tmp/new/', language: 'en', disableHooks:true}]
メタデータ
注意
これは現在、WebdriverIO V6 を使用している場合はサポートされていません。WebdriverIO V5 はこれをサポートし、WebdriverIO V7 は再びサポートします
前述のように、このレポートは、機能が実行された現在のマシン/デバイスのメタデータを自動的に保存できます。
これをカスタマイズするには、次のオブジェクトを `capabilities` に追加して追加できます
// Example wdio.conf.js
export const config = {
//..
capabilities: [
{
browserName: 'chrome',
// Add this
'cjson:metadata': {
// For a browser
browser: {
name: 'chrome',
version: '58',
},
// for an app
app: {
name: 'name.of.app.ipa',
version: '1.2.3',
},
device: 'MacBook Pro 15',
platform: {
name: 'OSX',
version: '10.12.6'
}
},
},
],
};
メタデータオブジェクトには `cjson` プレフィックスが必要です。そうでないと機能しません!
メタデータ値
metadata.app.name
- タイプ:
string
例: アプリの名前。
metadata.app.version
- タイプ:
string
例: アプリのバージョン。
metadata.browser.name
- タイプ:
string
- 使用可能な値:
internet explorer | edge | chrome | firefox | safari
metadata.browser.version
- タイプ:
string
例: ブラウザのバージョン。これは手動で追加するか、テストの実行中に取得して正確なバージョン番号を取得できます。
metadata.device
- タイプ:
string
例: デバイスのタイプを表す名前。たとえば、仮想マシンで実行する場合は、ここに Virtual Machine
を配置したり、iPhone 7 Plus
のようなモバイルの名前を配置したりできます。
metadata.platform.name
- タイプ:
string
- 使用可能な値:
windows | osx | linux | ubuntu | android | ios
metadata.platform.version
- タイプ:
string
例: プラットフォームのバージョン
メタデータに `browser` オブジェクトを提供しない場合、このモジュールは自動的に決定します。常に、決定できる最新の値で上書きされます。
`device` または `platform` オブジェクトを提供しない場合、デフォルトで `not known` に設定されます
`browser.name` または `browser.version` を提供しない場合、モジュールは自動的にこれを判断しようとします。
添付ファイル
次のすべてのフック/ステップで JSON ファイルにデータを添付するオプションがあります
- Before(All)
- After(All)
- Given
- When
- Then
- And
ステップファイルで提供する必要があるコードは次のとおりです。
ESモジュール(ESM)の場合
import cucumberJson from 'wdio-cucumberjs-json-reporter';
// Attach a string (if no type is provided it will automatically default to `text/plain`
cucumberJson.attach('just a string');
cucumberJson.attach('just a second string', 'text/plain');
// Attach JSON
cucumberJson.attach({"json-string": true}, 'application/json');
// Attach a screenshot in a before hook
cucumberJson.attach(await browser.takeScreenshot(), 'image/png');
CommonJS(CJS)の場合
const { attach } = require("wdio-cucumberjs-json-reporter");
// Attach a string (if no type is provided it will automatically default to `text/plain`
attach('just a string');
attach('just a second string', 'text/plain');
// Attach JSON
attach({"json-string": true}, 'application/json');
// Attach a screenshot in a before hook
attach(await browser.takeScreenshot(), 'image/png');
multiple-cucumber-html-reporter で使用する
WebdriverIO V4 用の以前のモジュール、wdio-multiple-cucumber-html-reporter は、multiple-cucumber-html-reporter モジュールとの連携機能が組み込まれていました。このレポーターではそうではありません。これは、WebdriverIO V5 の新しいセットアップが、onPrepare
および onComplete
フックの使用を許可しないインスタンスに基づいているためです。
それでも multiple-cucumber-html-reporter モジュールを使用したい場合は、構成ファイルに以下を追加できます。
-
モジュールをインストールするには
npm install multiple-cucumber-html-reporter --save-dev
-
これを構成ファイルに追加してください
import fs from 'node:fs/promises'
// Import the module
import { generate } from 'multiple-cucumber-html-reporter'
// Example wdio.conf.js
export const config = {
//..
// =====
// Hooks
// =====
/**
* Gets executed once before all workers get launched.
*/
onPrepare: () => {
// Remove the `.tmp/` folder that holds the json and report files
return fs.rm('.tmp/', { recursive: true });
},
/**
* Gets executed after all workers got shut down and the process is about to exit.
*/
onComplete: () => {
// Generate the report when it all tests are done
generate({
// Required
// This part needs to be the same path where you store the JSON files
// default = '.tmp/json/'
jsonDir: '.tmp/json/',
reportPath: '.tmp/report/',
// for more options see https://github.com/wswebcreation/multiple-cucumber-html-reporter#options
});
}
}
古い WebdriverIO バージョン
このモジュールは WebdriverIO V8+ のみで動作します!
V6 については、こちらのドキュメントを確認し、バージョン 2.0.4 を使用してください。
V5 については、こちらのドキュメントを確認し、バージョン 1.3.0 を使用してください。
このモジュールは、wdio-multiple-cucumber-html-reporter の代替ではありません。そのモジュールは WEBDRIVERIO V4 のみをサポートしており、レポートも作成します。このモジュールは JSON のみを作成し、レポートは作成しません!