【问题标题】:How can I specify step definition file for each feature file in cucumber-js?如何为 cucumber-js 中的每个功能文件指定步骤定义文件?
【发布时间】:2017-02-10 18:44:56
【问题描述】:

我的目标

我正在尝试为大型应用程序创建功能和步骤定义的可扩展结构,我的第一个尝试是尝试将 step_definition 文件链接到功能,以便我可以对不同的步骤定义使用相同的步骤模式。

我的代码

我展示我当前的例子:

我的文件夹结构:

/features/sample.feature
/features/example.feature
/features/step_definitions/sample_steps.js
/features/step_definitions/example_steps.js
/features/step_definitions/common/common_steps.js

在我的sample.feature 我有:

  Scenario: Launching Cucumber
  Given I have some step definitions
   When I check some step definition with parameter "any"
   Then I should see all green "sample"

在我的example.feature 我有:

  Scenario: Launching Cucumber
  Given I have some step definitions
   When I check some step definition with parameter "any"
   Then I should see all green "example"

GivenWhen 步骤在 /common/common_steps.js 文件中定义并且工作正常。

Then 步骤同时定义为 sample_steps.jsexample_steps.js,但不同。

在我的 sample_steps.js 我有:

Then('I should see all green {stringInDoubleQuotes}', (arg) => {
   if (arg !== 'sample') {
     throw 'I should see all green when the argument is "sample"';
   }
   return;
 });

最后,在我的 example_steps.js 中,我有:

Then('I should see all green {stringInDoubleQuotes}', (arg) => {
   if (arg !== 'example') {
     throw 'I should see all green when the argument is "example"';
   }
   return;
 });

错误

我的主要目标是在这里全部变绿,但是当然,它不起作用,我得到了这个明显的错误:

Multiple step definitions match:
   I should see all green {stringInDoubleQuotes} - features\step_definitions\example_steps.js:6
   I should see all green {stringInDoubleQuotes} - features\step_definitions\sample_steps.js:6

黄瓜-JVM

我知道在 cucumber-jvm 中我们可以指定一个链接特性和 step_definitions 的glue 属性,这正是我正在寻找的,但是在 cucumber-js 中。 Java 中的示例:

@RunWith(Cucumber.class)
@Cucumber.Options( glue = { "com.app.stepdefinitions.common", "com.app.stepdefinitions.sample" } )
public class SampleFeature{
}

@RunWith(Cucumber.class)
@Cucumber.Options( glue = { "com.app.stepdefinitions.common", "com.app.stepdefinitions.example" } )
public class ExampleFeature{
}

终于

如何使用 cucumber-js 实现与 cucumbr-jvm 相同的功能?

【问题讨论】:

    标签: javascript bdd cucumberjs


    【解决方案1】:

    你不能这样做吗:

    Then('I should see all green "sample"', () => {
       return;
     });
    
    
    Then('I should see all green "example"', () => {
       return;
     });
    

    在较大代码库中的步骤定义是许多步骤非常相似。即:我导航到“页面” - 您可以将代码放在一个共同的地方,并将网址放在其他地方。

    就个人而言,我会这样做:

    Then('I should see all green "(.*)"', (arg) => {
    
     // DO SOME COMMON STUFF
    
     arg = arg.toLowerCase();
     if(arg === "sample"){
       // DO SAMPLE OWN STUFF
     }else if(arg === "example"){
       // DO EXAMPLE STUFF
     }
    
     // CARRY ON WITH COMMON STUFF
    });
    

    但这可能会很长。如果他们做的事情完全不同,请使用第一个选项,如果他们做的事情几乎相同,请使用第二个选项。

    实际上可能你只需要这样做:

    Then('I should see all green "(.*)"', (arg) => {
    
     // DO SOME COMMON STUFF
     arg = arg.toLowerCase();
     var result = runTheseSteps(arg);
    
     // CARRY ON WITH COMMON STUFF
    });
    

    编辑

    我找到了一种方法可以做到这一点。这将是一个通用功能,不属于您的要求(对不起),但我相信它可能是让它工作的唯一方法。

    在你的钩子里:

    this.Before(function (scenario, callback) {
        var tags = [];
        for(var i=0;i<scenario.getTags().length; i++ ){
            tags[i] = scenario.getTags()[i].getName();
        }
        global.scenarioTags = tags.join(", ");
        callback();
    });
    

    在您的步骤定义中:

    this.Given(/^I should see all "(.*)"$/, function (arg) {
    
        if (scenarioTags.includes("@sample") != null) {
            // DO THE SAMPLE STUFF
        } else {
            // DO THE EXAMPLE STUFF
        }
    });
    

    虽然它使您的步骤定义通用,但这意味着您可以将任何内容放在双引号中,并且只要您标记您的场景,正确的代码就会运行。

    @sample
      Scenario: Launching Cucumber
       Given I have some step definitions
       When I check some step definition with parameter "any"
       Then I should see all green "sample"
    
    @example
      Scenario: Launching Cucumber
       Given I have some step definitions
       When I check some step definition with parameter "any"
       Then I should see all green "example"
    

    【讨论】:

    • 嗯...我想避免使用接收参数的超级通用方法,因为它使步骤过于复杂。您的第一个选择仍然是我现在正在做的事情。但这让我在功能文件中放了一些技术性的词来区分步骤...... :(
    • 查看我的编辑,它可能是通用的,但会做你想做的,直到 Cucumber 提供一种将实际步骤粘合到场景的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多