Cucumber - Regular Expressions

Cucumber: Regular Expressions Explained

Regular Expressions (RegEx) are a powerful tool in Cucumber for matching steps in feature files with their corresponding step definitions in code. They allow you to create flexible and dynamic patterns to handle various inputs, making test automation more efficient.

What are Regular Expressions in Cucumber?

In Cucumber, Regular Expressions are used within step definitions to match test steps written in Gherkin syntax. They enable parameterization by capturing dynamic values from feature steps and passing them to the underlying code.

Basic Syntax:

  • (\\d+): Matches one or more digits (e.g., 42).
  • (\\w+): Matches one or more word characters (e.g., "apple").
  • "([^"]*)": Matches any text within double quotes (e.g., "hello world").

Example:


                  Feature: Calculator Operations
              
                    Scenario: Add two numbers
                      Given I have entered 50 into the calculator
                      And I have entered 20 into the calculator
                      When I press add
                      Then the result should be 70
                

The step definition for Given I have entered 50 into the calculator could look like this:


                  @Given("I have entered (\\d+) into the calculator")
                  public void enterNumberIntoCalculator(int number) {
                      calculator.enterNumber(number);
                  }
                

Advanced Features

Capturing Multiple Parameters:

Regular Expressions can capture multiple dynamic values from a single step.

Example:

                  Given I add 50 and 20
                

The corresponding step definition:


                  @Given("I add (\\d+) and (\\d+)")
                  public void addTwoNumbers(int num1, int num2) {
                      result = calculator.add(num1, num2);
                  }
                

Optional Groups:

Use parentheses with a ? to define optional parts of a pattern.

Example:

                  @Given("I (?:log in|sign in) as an admin")
                  public void logInAsAdmin() {
                      user.logInAs("admin");
                  }
                

Real-World Scenario:

Imagine an e-commerce platform where you want to validate dynamic pricing based on user input. Regular Expressions can help match varying steps for price calculations.

Feature File:

                  Scenario: Verify product price
                    Given the price of "Laptop" is 1200
                    Then the discounted price should be 1000
                
Step Definition:

                  @Given("the price of \"([^\"]*)\" is (\\d+)")
                  public void setProductPrice(String product, int price) {
                      productCatalog.setPrice(product, price);
                  }
              
                  @Then("the discounted price should be (\\d+)")
                  public void verifyDiscountedPrice(int expectedPrice) {
                      assertEquals(expectedPrice, calculator.getDiscountedPrice());
                  }
                

Benefits of Using Regular Expressions

  • Provides precise control over step matching.
  • Enables dynamic parameterization for versatile test cases.
  • Reduces duplication by consolidating similar steps into one definition.
  • Handles complex patterns and data extraction seamlessly.