Javascript 获取当前的功能/场景/步骤

Javascript 获取当前的功能/场景/步骤,javascript,cucumber,cucumberjs,Javascript,Cucumber,Cucumberjs,我如何在世界上获得当前的功能、场景和步骤 我尝试过这种方式,但我只有场景名称和描述: module.exports = function () { /** * Before each scenario */ this.Before(function (scenario, callback) { console.log(scenario); callback(); }); }; 谢谢你的帮助。好的,我终于找到了这个解决方

我如何在世界上获得当前的功能、场景和步骤

我尝试过这种方式,但我只有场景名称和描述:

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

谢谢你的帮助。

好的,我终于找到了这个解决方案:创建一个名为hooks的上下文对象

hooks.js
文件:

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();
    });
};
context
对象只有getter/setter方法

您现在可以访问代码中的当前功能/场景/步骤

以我为例,在世界上:

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;
        };
    };
};

在这段代码中,您可以看到一个函数getLastStep,这可能会有所帮助
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;