Regular expressions in Feature file

In this section we will discuss on Importance of Regular expressions in Feature file. Regex is defined as sequence of character that which used to search a pattern. This can be used only if the String contains some specific patter to search. Regular expression is key feature of the cucumber flexibility.

The question should come to your mind that why Regex is required to run the cucumber script. Let’s discuss the below scenario.

Scenario:

There is two Test case for Login scenario in feature file.

Feature: Application Login

Scenario: Home page Login
Given User is on Net banking landing page
When User login into application with “Pratik” and password “1234” 
Then Home page is populated.
And Cards are displayed


Scenario: Home page Login
Given User is on Net banking landing page
When User login into application with “Pratik” and password “1567” 
Then Home page is populated.
And Cards are not displayed

To automate this, you may think that you have written 2 scenario each 5 lines which will be fine to run the Step definition file for Each scenario. If in future, there are 100 scenario came then writing separate step definition file will be the worst decision. If you will see deeply the feature file and step definition file, then you can see that the Feature and Given are same for both scenario and both are mapping to same step of the step definition file. There is no point to write the step definition file separately. There is already implementation file exist. 

But in when step, you need to user different test data to test the application. Though both contexts is same you have to try with different user name and password. In that case do you want to write a separate step definition file for this?

No. You can still use the common step definition file to test the login functionality with different test data.  Let’s discuss how to configure this scenario.

Basically one thing you need to make sure that the format should be same for each scenario but only parameter need to be changed.  See the step definition file for this when scenario below

When User login into application with “Pratik” and password “1234”

Regex expression:

@when(“^User login in to application with \”([^\”]*)\” and password \”([^\”]*)\”$”)
public void user_login_into_application_with_something_and password_something(String strArg1, String strArg2) throws Throwable{
}

When you put something in “ ” in feature file cucumber assume that this could be dynamic and this may be always change. Soo you need to put “ ” for user name and password and telling cucumber that this may change for every scenario. For this parameter you need to write Regex expression to accept the user name and password.

Let’s see another example below.

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   |
@Given("^User Selects (.*?), (.*?), (\\d+) and (.*?)$")
    public void userSelects(String origin, String destination, int date, String monthYear)

From the above scenario we have got the clear picture on Importance of Regular expressions in Feature file.

Leave a Reply

Your email address will not be published. Required fields are marked *