Just Enough Regular Expressions for Cucumber – Agile For All

Jon Archer wrote about how Cucumber makes knowledge of regular expressions important. He’s right: Regular expressions are the key to Cucumber’s flexibility. Well-crafted regular expressions let you reuse step definitions, avoiding duplication and keeping your tests maintainable. But even experienced developers … ( Read More on source )

Source: Just Enough Regular Expressions for Cucumber – Agile For All

Cucumber statement definition parameters : 1st, 2nd, 3rd, 4th

Sometimes there is need to handle one of many ordered items on page, we can use integer as identifier, but it looks unnatural, for example :

User named “John” is author of 1 article

The number is misleading : it can be read as one, but we would like it to be read like “first”.

Below there is a Java example, where you can use both patterns (1st or 1).

 @Then("^User named \"(.*)\" is author of \"(\\d+)(?:st|nd|rd|th)\" article$")
public void UserNamedIsAuthorOfNthArticle(String name, int nth) {
    boolean isAuthor = testPage.getNthArticle(nth).getAuthor().equals(name);
    assertTrue("User named " + name + " is author of " + nth + " article", isAuthor);
}