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
----------------------------------------------------------------------