Search code examples
javacucumberserenity-bdd

Cucumber BDD Custom Parameter Definition not working for feature with examples


I'm trying to write step definitions with Serenity Cucumber BDD

This is my feature:

@test1
      Scenario Outline: I need to try to Sign up as a new user 
            Given I have clicked on the Sign up link
            When I enter <username> and <password>
            And I click on sign up button
            Then I must see Success message
            Examples:
                  | username   | password |
                  | user001    | test123  |
                  | user002    | test123  |

My step definition


 @When(value = "I enter {word} and {word}")
    public void iAddUserNameAndPassword(String userName, String password) {
        user.addNewUserInfo(userName, password);

How can I use "userName" and "password" instead of "word" in the step definition

I try to give the parameter definition shown below, but it doesn't work

@ParameterType("word")  // regexp
    public String userName(String userName){  // type, name (from method)
        return new String(userName);       // transformer function
    }


Solution

  • If you are using Cucumber you've got it almost right. I don't actually know what Serenity does.

    The parameter type annotation should have a value that is a regular expression. This regular expression should match the values used in your step.

    @ParameterType("[a-z0-9]+")
    public String userName(String value){
        return value;
    }
    
    @ParameterType("[a-z0-9]+")
    public String password(String value){
        return value;
    }
    

    Then in your step definition you can use the method names of the annotated methods as parameter types.

    @When("I enter {userName} and {password}")
    public void iAddUserNameAndPassword(String userName, String password) {
        user.addNewUserInfo(userName, password);
    }