How to write numbers in Cucumber scenarios
Asked Answered
J

3

5

I want to handle numeric parameters in the Cucumber step definitions. Please let me know how I can do 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

However, I have observed that 404 is taken as a parameter.

Jackfish answered 9/9, 2015 at 8:50 Comment(0)
G
9

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.

Gelderland answered 9/9, 2015 at 13:13 Comment(3)
But I dont want cucumber to take it as parameter.Jackfish
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. https://mcmap.net/q/25772/-what-is-a-non-capturing-group-in-regular-expressionsGelderland
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 ...Neoplasticism
M
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.

Maroney answered 11/12, 2019 at 12:57 Comment(1)
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.Gudrunguelderrose
H
0

As an alternative you may use one of the built-in types of Cucumber expressions for number types: {int}, {long}, {byte}, {float}, {double}, {decimal}.

Scenario: Display not found page
  Given the context "Invalid URL" is open on "Market"
  When a user is on the error 404 page
  Then page not found message is displayed
@When("a user is on the error {int} page")
public void userIsOnErrorPage(int httpStatusCode) {
  //
}
Humeral answered 3/7, 2024 at 16:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.