最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cucumber JS Get current FeatureScenarioStep in World - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question asked Dec 22, 2014 at 14:48 rbaaboudrbaaboud 1311 silver badge8 bronze badges 1
  • In this code you can see a function getLastStep github./cucumber/cucumber-js/blob/master/lib/cucumber/ast/… maybe this can help – Brice Favre Commented Dec 30, 2014 at 9:08
Add a ment  | 

2 Answers 2

Reset to default 5

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;
        };
    };
};
var driver = require('./world.js').getDriver();
var path = require("path");
var fs = require("fs");
var mkdirp = require('mkdirp');
var sanitize = require("sanitize-filename");


var myHooks = function () {

  this.Before(function (scenario) {
    this.driver.manage().window().maxsize();
  });

  this.After(function (scenario) {
    this.driver.takeScreenshot().then(function (data) {
      var now = new Date().toLocaleString().replace(/T/, ' ').replace(/:/g,'_');
      var nowtime = now.split(" ");
      var screenshotPath = path.join(__dirname,'../../screenshots', 'normal',nowtime[0]);
      mkdirp(screenshotPath, function (err) {
          if (err) console.error(err);
      });
      var base64Data = data.replace(/^data:image\/png;base64,/, "");
      fs.writeFile(path.join(screenshotPath, nowtime[1] + sanitize(scenario.getName() + ".png").replace(/ /g, "_")), base64Data, 'base64', function (err) {
        if (err) console.log(err);
      });
    });
    if (scenario.isFailed()) {
      this.driver.takeScreenshot().then(function (data) {
        var decodedImage = new Buffer(data, 'base64').toString('binary');
        scenario.attach(decodedImage, 'image/png');
      });
    }
    return this.driver.manage().deleteAllCookies();
  });

  this.registerHandler('AfterFeatures', function (event) {
    return driver.quit();
  });

};

module.exports = myHooks;
发布评论

评论列表(0)

  1. 暂无评论