What is Parameterization in Cucumber?
Parameterization in Cucumber allows you to pass different input data into your Gherkin steps (feature files), enabling reusability of steps and data-driven testing. Instead of writing separate scenarios for each data input, you can parameterize the steps using:
Regular Expressions (Step Definition Parameters)
Scenario Outline and Examples Table
1. Parameterization using Step Definition Parameters
You can define steps with placeholders using double quotes (") and capture them in the step definitions with regex or Cucumber expressions.
Feature File (Login.feature)
Feature: Login Feature Scenario: Login with valid credentials Given User logs in with username "john" and password "password123"
Step Definition in Java
import io.cucumber.java.en.Given; public class LoginSteps { @Given("User logs in with username {string} and password {string}") public void user_logs_in_with_credentials(String username, String password) { System.out.println("Username: " + username); System.out.println("Password: " + password); // Add login logic here } }
2. Parameterization using Scenario Outline and Examples Table
This is useful when you want to run the same scenario multiple times with different inputs.
Feature File with Scenario Outline (Login.feature)
Feature: Login Feature Scenario Outline: Login with multiple credentials Given User logs in with username "<username>" and password "<password>" Examples: | username | password | | john | password123 | | alice | qwerty456 | | bob | 12345 |
Step Definition in Java
import io.cucumber.java.en.Given; public class LoginSteps { @Given("User logs in with username {string} and password {string}") public void user_logs_in_with_credentials(String username, String password) { System.out.println("Username: " + username); System.out.println("Password: " + password); // Add logic to perform login here } }
Important Points
| Feature | Benefit |
|---|---|
| Step Parameterization | Reusable steps with dynamic data |
| Scenario Outline + Examples | Data-driven testing made easy |
| Reduces code duplication | Less maintenance, more clarity |