【问题标题】:Loading hooks in CucumberJS with Protractor and Gulp使用 Protractor 和 Gulp 在 CucumberJS 中加载钩子
【发布时间】:2016-09-13 04:57:17
【问题描述】:

我用 Protractor 和 Gulp 设置 CucumberJS。我遵循了此处提供的文档: https://github.com/cucumber/cucumber-js

我有我的功能文件和步骤定义文件。我还在支持文件夹中创建了 world.js 文件,并将其加载到我的步骤定义文件中:

this.World = require("../support/world.js").World;

因此与文档中介绍的方式相同。 直到现在一切正常。

我试着在我的箱子里添加一些黄瓜钩。我按照文档中的建议在支持文件夹中创建了 hooks.js 文件,所以:

// features/support/hooks.js (this path is just a suggestion)

var myHooks = function () {
 this.Before(function (callback) {
    // Just like inside step definitions, "this" is set to a World instance.
    // It's actually the same instance the current scenario step definitions
    // will receive.

    // Let's say we have a bunch of "maintenance" methods available on our World
    // instance, we can fire some to prepare the application for the next
    // scenario:

    console.log("Before hook");

    // Don't forget to tell Cucumber when you're done:
    callback();
  });
};

module.exports = myHooks;

文档没有说明应该如何在我的步骤定义中加载这个 hook.js 文件,所以我假设它以某种方式加载了“约定优于配置”的方法。不幸的是,文件没有加载,也没有执行 Before 方法。

有什么想法吗?

【问题讨论】:

    标签: javascript cucumber bdd cucumberjs


    【解决方案1】:

    如果钩子与您的 step_definitions 不在同一个文件夹中,您需要明确指定钩子在哪里使用 --require。例如,

     cucumber.js test/functional/features/xyz.feature 
    --require test/functional/step_definitions/ 
    --require features/support/ --format=pretty
    

    为了避免这种情况,我通常将我的钩子放在 step_definitions 文件夹下。由于无论如何您都需要为 step_definitions 指定 require,因此您无需为 hooks 显式指定 require 。所以假设你的钩子在test/functional/step_definitions/ 中,跟随你的钩子应该被调用。

     cucumber.js test/functional/features/xyz.feature 
    --require test/functional/step_definitions/ 
    --format=pretty
    

    【讨论】:

    • 谢谢!它可能有效,但我有不同的文件夹结构。例如,要测试用户管理功能,我有:/test/e2/features/user/xyz.feature 和 /test/e2/features/user/step-definitions/step-defs.js。在这种方法中,我需要将 hook.js 放在每个没有意义的功能的 step-def 中。我可以尝试使用 --require 选项,但是,我不确定它如何与 gulp 一起使用。
    • 为什么没有像 /test/e2/features/step-definitions 这样的顶级文件夹,并在其下放置 users 文件夹并将 hooks.js 放在 step-definitions 级别。或者在您当前的设置中,您可以尝试将 hooks.js 放在功能文件夹下。这也应该有效。让我们知道哪些有效,哪些无效
    • 第一种方法实际上并不奏效,因为我想将功能文件和步骤定义一起保存在同一个文件夹中,因此将 /test/e2/features/user/xyz.feature 和 /test 放在一起/e2/features/user/step-definitions/step-defs.js。如果我理解正确的话,你的想法让我把我的功能文件放在 /test/e2/features/step-definitions/user 文件夹下。我也尝试将 hooks.js 放在 fetures 文件夹下,但没有帮助。
    • 由于步骤定义对所有测试都是全局的,因此将它们“分组”在功能文件夹下实际上没有意义。由 Pragmatic Press 出版的 Cucumber Book 是一本了解如何使用 cucumber 的好书(即使它的示例是用 Ruby 编写的——但 cucumber 最初是为 Ruby 社区编写的)。无论如何,推荐的文件夹结构是一个features 文件夹,其中包含所有*.feature 文件,其中包含一个包含所有步骤定义的step_definitions 文件夹,以及一个features 文件夹中的support 文件夹,其中包含@987654330 @ 和任何钩子。
    • 另外,请注意,通过将 step_definitionssupport 代码放在 features 文件夹下,您不需要使用 --require 标志,因为 cucumber 会自动查找步骤在*.feature 文件所在的文件夹中递归定义和支持代码。
    【解决方案2】:

    一旦您有了hooks.js 文件,请转到 protractor.conf.js 文件中的 cucumberOpts 并将路径添加到 hooks.js文件在那里,就是这样,你的 hooks.js 文件将被加载。

    cucumberOpts: {
        require: [
          conf.paths.e2e + '/steps/**/*Steps.js',
          conf.paths.e2e + '/utilities/hooks.js',
        ],
        tags: ['~@wip', '~@manual'],
        format: 'pretty'
      }
    

    您还可以在 hooks.js 中包含 console.log('Was my hook loaded') > 文件并稍后搜索该日志文本以确保您的挂钩已正确加载。

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 2017-11-27
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多