How to reuse Cucumber steps with Java

57 views
Skip to first unread message

Gergana Ivanova

unread,
Oct 25, 2016, 9:44:54 AM10/25/16
to Cukes
Hi, 
I try to reuse some Cucumber steps in Java like this:

@When("^I login as Patty$")
public void LoginPage(String userrole, String username,
String password) {
steps "When User Admin enters UserName Patty and Password pass123";
}

@When("^User (.*) enters UserName (.*) and Password (.*)$")
public void enterLoginDetails(String userrole, String username, String password){
MapleActions actions = new MapleActions(getDriver(), getDriverParams());
actions.sendKeysToElement(loginUsername, username);
actions.sendKeysToElement(loginPassword, password);
actions.clickElement(loginButton);
actions.perform();
}

And seems it is not possible with Java. Is there not reusing steps like in Ptthon and Ruby with Cucumber, is it, really?

Any help will be highly appreciated.

Gerry

Thomas Sundberg

unread,
Oct 25, 2016, 9:59:32 AM10/25/16
to cu...@googlegroups.com
On 25 October 2016 at 12:37, Gergana Ivanova <gershu...@gmail.com> wrote:
> Hi,
> I try to reuse some Cucumber steps in Java like this:
>
> @When("^I login as Patty$")
> public void LoginPage(String userrole, String username,
> String password) {
> steps "When User Admin enters UserName Patty and Password pass123";
> }
>

This particular case will not work. You are specifying three arguments
and your regular expression doesn't define any capture groups. You
will get a mismatch in this case.


>
> @When("^User (.*) enters UserName (.*) and Password (.*)$")
> public void enterLoginDetails(String userrole, String username, String
> password){
> MapleActions actions = new MapleActions(getDriver(), getDriverParams());
> actions.sendKeysToElement(loginUsername, username);
> actions.sendKeysToElement(loginPassword, password);
> actions.clickElement(loginButton);
> actions.perform();
> }
>

This cas should work, you are specifying the capture groups and
capture three string arguments.

>
> And seems it is not possible with Java. Is there not reusing steps like in
> Ptthon and Ruby with Cucumber, is it, really?
>

Cucumber will reuse steps if the regular expressions matches. That
behaviour is, as far as I know, consistent for all Cucumber
implementations.

HTH
Thomas

>
> Any help will be highly appreciated.
>
> Gerry
>
> --
> Posting rules: http://cukes.info/posting-rules.html
> ---
> You received this message because you are subscribed to the Google Groups
> "Cukes" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cukes+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Thomas Sundberg
M. Sc. in Computer Science

Mobile: +46 70 767 33 15
Blog: http://www.thinkcode.se/blog
Twitter: @thomassundberg

Better software through faster feedback

George Dinwiddie

unread,
Oct 25, 2016, 11:41:47 AM10/25/16
to cu...@googlegroups.com
Gerry,

On 10/25/16 6:37 AM, Gergana Ivanova wrote:
> Hi,
> I try to reuse some Cucumber steps in Java like this:

Calling steps from steps is deprecated because, while it saves a little
work at first, it soon becomes very hard to maintain.

>
> @When("^I login as Patty$")
> public void LoginPage(String userrole, String username,
> String password) {
> steps "When User Admin enters UserName Patty and Password pass123";
> }
>
> @When("^User (.*) enters UserName (.*) and Password (.*)$")
> public void enterLoginDetails(String userrole, String username, String
> password){
> MapleActions actions = new MapleActions(getDriver(), getDriverParams());
> actions.sendKeysToElement(loginUsername, username);
> actions.sendKeysToElement(loginPassword, password);
> actions.clickElement(loginButton);
> actions.perform();
> }

I suggest extracting the details to a helper method:

public void enterLoginDetails(String userrole, String username, String
password){
MapleActions actions = new MapleActions(getDriver(), getDriverParams());
actions.sendKeysToElement(loginUsername, username);
actions.sendKeysToElement(loginPassword, password);
actions.clickElement(loginButton);
actions.perform();
}

And then call that method from various steps:

@When("^I login as Patty$")
public void loginAsPatty() {
enterLoginDetails( lookupRoleFor("Patty"), "Patty",
"lookupPasswordFor("Patty");
}


@When("^User (.*) enters UserName (.*) and Password (.*)$")
public void loginAsParticularUser(String userrole,
String username,
String password) {
enterLoginDetails(userrole, username, password);
}

Note that I've implied other methods for retrieving shared test data
(lookupXyzFor() methods), but you could use embedded strings for Patty.
I quickly find that extracting such data from the body of the methods
makes things easier.

>
> Any help will be highly appreciated.

I hope that helps,
- George

--
----------------------------------------------------------------------
* George Dinwiddie * http://blog.gdinwiddie.com
Software Development http://www.idiacomputing.com
Consultant and Coach http://www.agilemaryland.org
----------------------------------------------------------------------

Gergana Ivanova

unread,
Oct 26, 2016, 9:28:04 AM10/26/16
to Cukes
Thanks, George! It looks helpful.

/Gerry
Reply all
Reply to author
Forward
0 new messages