0

Although similar to Optional parameter in cucumber, still it is for ruby programmers which I am not familiar with...

So what I'd like to have is to be able to use either one of those two options:

Given REST ADCC Login with username "admin" and password "admin"

and

Given REST ADCC Login with username "admin"

For the latter I would allow myself to set the password with the username value for testing purposes only.

The step implementation as I understood is:

@Given("^REST ADCC Login with username \"(.*)\"(?: and password \"(.*)\")?( negative)?$")
public void login(String username, String password, String negative) {
        loginLogoutStepsHandler.login(username, password);
}

but then, when I use the first step option, with the password defined, the password param gets null as it should have been "admin".

What is wrong with my syntax?

Thanks!

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
dushkin
  • 1,939
  • 3
  • 37
  • 82

2 Answers2

2

Create two step definitions. Call one from the other:

@Given("REST ADCC Login with username \"(.*)\" and password \"(.*)\"")
public void restADCCLoginWithUsernameAndPassword(String username, String password) {
    // log in...
}

@Given("REST ADCC Login with username \"(.*)\"\"")
public void restADCCLoginWithUsernameAndPassword(String username) {
    restADCCLoginWithUsernameAndPassword(username, "test");
}

It achieves code reuse without getting clever with regular expressions.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • 1
    Thanks! It didn't work for me. However, I managed to find the solution, also following your idea to abandon the single step solution :) As you can see, the changes between your solution and the one which worked are minor. Thanks again! – dushkin Aug 20 '19 at 13:16
  • To be clear, the `.*` regex construct is greedy by default so the second regexp matches everything between the first and last quotes – ashley Apr 12 '22 at 12:12
  • Oh, geez. I didn't realize the two steps were so similar. The `.*` syntax would have caused both steps to be a match for the `with password` variant. – Greg Burghardt Apr 12 '22 at 12:20
0

for whoever may be of interest for them, what eventually worked (following the idea of Greg of using two separate implementations) for me was:

@Given("^REST ADCC Login with username \"([^\"]*)\"$")
public void rest_ADCC_Login_with_username(String username) throws Throwable {
    loginLogoutStepsHandler.login(username, username);
}

@Given("^REST ADCC Login with username \"([^\"]*)\" and password \"([^\"]*)\"$")
public void rest_ADCC_Login_with_username_and_password(String username, String password) throws Throwable {
    loginLogoutStepsHandler.login(username, password);
}
dushkin
  • 1,939
  • 3
  • 37
  • 82