Report Portal レポーター
wdio-reportportal-reporter はサードパーティ製のパッケージです。詳細については、GitHub | npm を参照してください。
WebdriverIO レポータープラグインで、結果を Report Portal(http://reportportal.io/) に報告します。
インストール
最も簡単な方法は、package.json に wdio-reportportal-reporter と wdio-reportportal-service を devDependency として保持することです。
{
  "devDependencies": {
    "wdio-reportportal-reporter": "^7.0.0",
    "wdio-reportportal-service": "^7.0.0"
  }
}
WebdriverIO のインストール方法については、こちら を参照してください。
設定
wdio.conf.js ファイルで出力ディレクトリを設定します
const reportportal = require('wdio-reportportal-reporter');
const RpService = require("wdio-reportportal-service");
const conf = {
  reportPortalClientConfig: { // report portal settings
    token: '00000000-0000-0000-0000-00000000000',
    endpoint: 'https://reportportal-url/api/v1',
    launch: 'launch_name',
    project: 'project_name',
    mode: 'DEFAULT',
    debug: false,
    description: "Launch description text",
    attributes: [{key:"tag", value: "foo"}],
    headers: {"foo": "bar"}, // optional headers for internal http client
    restClientConfig: { // axios like http client config - https://github.com/axios/axios#request-config
      proxy: {
        protocol: 'https',
        host: '127.0.0.1',
        port: 9000,
        auth: {
          username: 'mikeymike',
          password: 'rapunz3l'
        }
      },
      timeout: 60000
    }
  },
  reportSeleniumCommands: false, // add selenium commands to log
  seleniumCommandsLogLevel: 'debug', // log level for selenium commands
  autoAttachScreenshots: false, // automatically add screenshots
  screenshotsLogLevel: 'info', // log level for screenshots
  parseTagsFromTestTitle: false, // parse strings like `@foo` from titles and add to Report Portal
  cucumberNestedSteps: false, // report cucumber steps as Report Portal steps
  autoAttachCucumberFeatureToScenario: false, // requires cucumberNestedSteps to be true for use
  sanitizeErrorMessages: true, // strip color ascii characters from error stacktrace
  sauceLabOptions : {
    enabled: true, // automatically add SauseLab ID to rp tags.
    sldc: "US" // automatically add SauseLab region to rp tags.
  }
};
exports.config = {
  // ...
  services: [[RpService, {}]],
  reporters: [[reportportal, conf]],
  // ...
};
追加 API
API メソッドは以下を使用してアクセスできます
const reporter = require('wdio-reportportal-reporter')
メソッドの説明
- reporter.addAttribute({key, value})– 現在のテストに属性を追加します。- key(文字列、オプション) - 属性キー。空でない文字列である必要があります。
- value(文字列、必須) – 属性値。空でない文字列である必要があります。
 
- reporter.addAttributeToCurrentSuite({key, value})- 現在のスイートに属性を追加します。- key(文字列、オプション) - 属性キー。空でない文字列である必要があります。
- value(文字列、必須) – 属性値。空でない文字列である必要があります。
 
- reporter.addDescriptionToCurrentSuite(description)- 現在のスイートに文字列を追加します。- description(文字列) - 説明の内容。テキストはマークダウンでフォーマットできます。
 
- reporter.addDescriptionToAllSuites(description)- 今後のすべてのスイートに文字列を追加します。(すべてのフックの前に使用すると、すべてのスイートに同じ説明が追加されます)- description(文字列) - 説明の内容。テキストはマークダウンでフォーマットできます。
 
- reporter.sendLog(level, message)– 現在のスイート\テストアイテムにログを送信します。- level(文字列) - ログレベル。値は ['trace', 'debug', 'info', 'warn', 'error'] です。
- message(文字列) – ログメッセージの内容。
 
- reporter.sendFile(level, name, content, [type])– 現在のスイート\テストアイテムにファイルを送信します。- level(文字列) - ログレベル。値は ['trace', 'debug', 'info', 'warn', 'error'] です。
- name(文字列) – ファイル名。
- content(文字列) – 添付ファイルの内容
- type(文字列、オプション) – 添付ファイルの MIME タイプ、デフォルトは- image/png
- message(文字列) – ログメッセージの内容。
 
- reporter.sendLogToTest(test, level, message)- 特定のテストにログを送信します。- test(オブジェクト) -- afterTest\afterStepwdio フックからのテストオブジェクト
- level(文字列) - ログレベル。値は ['trace', 'debug', 'info', 'warn', 'error'] です。
- message(文字列) – ログメッセージの内容。
 
- reporter.sendFileToTest(test, level, name, content, [type])– 特定のテストにファイルを送信します。- test(オブジェクト) -- afterTest\afterStepwdio フックからのテストオブジェクト
- level(文字列) - ログレベル。値は ['trace', 'debug', 'info', 'warn', 'error'] です。
- name(文字列) – ファイル名。
- content(文字列) – 添付ファイルの内容
- type(文字列、オプション) – 添付ファイルの MIME タイプ、デフォルトは- image/png
- message(文字列) – ログメッセージの内容。
 
注意: sendLog\sendFile は **現在実行中のテストアイテム** にログを送信します。つまり、アクティブなテストがない場合 (フックやスイートレベルなど) にログを送信すると、Report Portal UI に報告されません。
メソッド sendLogToTest\sendFileToTest は、wdio afterTest フックから失敗したテストアイテムにスクリーンショットやログを送信する必要がある場合に役立ちます。
Mocha の例
const reportportal = require('wdio-reportportal-reporter');
const path = require('path');
const fs = require('fs');
exports.config = {
...
  async afterTest(test) {
    if (test.passed === false) {
      const filename = "screnshot.png";
      const outputFile = path.join(__dirname, filename);
      await browser.saveScreenshot(outputFile);
      reportportal.sendFileToTest(test, 'info', filename, fs.readFileSync(outputFile));
    }
  }
...
Jasmine の例
const reportportal = require('wdio-reportportal-reporter');
const path = require('path');
const fs = require('fs');
exports.config = {
...
  async afterTest(test) {
    if (test.passed === false) {
      const filename = "screnshot.png";
      const outputFile = path.join(__dirname, filename);
      await browser.saveScreenshot(outputFile);
      //!!
      Object.assign(test, {title: test.description}}
      reportportal.sendFileToTest(test, 'info', filename, fs.readFileSync(outputFile));
    }
  }
...
WDIO Cucumber "5.14.3+" の例
const reportportal = require('wdio-reportportal-reporter');
exports.config = {
...
   afterStep: async function (uri, feature, { error, result, duration, passed }, stepData, context) {
     if (!passed) {
        let failureObject = {};
        failureObject.type = 'afterStep';
        failureObject.error = error;
        failureObject.title = `${stepData.step.keyword}${stepData.step.text}`;
        const screenShot = await global.browser.takeScreenshot();
        let attachment = Buffer.from(screenShot, 'base64');
        reportportal.sendFileToTest(failureObject, 'error', "screnshot.png", attachment);
    }
  }
...
}
Report Portal UI 起動ページへのリンクを取得する
const RpService = require("wdio-reportportal-service");
...
    onComplete: async function (_, config) {
        const link = await RpService.getLaunchUrl(config);
        console.log(`Report portal link ${link}`)
    }
...
または、より複雑な方法
const RpService = require("wdio-reportportal-service");
...
    onComplete: async function (_, config) {
        const protocol = 'http:';
        const hostname = 'example.com';
        const port = ':8080'; // or empty string for default 80/443 ports
        const link = await RpService.getLaunchUrlByParams(protocol, hostname, port, config);
        console.log(`Report portal link ${link}`)
    }
...
既存の起動にテストを報告する
既存のアクティブな起動にテストを報告する場合は、環境変数 `REPORT_PORTAL_LAUNCH_ID` によってレポーターに渡すことができます。起動の終了と開始は、ユーザーの責任となります。
export REPORT_PORTAL_LAUNCH_ID=SomeLaunchId
npm run wdio
ライセンス
このプロジェクトは MIT ライセンスの下でライセンスされています。詳細については、LICENSE.md ファイルを参照してください。