Search code examples
cucumbercucumber-jvmcucumberjscucumber-javacucumber-junit

How to ignore stepdefinition variable declaration in cucumber for same value


How to ignore stepdefination variable declaration in cucmber for same value?

So suppose I have example as below:

Scenario Outline: Looking up the definition of fruits
    Given the user is on the Wikionary home page
    When the user <name> looks up the definition of the word <name>
    Then they should see the definition 'An edible fruit produced by the pear tree, similar to an apple but elongated towards the stem.'
    Examples:
    | name |
    | pear |

Step definition like below:

@When("^the user (.*?) looks up the definition of the word (.*?)$")
public void when(String name, String name2){
  System.out.println(name);
  System.out.println(name2);
}

Now in above step I have created two variables unnecessary and I am doing it because my cucumber report should get the name at two places in the when statement.
If I put only one variable then cucumber will throw an error.

Please let me know if you need any further information or if I am missing anything.


Solution

  • Use non-capturing group in regex - (?:.*?) to any of the groups. Only one argument will then be required in method.

    https://agileforall.com/just-enough-regular-expressions-for-cucumber/