无法在新的JsonFormatter中读取未定义的属性'on'

时间:2017-08-22 04:07:08

标签: typescript protractor cucumber cucumberjs

我正在尝试使用 cucumber-html-reporter 生成HTML报告

收到错误:

Unhandled rejection TypeError: Cannot read property 'on' of undefined
at new JsonFormatter (C:\path-to-project\node_modules\cucumber\src\formatter\json_formatter.js:21:5)
at Object.<anonymous> (C:\path-to-project\test\reporting\CucumberReportExtension.ts:43:27)

检查release notes未定义的参数。我不确定这个问题是否属于那个! 如果是这样,有人可以请我解决这个问题。

另外请告诉我,以便您有更好的解决方案,使用typescript生成带黄瓜量角器的报告。 (请考虑以下版本)

量角器版本: 5.1.2

黄瓜版: 3.0.0

ScenarioHook.ts

import {defineSupportCode} from "cucumber";
import {CucumberReportExtension} from 
"../reporting/CucumberReportExtension";

defineSupportCode(({AfterAll}) => {

  AfterAll(async () => {
     new CucumberReportExtension().myJsonFormatter;
  });
});

CucumberReportExtension.ts

import * as fs from 'fs';
import { mkdirp } from 'mkdirp';
import * as report from 'cucumber-html-reporter';
let Cucumber = require('cucumber');

export class CucumberReportExtension {

 private jsonDir = process.cwd() + "/reports/json";
 private htmlDir = process.cwd() + "/reports/html";
 private jsonFile = this.jsonDir + "/cucumber_report.json";

 private cucumberReporterOptions = {
    theme: "bootstrap",
    jsonFile: this.jsonFile,
    output: this.htmlDir + "/cucumber_reporter.html",
    reportSuiteAsScenarios: true,
    metadata: {
        "App Version":"0.0.1",
        "Test Environment": "TestingMicroSheet",
        "Browser": "Chrome  59.0.945",
        "Platform": "Windows 10",
        "Parallel": "Scenarios",
        "Executed": "Local"
    }
 };

 private CreateReportFile(dirName, fileName, fileContent) {
    //Check if the directory exist
    if (!fs.existsSync(dirName))
        mkdirp.sync(dirName);
    try {
        fs.writeFileSync(fileName, fileContent);
    }
    catch (message) {
        console.log("Failed to create File/Directory :" + message);
    }
 }

 private GenerateCucumberReport(cucumberReportOption){
    report.generate(cucumberReportOption);
 }

 myJsonFormatter = new Cucumber.JsonFormatter({
    log: jLog => {
        this.CreateReportFile(this.jsonDir, this.jsonFile, jLog);
        this.GenerateCucumberReport(this.cucumberReporterOptions);
    }
 });
}
export let JsonFormatter = new CucumberReportExtension().myJsonFormatter;

config.ts

import { Config } from 'protractor';

export let config: Config = {

 seleniumAddress: 'http://localhost:4444/wd/hub',

 framework: 'custom',
 frameworkPath: require.resolve('protractor-cucumber-framework'),
 specs: ["../features/*.feature"],

 baseUrl: "http://localhost:4200/",

 cucumberOpts: {
    compiler: "ts:ts-node/register",
    strict: true,
    format: ['json:../reporting/results.json'],
    require: ['../steps/*.js', '../hooks/*.js'],
    tags: '@smoke'
 }
}

的package.json

{
  "name": "protractor-cucumber-sample",
  "version": "0.0.0",
  "private": true,
  "scripts": {
  "start": "node ./bin/www"
   },
  "dependencies": {
  "@types/jasmine": "^2.5.53",
  "body-parser": "~1.17.1",
  "cookie-parser": "~1.4.3",
  "debug": "~2.6.3",
  "express": "~4.15.2",
  "jade": "~1.11.0",
  "morgan": "~1.8.1",
  "serve-favicon": "~2.4.2"
   },
 "devDependencies": {
 "@types/chai": "^4.0.3",
 "@types/cucumber": "^2.0.3",
 "@types/mkdirp": "^0.5.0",
 "chai": "^4.1.1",
 "chai-as-promised": "^7.1.1",
 "cucumber": "^3.0.0",
 "cucumber-html-reporter": "^2.0.3",
 "jasmine": "^2.7.0",
 "jasminewd2": "^2.1.0",
 "protractor-cucumber-framework": "^4.0.2",
 "ts-node": "^3.3.0",
 "typescript": "^2.4.2"
  }
 }

如果需要其他信息可以帮助您解决此问题,请与我们联系。

如果这不是正确的论坛,请您使用正确的论坛详细信息回复我发布此问题。

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用最新的黄瓜3.0,您必须将节点event emitter传递给自定义格式化程序。有关节点事件如何工作的详细信息,请通过此链接 - enter image description here

现在在CucumberReportExtension.ts

中使用此功能
import * as fs from 'fs';
import { mkdirp } from 'mkdirp';
import * as report from 'cucumber-html-reporter';
let Cucumber = require('cucumber');
import { EventEmitter } from 'events';
const eventBroadcaster = new EventEmitter();

export class CucumberReportExtension {

private jsonDir = process.cwd() + "/reports/json";
private htmlDir = process.cwd() + "/reports/html";
private jsonFile = this.jsonDir + "/cucumber_report.json";

private cucumberReporterOptions = {
theme: "bootstrap",
jsonFile: this.jsonFile,
output: this.htmlDir + "/cucumber_reporter.html",
reportSuiteAsScenarios: true,
metadata: {
    "App Version":"0.0.1",
    "Test Environment": "TestingMicroSheet",
    "Browser": "Chrome  59.0.945",
    "Platform": "Windows 10",
    "Parallel": "Scenarios",
    "Executed": "Local"
  }
};

private CreateReportFile(dirName, fileName, fileContent) {
//Check if the directory exist
if (!fs.existsSync(dirName))
    mkdirp.sync(dirName);
try {
    fs.writeFileSync(fileName, fileContent);
}
catch (message) {
    console.log("Failed to create File/Directory :" + message);
 }
}

private GenerateCucumberReport(cucumberReportOption){
report.generate(cucumberReportOption);
 }

 myJsonFormatter = new Cucumber.JsonFormatter({
       eventBroadcaster: eventBroadcaster,
       log: jLog => {
           this.CreateReportFile(this.jsonDir, this.jsonFile, jLog);
           this.GenerateCucumberReport(this.cucumberReporterOptions);
        }
 });
}
export let JsonFormatter = new CucumberReportExtension().myJsonFormatter;

注意:我尚未对其进行测试,但它应该可以使用!我会在完成新黄瓜3.0功能试验后更新答案!