2

I'm refactoring my script and am currently doing the following;

When("I click the button {string}", (buttonID, next) => {

    pageElement = driver.findElement(By.id(buttonID));
    driver.wait(until.elementIsVisible(pageElement), 10000);
    pageElement.click();
    next();
});

And("I click the button {string}", (buttonID, next) => {

    pageElement = driver.findElement(By.id(buttonID));
    driver.wait(until.elementIsVisible(pageElement), 10000);
    pageElement.click();
    next();
});

I am doing this because of readability purposes, i have a scenario where 'And' is more readable and also scenarios where 'When' is more applicable.

I've seen the following..

@Then("^(?:it's do something|it's do another thing)$");

Which allows for multiple scenario names to be used for the same function but sadly, i'm looking for the opposite.

Any help would be greatly appreciated.

Jack Williams
  • 195
  • 1
  • 1
  • 15

2 Answers2

2

We are working with Specflow and when we need to use same step as When And or Then, we just do the following:

[Given(@"I enter all required customer information")]
[When(@"I enter all required customer information")]
[Then(@"I enter all required customer information")]
public void GivenIEnterAllRequiredCustomerInformation()
{
   MyMethod();
}

So, in your case, try something like:

When("I click the button {string}", And("I click the button {string}", (buttonID, next) => {
    pageElement = driver.findElement(By.id(buttonID));
    driver.wait(until.elementIsVisible(pageElement), 10000);
    pageElement.click();
    next();
});
IPolnik
  • 619
  • 5
  • 13
2

Thank you @IPolnik your solution was corrrect apart from one part, to seperate the 'When' and the 'And' you use a comma, displayed below.

 When ("I click the button {string}", And ("i click the button {string}"), (buttonID, next) => {

    pageElement = driver.findElement(By.id(buttonID));
    driver.wait(until.elementIsVisible(pageElement), 10000);
    pageElement.click();
    next();
});

Without your suggestion i would never have gotten there though, so thank you very much and that is also why i said that you resolved my question.

Have a great day, Jack.

edit: I have also found that this syntax also works

   When ("I click the button {string}" | And ("i click the button {string}"), (buttonID, next) => {

    pageElement = driver.findElement(By.id(buttonID));
    driver.wait(until.elementIsVisible(pageElement), 10000);
    pageElement.click();
    next();
});

I believe would probably be better practice.

Jack Williams
  • 195
  • 1
  • 1
  • 15