Search code examples
protractorbddgherkincucumberjs

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


Can a common step function be mapped for multiple given-when-then statements? In Cucumber JS the step definitions are in the format

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

For In C# version of BDD (Specflow), multiple gherkin statements can be bound to a single method. For e.g.

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

}

Is there any mechanism to implement this in CucumberJS as well?


Solution

  • Yes, you can do that in cucumber.js using regex. For example:

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

    Above regex will match both the statements so this step-definition will get called for both.

    To test it have a look here: http://www.regextester.com/?fam=95418