javascript - Cucumber JS 获取当前功能/场景/世界步骤

标签 javascript cucumber cucumberjs

如何获取当前的功能、场景和世界步数?

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

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

感谢您的帮助。

最佳答案

好的,所以我终于找到了这个解决方案:创建一个在 hooks 中调用的 Context 对象。

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

关于javascript - Cucumber JS 获取当前功能/场景/世界步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27605001/

相关文章:

javascript - 为单选按钮中的一项选择创建规则

javascript - 三.JS r71 : Transparent renderer doesn't work with post-processing filters

javascript - onClick 不适用于移动设备(触摸)

javascript - 从 JavaScript 中高精度检索地理位置的最佳模式是什么?

ruby - 堆栈级别太深(SystemStackError)

javascript - 错误 : Invalid Chai property: toBe. 您是说 "to"吗?

eclipse - 如何在 eclipse 中禁用 cucumber 消息

iphone - cucumber :如何从键盘输入文本到文本字段?

xpath - 使用 cssSelector 获取子元素

rest - 使用 Nightwatch.js 实现 REST API 测试的最佳方式