简体   繁体   English

如何使用Node JS创建Selenium Cucumber html报告

[英]How to create Selenium Cucumber html reports with Node JS

I want to create cucumber html reports and I am new to Node JS and I tried searching for it and I used the following 我想创建黄瓜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();
    });

But when I run my code, The cucumber feature scenarios gets executed and it finally gives me an error stating, 但是当我运行我的代码时,执行黄瓜功能方案,最终给了我一个错误,指出:

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' }

Do the above code automatically generates .json and .html file or we need to manually create a .json file and converts that into a html report. 执行上面的代码会自动生成.json和.html文件,或者我们需要手动创建.json文件并将其转换为html报告。

I have worked on Java and it automatically creates the json and html reports at the end the execution. 我使用过Java,它在执行结束时自动创建json和html报告。

As this is very new I am not able to figure out whats the missing part 由于这是非常新的内容,因此我无法弄清缺少的部分

Thanks 谢谢

Your code to generate HTML report will expect the json file: /report/cucumber.json had been exist. 您的生成HTML报告的代码将期望json文件:/report/cucumber.json已经存在。

So you need other code to help to generate the json file during test running, I will give code used in my project for your reference. 因此,您需要其他代码来帮助在测试运行期间生成json文件,我将提供在我的项目中使用的代码供您参考。

Note: below code can only work on Cucumber 1, can't work on Cucumver 2, below is the version I used: 注意:下面的代码只能在Cucumber 1上运行,不能在Cucumver 2上运行,以下是我使用的版本:

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

1) cucumber-json-report.js to generate Cucumber JSON report during running. 1)cucumber-json-report.js在运行期间生成黄瓜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 to take screenshot when failure 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 to generate Cucumber HTML report after all features running end. 3)在所有功能运行结束后, cumcuming-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 to include above three files in cucumberOpts.require 4)量角器conf.js将上述三个文件包含在cummaryOpts.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: '',
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM