1

我想创建黄瓜 html 报告,我是 Node JS 的新手,我尝试搜索它,我使用了以下

this.registerHandler('AfterFeatures', function(callback) {
        try {
            var options = {
                theme: "bootstrap",
                jsonFile: "/report/cucumber.json",
                output: "/report/cucumber_report.html",
                reportSuiteAsScenarios: true,
                launchReport: true,
                metadata: {
                    "App Version": "0.0.1"
                }
            };
            reporter.generate(options);
        } catch (e) {
            console.log(e);
        }
        callback();
    });

但是当我运行我的代码时,黄瓜功能场景被执行,它最终给了我一个错误说明,

Unable to parse cucumberjs output into json: '/report/cucumber.json' { Error: /report/cucumber.json: ENOENT: no such file or directory, open '/report/cucumber.json'
    at Object.fs.openSync (fs.js:652:18)
    at Object.fs.readFileSync (fs.js:553:33)
    at Object.readFileSync (/Users/sarav/Documents/GitHub/automationtests/node_modules/jsonfile/index.js:67:22)
    at isValidJsonFile (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:404:48)
    at Object.generate (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:426:9)
    at Object.generateReport [as generate] (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/index.js:30:21)
    at /Users/sarav/Documents/GitHub/automationtests/features/support/hooks.js:49:22
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/report/cucumber.json' }

上述代码是否会自动生成 .json 和 .html 文件,或者我们需要手动创建一个 .json 文件并将其转换为 html 报告。

我研究过 Java,它会在执行结束时自动创建 json 和 html 报告。

由于这是非常新的,我无法弄清楚缺少的部分是什么

谢谢

4

1 回答 1

2

您生成 HTML 报告的代码需要 json 文件:/report/cucumber.json 已经存在。

所以你需要其他代码来帮助在测试运行期间生成json文件,我将给出我项目中使用的代码供你参考。

注意:下面的代码只能在 Cucumber 1 上工作,不能在 Cucumver 2 上工作,下面是我使用的版本:

  "dependencies": {
    "cucumber": "1.2.1",
    "cucumber-html-reporter": "0.2.6",

1) cucumber-json-report.js 在运行过程中生成 Cucumber JSON 报告。

var fs = require('fs-extra');
var path = require('path');
var moment = require('moment');
var Cucumber = require('cucumber');

module.exports = function() {

  var JsonFormatter = Cucumber.Listener.JsonFormatter();

  JsonFormatter.log = function(string) {

    var outputDir = './reports';
    var targetJson = outputDir + '/cucumber_report.json';

    if (fs.existsSync(outputDir)) {
      fs.moveSync(outputDir, outputDir + '_' + moment().format('YYYYMMDD_HHmmss') + "_" + Math.floor(Math.random() * 10000), {
        overwrite: true
      });
    }
    fs.mkdirSync(outputDir);
    fs.writeFileSync(targetJson, string);
  };

  this.registerListener(JsonFormatter);
};

2) screenshot.js失败时截图

module.exports = function() {

  this.After(function(scenario, callback) {
    if (scenario.isFailed()) {
      browser.takeScreenshot().then(function(buffer) {
        var decodedImage = new Buffer(buffer, 'base64');
        scenario.attach(decodedImage, 'image/png');
        callback();
      }, function(err) {
        callback(err);
      });
    } else {
      callback();
    }
  });
};

3) cucumber-html-report.js 在所有功能运行结束后生成 Cucumber HTML 报告。

var reporter = require('cucumber-html-reporter');

    module.exports = function() {

      this.AfterFeatures(function(features, callback) {
        var options = {
          theme: 'bootstrap',
          jsonFile: 'reports/cucumber_report.json',
          output: 'reports/cucumber_report.html',
          reportSuiteAsScenarios: true
        };

        reporter.generate(options);
        callback();
      });
};

4) Protractor conf.js在 cucumberOpts.require 中包含以上三个文件

cucumberOpts: {
    monochrome: true,
    strict: true,
    plugin: ["pretty"],
    require:[
        './step_definitions/*step.js',
        './support/screenshot.js',
        './support/cucumber-json-report.js',
        './support/cucumber-html-report.js'
    ],
    tags: '',
},
于 2017-12-29T10:23:20.173 回答