Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

How To Use Regular Expressions In The Cucumber Framework?

Category: BDD Cucumber

We can use regular expressions in the Cucumber framework inside the step definition file. The regular expression can be used to combine two or more similar steps in the feature file within a test method in the step definition file. Also, the regular expression is used for data parameterization in Cucumber as the Feature New User Registration in the feature file described previously.

Feature file implementation.

  • Feature: Student Study Schedule
  • Description: Verify student study schedule
  • Scenario: Verify hours spent on science subjects
  • Given Student studies Chemistry on Tuesday
  • Given Student studies Mathematics on Tuesday
  • Given Student has thirty minutes recess
import cucumber.api.java.en.Given;
public class StudentSchedule {
 
@Given ("^Student studies ([^\"]*) on Tuesday$")
public void student_Tuesday_schedule (String subject) {
       if (subject.equals ("Chemistry")){
              System.out.println ("Student studies Chemistry on Tuesday");
        } else {
               System.out.println ("Student studies Mathematics on Tuesday");
         }
    }
@Given ("^Student has thirty minutes recess$")
public void student_recess () {
      System.out.println ("Student recess time is thirty minutes");
    }
}

Leave a Reply

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