gkushang / cucumber-html-reporter

Generates Cucumber HTML reports in three different themes
MIT License
234 stars 154 forks source link

Cannot read property 'JsonFormatter' of undefined #83

Closed edgarechm closed 7 years ago

edgarechm commented 7 years ago

This is my hooks file... with Cucumberjs2.0 update.

var Cucumber = require('cucumber');
var fs = require('fs');
var reporter = require('cucumber-html-reporter');
var CucumberHtmlReport = require('cucumber-html-report');
var seleniumWebdriver = require('selenium-webdriver');
var {defineSupportCode} = require('cucumber');

//var hooks = function () {
defineSupportCode(function({After}) {

    var outputDir = './'

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

    var createHtmlReport = function(sourceJson) {
      var CucumberHtmlReport = require('cucumber-html-report');
      var report = new CucumberHtmlReport({
        source: sourceJson, // source json
        dest: outputDir // target directory (will create if not exists)
      });
      report.createReport();
    };

    var JsonFormatter = Cucumber.Listener.JsonFormatter();
    JsonFormatter.log = function(string) {
      if (!fs.existsSync(outputDir)) {
        fs.mkdirSync(outputDir);
      }

      var targetJson = outputDir + 'cucumber_report.json';
      fs.writeFile(targetJson, string, function(err) {
        if (err) {
          console.log('Failed to save cucumber test results to json file.');
          console.log(err);
        } else {
          createHtmlReport(targetJson);
        }
      });
    };

    registerListener(JsonFormatter);
});

I get this error message:

Unhandled rejection TypeError: Cannot read property 'JsonFormatter' of undefined
    at C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\support\hooks.js:36:39
    at C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\support_code_library\builder.js:77:12
    at Array.forEach (native)
    at Object.build (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\support_code_library\builder.js:76:7)
    at Cli.getSupportCodeLibrary (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\cli\index.js:137:32)
    at Cli.<anonymous> (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\cucumber\lib\cli\index.js:144:39)
    at Generator.next (<anonymous>)
    at Generator.tryCatcher (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\util.js:16:23)
    at PromiseSpawn._promiseFulfilled (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\generators.js:97:49)
    at Promise._settlePromise (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\promise.js:574:26)
    at Promise._settlePromise0 (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (C:\workspace\TestFinalMile\trunk\src\test\resources\integration\finalmile\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:649:20)

[11:06:33] E/launcher - BUG: launcher exited with 1 tasks remaining
npm ERR! Test failed.  See above for more details.

It looks like the error is in this line of code: var JsonFormatter = Cucumber.Listener.JsonFormatter();

Any clues to what can be wrong?

Cucumberjs version 2.0 Protractor version 5.1.1

I can see a probable cause. Cucumberjs old version had a completely different implementation of the JsonFormatter method. What do I need to change in my hooks.js in order to have my reports work? Is this the correct place to ask my question?

gkushang commented 7 years ago

@edgarechm

I see you're explicitly generating JSON file and then passing to the html-reporter. Doesn't protactor creates the JSON file by itself?

However, Cucumber V2 has changed the implementation and Cucumber.Listener.JsonFormatter() has changed its place. Could you submit an issue at cucumberjs?

edgarechm commented 7 years ago

Protractor does, if I change output format to Json. However, I need to have it in both outputs. Gherking for me and JSON to create the report.

And yes, kinda thought the issue is with Cucumberjs, from what I saw at the JsonFormatter differences in code. I'll let them know about the issue at cucumberjs.

gkushang commented 7 years ago

@edgarechm

I hope you'll get it resolved through Cucumber.js group. I am closing this issue. Feel free to re-open if you have any further feedback.

Thanks!