Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber JS Get current Feature / Scenario / Step in World

How can i get current Feature, Scenario and Step in World?

I tried this way but I only have the Scenario name and description :

module.exports = function () {
    /**
     * Before each scenario
     */
    this.Before(function (scenario, callback) {
        console.log(scenario);
        callback();
    });
};

Thanks for your help.

like image 690
rbaaboud Avatar asked Dec 22 '14 14:12

rbaaboud


1 Answers

Ok, So I finally found this solution: create a Context object called in hooks.

hooks.js file:

var context = require(process.cwd() + '/src/e2e/support/context');

module.exports = function Hooks() {

    this.BeforeFeature(function (event, callback) {
        context.setCurrentFeature(event.getPayloadItem('feature'));

        callback();
    });

    this.BeforeScenario(function (event, callback) {
        context.setCurrentScenario(event.getPayloadItem('scenario'));

        callback();
    });

    this.BeforeStep(function (event, callback) {
        context.setCurrentStep(event.getPayloadItem('step'));

        callback();
    });
};

The context object has just getter/setter methods.

You can now access current feature/scenario/step anywere in your code.

For my exemple, in World:

var context = require(process.cwd() + '/src/e2e/support/context');

module.exports = function () {
    this.World = function World(callback) {
        this.handleError = function (error, callback) {
            var _this = this;

            browser.takeScreenshot().then(function (imageData) {
                var formatFeature = helperString.slugify(context.getCurrentFeature().getName());
                var formatScenario = helperString.slugify(context.getCurrentScenario().getName());

                var token = formatFeature + '_' + formatScenario;
                var path = process.cwd() + '/logs/test/e2e/';

                var pngStream = fs.createWriteStream(path + token + '_screenshot.png')    ;

                pngStream.write(new Buffer(imageData, 'base64'));
                pngStream.end();

                _this.delayCallback(function handleErrorCallback() {
                    callback.fail(new Error(error));
                });
            });

            return _this;
        };
    };
};
like image 152
rbaaboud Avatar answered Sep 20 '22 22:09

rbaaboud