How to write numbers in cucumber scenarios

user2340124 picture user2340124 · Sep 9, 2015 · Viewed 11.4k times · Source

I would like to write a number in cucumber page. Please let me know How can I write this.

Scenario Outline: Enter an invalid URL

Given the context "Invalid URL" is open on "Market" 
  When user is on the error 404 page
  Then page not found message is displayed

But I have observed that 404 is taken as a parameter.

Answer

burythehammer picture burythehammer · Sep 9, 2015

Sure, you just have to handle it as a regular expression (regex) in your step definitions. Your step definition could read as following:

@When("^When user is on the error \"(\\d+)\" page$")
public void When_user_is_on_the_error_page(int errorNum) throws Throwable {

...

}

That'll handle 1 or more digits. The \d represents a digit and the + sign represents "1 or more".

I personally like wrapping my parameters in double quotes ("") because then the IDE understands it's a parameter and can help you out with autocomplete, etc.

If you want to restrict it to more specific numbers (say only three digits) you can refine your regex to something like [\d]{3}. Oracle have lessons on Java regex here.