如何在黄瓜中使用可选参数

how use optional parameters in cucumber

我想要相同的 Gherkin 句子(带参数和不带参数):

带有参数的小黄瓜:

When a 'notify' message is sent to the green box with the properties.
 |type|message|
 |error|The error message|

没有参数的小黄瓜:

When a 'notify' message is sent to the green box with the properties.

Java(黄瓜):

@When("^a '(.*)' message is sent to the green box with the properties.$")
public void hello(String name, List<GherkinCondition> conditions) {
    ...
}

我有一个错误,因为 java 方法是用 2 个参数声明的,而在 "without paramaters" 的情况下我只有一个。

堆栈跟踪:

cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'steps.CommonSteps.hello(String,GherkinCondition>) in file:/C:/workspace/xxxxx/java/target/classes/' with pattern [^a '(.*)' message is sent to the green box with the properties.$] is declared with 2 parameters. However, the gherkin step has 1 arguments [notify]. 

Cucumber 步骤定义不支持可选参数。

要么你写两个不同的步骤定义,要么你可以为第二种情况提供一个空数据表。

When a 'notify' message is sent to the green box with the properties.
 |type|message|

甚至

When a 'notify' message is sent to the green box with the properties.
     |||

我创建了一个 PR 来解决这个问题:https://github.com/cucumber/cucumber-jvm/pull/1056

Gherkin 脚本测试:

@hello 
Feature: hello (Function to validate the environment.) 

    Scenario Outline: Function to validate the environment.
        Given me a hello, please. Best Regards '<author>'.
        Given me a hello, please. Best Regards '<author>'.
            |key|expected|actual|
        Given me a hello, please. Best Regards '<author>'.
            |key|expected|actual|
            |zip|35000|<zip>|
            |city|Rennes|<city>|

        Given me a bye, please. Best Regards '<author>'.
        Given me a bye, please. Best Regards '<author>'.
            |zip|<zip>|
            |city|<city>|

        Given me a cat, please. Best Regards '<author>'.

    Examples:
    #Begin Examples#
    |author|zip|city|
    |Jenkins Q1|35000|Rennes|
    |Jenkins Q2|35000|Rennes|
    #End Examples#

Java黄瓜代码:

    @Given("^me a hello, please. Best Regards '(.*)'.$")
    public void hello(String name, List<GherkinCondition> conditions) {
        int i = 0;
        for (GherkinCondition gherkinCondition : conditions) {
            i++;
            logger.info(String.format("  expected contition N°%d=%s", i, gherkinCondition.getExpected()));
            logger.info(String.format("  actual   contition N°%d=%s", i, gherkinCondition.getActual()));
        }
        logger.info("Hello " + name + "!");
    }

    @Given("^me a cat, please. Best Regards '(.*)'.$")
    public void hello(String name) {
        logger.info("Take my cat " + name + "!");
    }

    @Given("^me a bye, please. Best Regards '(.*)'.$")
    public void bye(String name, Map<String, String> params) {
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            i++;
            logger.info(String.format("  Key N°%d: %s   Value:%s", i, entry.getKey(), entry.getValue()));
        }
        logger.info("Bye " + name + "!");
    }

这对我有用。我可以通过 IND 或 USA。注意:如果您没有通过任何 IND,USA,这将不起作用。

@When("^I search (.*) (IND|USA)? in Database$")
public void searchInDatabase(String name, String country){
    if(country.equals("IND"))
        dbObj.searchDatabase(name, true);
    else if(country.equals("USA"))
        dbObj.searchDatabase(name, false);
    else
        dbObj.searchDatabase(name, true); 
}

我得到了问题的答案:-

@When("^我搜索 (.*) in( IND| USA)? Database$")

我正在寻找 USA 或 IND 的可选参数。上面一行解决了我的问题。 ( 和 |

之后需要空格

我是这样做的:

@Then("^Something is (not )?counted$")
public void somethingCounted(String optional) {
    //
}

正则表达式同时匹配 "Something is counted" 和 "Something is not counted"

对于第一种情况,您将获得 "not" 值,对于第二种情况,您将获得空值。

无法在 Java 中具体说明如何解决此问题,但是在使用 PHP 时,我认为您可以通过将参数设置为 NULL 来实现您的目标,即:

假设您想检查颜色,而您的小黄瓜状态如下:

Given I want to check colors "red" and "green"

你会有这样一个函数:

public function iCheckColors($arg1, $arg2) {
    // do stuff here;
}

然后你可能想检查你的小黄瓜线中的三种颜色,例如:

Given I want to check colors "red" and "green" and "blue"

在这种情况下,您需要将第三个参数添加到您的函数中:

public function iCheckColors($arg1, $arg2, $arg3) {
    // do stuff here;
}

这里的问题是,如果你再次使用第一个 Gherkin 行,你的函数会抛出一个错误,因为她需要第三个参数,但是如果你将这个参数设置为 NULL:

public function iCheckColors($arg1, $arg2, $arg3 = NULL) {
    // do stuff here;
}

那么添加到函数中的两条 Gherkin 行应该都可以工作。它仍然需要两个参数,如果您将第三个参数添加到 Gherkin 行,它会覆盖 NULL 值。

希望这就是您要实现的目标。

目前还没有解决办法;

Alternative text only works when there is no whitespace between the alternative parts.

因此您使用 () -> optional 的方式是我们目前最好的。