Hot questions for Using Cucumber in regex

Question:

I have cucumber step like this

Given I am on (Similar Article Alerts|Search Alerts|Journal Alerts) page

I have a step definition for this

  @Given("^I am on \"([^\"]*)\" page$")
   public void iAmOnSimilar_X_Page(String pageName) throws Throwable {
  // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
   }

but I am getting Undefined step reference. Could anyone help me with this please?


Answer:

you have to escape the opening and closing parenthesis \( and \) instead of double quotes, they are not present in your example data.

In the negating character class match not a closing parenthesis [^)]+

^I am on \(([^)]*)\) page$

In Java:

String regex = "^I am on \\(([^)]*)\\) page$";

Question:

I'm trying to use Regex to match the method to my step definiton. I want the step to be matched to any of the following and capture the number group.

  1. the fuel percent is 74
  2. the fuel percent should be 74
  3. fuel percent is 74

This is the regex expression i'm using @Then(".\bfuel\b.\bpercent\b.* (\d+)")

But it doesnt seem to match or capture the value. It says please implement the missing steps

@Then("the fuel percent should be {int}")
public void the_fuel_percent_should_be(Integer int1) {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

Answer:

Gherkin:

the fuel percent is '74'
the fuel percent should be '74' fuel
percent is '74'

Java:

@Then(".*fuel percent (is|should be) '(.*)'")
public void the_fuel_percent_should_be(String type, Integer int1) {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

Regexp online demo here.

Question:

I am unable to execute the below Cucumber scenario due to error in my step definition file for my when step

"User Selects <Origin>, <Destination>, <Date> and <MonthYear>".

Regular expression for Date and MonthYear have some problem. I have added my Cucumber Scenario and snippet of my step definition

Cucumber Feature:

Scenario Outline: User Searches One Way Flight
Given User is logged into the application
When User selects One way trip
And User Selects <Origin>, <Destination>, <Date> and <MonthYear>
Then Search results should be displayed.

Examples:

    | Origin        | Destination | Date | MonthYear      |
    | New York      | Sydney      | 20   | January 2016   |

Step Definition

@When("^User Selects \"([^\"]*)\", \"([^\"]*)\", \"(\\d+)\" and \"([\\d+]*)\"$")
public void user_selects(String origin, String destination, String date, String monthYear) {
    flight.searchOneWayFlight(origin, destination, date, monthYear);
}

Answer:

You need to use int for the date and the regular expression from monthYear will not pass the month part.

@Given("^User Selects (.*?), (.*?), (\\d+) and (.*?)$")
    public void userSelects(String origin, String destination, int date, String monthYear)

Question:

I'm a starter in Cucumber Selenium with Java and have Google'd, but couldn't find if it was possible. This is the issue. I have a method that has a String parameter that I would like to match to a variable name. Then I would like to use that variable for another method.

So in short I would have a parameter String (f.e. "name") And would like that to be matched to the variable 'name' and get/use that value

Something like this.

My cucumber scenario would have a step like this:

Abstract Scenario: check errors in fields
If there is an error in "field"
Then foo

Examples:
|field  |
|name   |
|address|
|phone  |

My method in the step definition would be something like:

private final name = "field_ID_Name"
private final address = "field_ID_Address"
private final phone = "field_ID_Phone"

public void thereIsAnErrorIn(String field){
// Here the match from the argument "name" to use of the variable "name"
checkError(field)
}

private void checkError(String field){
// here I'd like to check based on the value, so in my case for variable
//'name' would get "field_ID_name" as passed parameter
seleniumdriver.pollVisible(field) //field = "field_ID_Name"
}

Now (obviously) it passes on "name" as the string, but I couldn't figure out a way to pass on "field_ID_Name" for this example. I could use a switch statement to check for various possible values of field, but I rather have it match based on the string passed. Another option to put the exact locator as an argument for the first method, but that isn't that easy to read for my customer who has to understand the feature-files (cucumber scenario's).

Any suggestions appreciated!


Answer:

You can create a map with variable names as keys and the desired argument to be passed to pollVisible function as the value. So your map would look something like :

myMap : name -> field_ID_Name

Now do:

private void checkError(String field){

seleniumdriver.pollVisible(myMap.get(field));

}

Question:

I want to detect a list of Apps named AppA, AppB and AppC in my cucumber step.

i.e. "Given there are 2 badges in the repository owned by AppA,AppB"

I already tried the following and it works according to the IntelliJ colouring syntax :

@Given("^there are (\\d+) badges in the repository owned by (App[ABC](,App[ABC])*)$")

But at compilation the following error

However, the gherkin step has 3 arguments [2, AppA,AppB, ,AppB].

I think it is caused by the parenthesis within the parenthesis of the regex. Does anyone could share me a workaround so I get only [2, AppA,AppB] ?


Answer:

I solved the issue using the non-capturing groupe ?:

@Given("^there are (\\d+) badges in the repository owned by (App[ABC](?:,App[ABC])*)$")

It seems that cucumber takes everything that is in capturing groups, even if they are nested (even if some regex checker tools only detect 1 for the list)