4

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.

RBT
  • 24,161
  • 21
  • 159
  • 240
user2340124
  • 81
  • 3
  • 3
  • 12

2 Answers2

8

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.

burythehammer
  • 382
  • 2
  • 6
  • 1
    But I dont want cucumber to take it as parameter. – user2340124 Sep 09 '15 at 15:39
  • You do not have to use the captured int errorNum if you wish. Alternatively, you can use regular expression non-captures. That seems like overkill to me though. http://stackoverflow.com/questions/3512471/non-capturing-group – burythehammer Sep 09 '15 at 16:11
  • don't use a regular expression just use a string. In Cuke(ruby) this would be `When "the user is on the error 404 page" do ...` – diabolist Sep 11 '15 at 00:04
0

With

When user is on the error 404 page

You could use in the steps:

@When("^When user is on the error (.*) page$")
   public void When_user_is_on_the_error_page(int errorNum) {
      ...
    }

the (.*) will take whatever is in that slot and use as a variable that will be defined by the type between the (). In this case, the '400' will be converted to a int, thanks to:

(**int** errorNum){ ...}

If you have no practice with regex, it can be really useful since it will make your steps easier to read, since the (.*) doesn't have a pre defined type.

  • Welcome to SO. As far as I can see other person suggested to cut status code and pass it to variable. Are you sure the "400" will be properly converted into number? If you think that your answer is better compared to existing accepted answer then please add explanation to your post. – Maxim Sagaydachny Dec 11 '19 at 13:16