How to use power of varargs while defining step definitions in cucumber java bindings. I have below step
Given I have following product: prod1, prod2, prod3
My Step definition
@Given("^I have following product [(exhaustive list of products or pattern of the product names)]$")
public void stepDef(String...args)
{
//Process varargs array "args" in here
}
I know workaround can be to except the complete string after colon and then in the code break the string using split(",") and dump it into array. But i just want to know if cucumber on its own supports varargs pattern.
TIA!!!
An annotation followed by the pattern is used to link the Step Definition to all the matching Steps, and the code is what Cucumber will execute when it sees a Gherkin Step. Cucumber finds the Step Definition file with the help of the Glue code in Cucumber Options.
If Cucumber is telling you that your steps are undefined, when you have defined step definitions, this means that Cucumber cannot find your step definitions. You'll need to make sure to specify the path to your step definitions (glue path) correctly.
A Step Definition is a Java method Kotlin function Scala function JavaScript function Ruby block with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute.
I dont know if varargs are supported in cucumber, but maybe you can archieve your goal with direct list matchings?
You can define Example Lists in the Feature files in Cucumber
You can define them at the end of a Step:
@Given i want a list
|Entry1|
|Entry2|
Or inline:
@Given i want a list: Entry1, Entry2
Then you can have glue code like:
@Given(^i want a list$)
public void i_want_a_list(List<String> entries) {
//do stuff
}
@Given(^i want a list: (.*)$)
public void i_want_a_list(List<String> entries){
//Do something with the list
}
you can find more info here: https://cukes.info/docs/reference/jvm#step-definitions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With