Selenium and Cucumber

How to handle Regular Expressions in Selenium and Cucumber with Java

Regular expressions in cucumber

Introduction

A Regular Expression is a pattern that describes a particular amount of text and is passed into the step definition file. The cucumber framework supports two types of expressions, i.e., Regular Expression and Cucumber Expression. Regular expressions in Cucumber can have alphanumeric characters, numeric digits, string digits, etc. which are supported by cucumber expression as well. With the help of regular expressions, we can select a set of similar statements in the feature file, and we can parameterize texts that can be used repeatedly.  

In this blog, we will discuss- 

  • Project Setup for Regular Expressions in Cucumber 
  • How to use Reg Expressions  

Project setup for regular expressions in Cucumber 

Project setup for regular expressions
  • Select Cucumber JUnit>click on the latest version>copy the dependency 
Junit
  • > add it to the pom.xml file 
  • Similarly, we need to search for Cucumber JVM:JUnit(io.cucumber)> Select Cucumber JVM:JUnit(io. cucumber) >click on the latest version>copy the dependency 
Regular expressions in Cucumber
  • > add it to the pom.xml file 
  • Below is an image of the pom.xml file after all the dependencies have been added 
pom.xml file

How to use Regular Expressions

In order to use regular expressions, we first need to create a feature file 

To create a new feature file, right-click on the folder in ‘src/test/resources’> click on New> go to File>provider File name with ‘.feature’ extension> click on Finish 

Create new file

In the below image, a feature file with the name ‘RegEx’ has been created. 

Regular expressions in cucumber

Next, we need to create the features, scenarios for the features, and the steps to follow for the specific feature as per the scenario. 

@Registration 
Feature: Registration feature 
@Filling 
Scenario: Filling the registration form 
   Given I want to fillup the registration form 
   And get admission in the institution 
   And the duration to fill the form is from 1stSept2022 to 5thSept2022 
   When I complete filling the form 
   And submit it on 5thSept2022 
   Then I validate if the registration process has been done succesfully 
Scenarios for the features for regular expressions in cucumber

In the above example, a feature file has been created. It contains a Feature ‘Registration feature’ as the Title of the feature under the @Registration tag. Also, the Scenario for this feature is to validate the registration functionality under the @Filling tag followed by the Steps given by using Given, When, Then, And. This will instruct the process for testing the particular feature

After the creation of a feature file, we need to create a step definition file  

To add the Step Definitions/Glue Code> right click on src/test/ java package> go to New> select Folder>Provide the name of the folder>click on Finish 

New folder java

In the above image, a folder with the name ‘StepDefinitions’ has been created  

After creating the Steps folder right click on it> go to New> select class> provide the name of the class>click on Finish 

Step definitions

In the above image, a step definition file with the class name as ‘RegistrationForm’ has been created. 

Next, we need to provide the functions for ‘Given, When, And, and Then’ to the class created in the Step Definitions folder.

package StepDefinitions; 
import io.cucumber.java.en.And; 
import io.cucumber.java.en.Given; 
import io.cucumber.java.en.Then; 
import io.cucumber.java.en.When; 
public class RegistratonForm { 
   @Given("I want to fillup the registration form") 
   public void task() { 
        System.out.println("Visit the registration form page"); 
} 
   @And("get admission in the institution")
   public void purpose() { 
        System.out.println("Apply for an admission in the institute"); 
} 
    @And("the duration to fill the form is from 1stSept2022 to 10thSept2022") 
    public void estimates() { 
        System.out.println("Need to fill the form anytime between 1st sept 2022 to 5th sept 2022"); 
    } 
    @When("I complete filling the form") 
    public void fill() { 
        System.out.println("Form fillup has been completed"); 
    } 
    @And("submit it on 5thSept2022") 
    public void submission() { 
        System.out.println("Form has been submitted"); 
    } 
    @Then("I validate if the registration process has been succesfully completed") 
    public void validations() { 
        System.out.println("Registration has been done successfully"); 
} 

} 
Code for regular expressions in cucumber

The above code will provide a backend code for the steps that we have written in the feature file. So the processor will know the exact code to be executed through the step definition for each step that we have created in the feature file. 

Next, we need to use regular expressions in our step definitions class file 

Note: Regular expressions should start with ‘^’symbol and end with ‘$’ symbol in step definitions.

package StepDefinitions; 
import io.cucumber.java.en.And; 
import io.cucumber.java.en.Given; 
import io.cucumber.java.en.Then; 
import io.cucumber.java.en.When; 
public class RegistratonForm { 
     @Given("^I want to fillup the registration form$") 
     public void task() { 
           System.out.println("Visit the registration form page"); 
} 
     @And("^get admission in the institution$") 
     public void purpose() { 
           System.out.println("Need to get an admission in the institute"); 
} 
      @And("^the duration to fill the form is from 1stSept2022 to 5thSept2022$") 
      public void estimates() { 
            System.out.println("Need to fill the form anytime between 1stSept2022 to 5thSept2022"); 
    } 
      @When("^I complete filling the form$") 
      public void fill() { 
             System.out.println("Form fillup has been completed"); 
    }  
       @And("^submit it on 5thSept2022$") 
       public void submission() { 
              System.out.println("Form has been submitted"); 
    } 
        @Then("^I validate if the registration process has been done succesfully$")
        public void validations() { 
               System.out.println("Registration has been done successfully"); 
} 

} 
Code for regular expressions in cucumber

Use the expressions below in the step definitions class file:

  • [a-zA-Z]{1,}  – any number of alphabets 
