Protractor-CucumberJS - 将多个 Gherkin 语句映射到一个单步函数

Protractor-CucumberJS - Map Multiple Gherkin Statements to a Single Step Function

是否可以为多个 given-when-then 语句映射一个公共步进函数?在 Cucumber JS 中,步骤定义的格式为

this.Given(/^I have the following for a particular test$/, function () {
        //code for the step
    });

对于 BDD (Specflow) 的 C# 版本,多个 gherkin 语句可以绑定到一个方法。例如

[Given(@"I have the following for a particular test")]
[Given(@"I have also the following for another test")]
public void GivenIHaveTheFollowingForAParticularTest()
{

}

在 CucumberJS 中是否也有实现此功能的机制?

是的,您可以在 cucumber.js 中使用正则表达式来做到这一点。例如:

this.Given(/^I have (?:the following for a particular|also the following for another)? test&/, function () {
    // code for step
});

以上正则表达式将匹配这两个语句,因此将为这两个语句调用此步骤定义。

要测试它,请看这里:http://www.regextester.com/?fam=95418