Cucumber - Parameterization
Cucumber - Parameterization
Parameterization in Cucumber allows you to make your scenarios more dynamic and reusable by passing values directly into step definitions. This reduces redundancy and enhances maintainability, making it easier to test multiple input values or configurations without duplicating steps.
What Is Parameterization?
Parameterization involves using placeholders in Gherkin steps to accept dynamic values. These placeholders are defined within double quotes ("value"
) in the feature file. The corresponding step definition uses parameters to capture these values and execute logic based on them.
Example: Parameterization with Cucumber
Below is an example demonstrating how to test a login functionality with parameterized values for username and password.
Feature File (LoginFeature.feature)
Feature: User Login
Test the login functionality with parameterized inputs.
Scenario: Login with valid credentials
Given the user enters username "testuser" and password "password123"
When the user clicks the login button
Then the user should see a welcome message
Scenario: Login with invalid credentials
Given the user enters username "invaliduser" and password "wrongpassword"
When the user clicks the login button
Then the user should see an error message
Step Definitions File (LoginSteps.java)
package com.example.tests;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
public class LoginSteps {
private String username;
private String password;
@Given("the user enters username {string} and password {string}")
public void enterCredentials(String username, String password) {
this.username = username;
this.password = password;
System.out.println("Username: " + username);
System.out.println("Password: " + password);
// Logic to input credentials in the application
}
@When("the user clicks the login button")
public void clickLoginButton() {
System.out.println("Clicking the login button...");
// Logic to simulate clicking the login button
}
@Then("the user should see a welcome message")
public void verifyWelcomeMessage() {
System.out.println("Verifying the welcome message for user: " + username);
// Logic to verify the welcome message
}
@Then("the user should see an error message")
public void verifyErrorMessage() {
System.out.println("Verifying the error message for user: " + username);
// Logic to verify the error message
}
}
Test Runner File (TestRunner.java)
package com.example.tests;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(
features = "src/test/resources/features",
glue = "com.example.tests"
)
public class TestRunner extends AbstractTestNGCucumberTests {
// No additional code required; Cucumber handles execution
}
Real-World Use Cases
- Form Testing: Test different input combinations in forms, such as email, password, and phone numbers.
- API Testing: Pass dynamic API endpoints, headers, and body parameters to test various scenarios.
- E-commerce: Parameterize product names, quantities, and prices to test cart functionality.
- Authentication Testing: Test different login credentials and security mechanisms with dynamic inputs.
Advanced Parameterization: Regular Expressions
Cucumber also supports parameterization using regular expressions, allowing you to capture dynamic values from step definitions without using quotes. Here’s an example:
Feature File
Scenario: Search for a product
Given the user searches for product ID 12345
Then the user should see details for product ID 12345
Step Definitions File
package com.example.tests;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
public class ProductSearchSteps {
@Given("the user searches for product ID {int}")
public void searchForProduct(int productId) {
System.out.println("Searching for product ID: " + productId);
// Logic to search for the product
}
@Then("the user should see details for product ID {int}")
public void verifyProductDetails(int productId) {
System.out.println("Verifying details for product ID: " + productId);
// Logic to verify product details
}
}
Benefits of Parameterization
- Eliminates redundancy by reusing steps with dynamic inputs.
- Improves readability and clarity in test scenarios.
- Supports a wide range of data types, including strings, integers, and more.
- Facilitates testing with various combinations of inputs or configurations.