package StepDefinitions; 
import io.cucumber.java.en.And; 
import io.cucumber.java.en.Given; 
import io.cucumber.java.en.Then; 
import io.cucumber.java.en.When; 
public class RegistratonForm { 
      @Given("^I want to fillup the ([a-zA-Z]{1,}) form$") 
      public void task(String application) { 
            System.out.println("Visit the" +application+ "form page"); 
} 
       @And("^get admission in the institution$") 
       public void purpose() { 
             System.out.println("Need to get an admission in the institute"); 
} 
        @And("^the duration to fill the form is from 1stSept2022 to 5thSept2022$")
        public void estimates() {
             System.out.println("Need to fill the form anytime between 1stSept2022 to 5th sept 2022"); 
    } 
        @When("^I complete filling the form$") 
        public void fill() { 
              System.out.println("Form fillup has been completed"); 
    } 
         @And("^submit it on 5thSept2022$") 
         public void submission() { 
              System.out.println("Form has been submitted"); 
    } 
          @Then("^I validate if the ([a-zA-Z]{1,}) process has been done succesfully$") 
          public void validations(String application) { 
               System.out.println(application +"has been done successfully"); 
} 
} 
Code for regular expressions in cucumber

Code explanation:

In the above code, we have used a regular expression [a-zA-Z]{1,}. This expression is applicable only to alphabets. This means that the expression should be replaced with words that don’t contain numbers, special characters, etc. other than alphabets. So here in the given example, we have replaced the word registration with regular expression [a-zA-Z]{1,}. Then passed in a parameter ‘String application’ to the public void task() function and public void validations () function. 

  • [a-zA-Z0-9]{1,} – alphanumeric 
package StepDefinitions;  
import io.cucumber.java.en.And;  
import io.cucumber.java.en.Given;  
import io.cucumber.java.en.Then;  
import io.cucumber.java.en.When;
public class RegistratonForm {  
     @Given("^I want to fillup the ([a-zA-Z]{1,}) form$")
     public void task(String application) {  
           System.out.println("Visit the" +application+ "form page");  
}  
       @And("^get admission in the institution$") 
       public void purpose() {  
             System.out.println("Need to get an admission in the institute");  
}  
        @And("^the duration to fill the form is from 1st sept 2022 to 5th sept 2022$") 
        public void estimates() { 
              System.out.println("Need to fill the form anytime between 1st sept 2022 to 5th sept 2022");  
    }  
        @When("^I complete filling the form$") 
        public void fill() { 
              System.out.println("Form fillup has been completed");  
    }  
         @And("^submit it on ([a-zA-Z0-9]{1,})$")  
         public void submission(String submitDate) {  
               System.out.println("Form has been submitted on"+ submitDate);  
    }   
          @Then("^I validate if the ([a-zA-Z]{1,}) process has been done succesfully$")
          public void validations(String application) {  
                System.out.println(application +"has been done successfully");  
}  
}         
Code for regular expressions in cucumber

Code explanation: 

In the above code, we have used a regular expression [a-zA-Z0-9]{1,} this expression is applicable only to alphanumeric characters. So here in the given example, we have replaced 5th Sept 2022 with regular expression [a-zA-Z0-9]{1,}. Then passed in a parameter ‘String submitDate’ to the public void submission () function. 

  • [^\”]* – anything 
package StepDefinitions;  
import io.cucumber.java.en.And;  
import io.cucumber.java.en.Given;  
import io.cucumber.java.en.Then;  
import io.cucumber.java.en.When;
public class RegistratonForm {  
      @Given("^I want to fillup the ([a-zA-Z]{1,}) form$")  
      public void task(String application) {  
           System.out.println("Visit the" +application+ "form page");  
}  
      @And("^get admission in the institution$")
      public void purpose() {  
           System.out.println("Need to get an admission in the institute");  
}  
      @And("^the duration to fill the form is from ([^\”]*) to ([^\”]*)$")  
      public void estimates(String startDate,String endDate) {
           System.out.println("Need to fill the form anytime between "+startDate+" to "+endDate);  
    }  
      @When("^I complete filling the form$") 
      public void fill() { 
           System.out.println("Form fillup has been completed");  
    }  
      @And("^submit it on ([a-zA-Z0-9]{1,})$")  
      public void submission(String submitDate) {   
           System.out.println("Form has been submitted on"+ submitDate);  
    } 
       @Then("^I validate if the ([a-zA-Z]{1,}) process has been done succesfully$") 
       public void validations(String application) {  
            System.out.println(application +"has been done successfully");  
}  
}          
Code for regular expressions in cucumber

Code explanation: 

In the above code, we have used a regular expression [^\”]*. This expression is applicable to all types of texts i.e alphabets, numbers, alphanumeric characters, etc. Here in the given example, we have replaced 1st Sept 2022 and 5th Sept 2022 with the regular expression [^\”]*. Then passed in a parameter ‘String startDate’ and ‘String endDate’ to the public void estimates () function. 

Execution and results:

To Execute the code, we need to create a runner class in the Step Definitions folder. 

To create a runner class, we need to right-click on the Step Definitions folder>go to new> select class> provide the name of the class>click on Finish 

Executions

After Creating the test runner class file, we need to add the cucumber annotation and options. We also need to provide the location of the feature files and step definitions as shown in the below image: 

cucumber  annotations

To run the test runner class file, we need to right-click on the class file> select Run As> select JUnit Test. 

After executing the test runner class, we should get JUnit results as passed as shown in below image:

Results