-1

With the upcoming of Cucumberjs2.3.0, I had to make some changes to the hooks.js file, in order to have cucumber-html-reporter working back again.

The hooks.js explicitly creates a json file with the execution report, that I will later use to generate an HTML report (because I need both JSON and gherkin outputs).

Here is hooks.js, with some updates for Cucumberjs2.3.0:

var JsonFormatter = require('cucumber').JsonFormatter;
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');

'use strict';

defineSupportCode(function({After,registerHandler,registerListener}) {

    var outputDir = './'

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

    var createHtmlReport = function (sourceJson) {
        var report = new CucumberHtmlReport({
            source: sourceJson
            , dest: outputDir
        });
        report.createReport();
    };

    //I don't know what method to call since now JsonFormatter is a class!
    var JsonFormatter = Cucumber.Listener.JsonFormatter();
    JsonFormatter.log = function (string) {
        if (!fs.existsSync(outputDir)) {
            fs.mkdirSync(outputDir);
        }

        var targetJson = outputDir + 'cucumber.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 noticed that Cucumberjs2.3.0, no longer has a Listener class containing JsonFormatter. So that brings me to my problem, I don't know how to call the method in order to create the JSON file, which was in this line of hooks.js:

var JsonFormatter = Cucumber.Listener.JsonFormatter(); ///?????

When I run my tests I get this error:

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.

What do I need to change in my hooks.js, in order to make it work as it worked before May12, when Cucumberjs2.3.0 came up?

Here is my protractor config file, just in case:

var featsLocation = 'features/';
var stepsLocation = 'steps/';

exports.config = {
    directConnect: true,
    chromeDriver: '/srv/build/applications/chromedriver/chromedriver.exe',
    seleniumServerJar: '/srv/build/applications/selenium/selenium-server-standalone-3.4.0.jar',
    rootElement: 'html',
    params:{
        authURL: '',    
        login:{
            email:'',
            passw:''
        }
    },
    resultJsonOutputFile:'',
    getPageTimeout: 30000,
    allScriptsTimeout: 30000,
    framework: 'custom',
    frameworkPath: require.resolve('protractor-cucumber-framework'),
    capabilities: {
        'browserName': 'chrome',
            chromeOptions:{
                args:[]//"--headless"]
            }
        //'browserName': 'phantomjs',
        //'phantomjs.binary.path': '/srv/build/applications/phantomjs/bin/phantomjs'   //<--Comment to run from local
    },

    onPrepare: function(){
        global.EC = protractor.ExpectedConditions;
    },

    specs: [ /**/
              featsLocation+'authenticateCSM.feature'
            , featsLocation+'rejects.feature'
            , featsLocation+'rejects_sprint.feature'
     ],

     baseUrl: '',

     cucumberOpts: {
         tags: '',
         require: [
                   './support/*.js'
                    , stepsLocation+'*.spec.js'

        ],
        monochrome: true,
        strict: true,
        plugin: "json"
     },
};

And this is my package.json too:

{
  "name": "CucumberjsAT",
  "version": "1.0.0",
  "description": "Executes automated test, using Cucumberjs",
  "scripts": {
    "test": "protractor ./protractorConf.js --params.authURL=\"http://myweb.com\" --params.login.email=user@mail.com --params.login.passw=password --cucumberOpts.tags \"not @ignore and (@smoke or @me)\""
  },
  "author": "Kyon"
}

Here's my framework info in case it's helpful...

  • C:\workspace>npm view protractor-cucumber-framework version 3.1.1
  • C:\workspace>npm view cucumber version 2.3.0
  • C:\workspace>protractor --version Version 5.1.1
  • C:\workspace>npm view cucumber-html-reporter latest 2.0.0
  • C:\workspace>npm view cucumber-html-report latest 0.6.0
Kyon Perez
  • 124
  • 1
  • 13

1 Answers1

0

I found myself the solution to the issue.

Providing the name to the json file through passing parameter on the package. Then the following change the the After scenario in hooks.js.

After(function (scenario,callback) {
    let self = this;
    if (scenario.isFailed()) {
        browser.takeScreenshot().then(function (base64png) {
             self.attach(new Buffer(base64png, 'base64'), 'image/png');
             callback();
        }, function (err) {
            return callback(err);
        });
    } else{
        callback();
    }
});
Kyon Perez
  • 124
  • 1
  • 13