What is Scenario Outline in Cucumber?
A Scenario Outline in Cucumber is used when you want to run the same scenario multiple times with different sets of data. This is extremely useful for data-driven testing.
It avoids repeating the same scenario logic and makes the feature file clean, readable, and maintainable.
Why Use Scenario Outline?
To avoid writing multiple similar scenarios.
To test the same logic with different data inputs.
To improve test coverage.
How It Works
- Scenario Outline is written like a normal scenario.
- Examples keyword is used to provide the input data in a tabular format.
- Each row in the Examples table is treated as a new execution of the scenario.
- The placeholders (example:
<username>,<password>) are replaced with values from the Examples table during execution.
Syntax of Scenario Outline
Scenario Outline: Scenario title Given some step with <parameter1> When action with <parameter2> Then expected result Examples: | parameter1 | parameter2 | | value1 | value2 | | value3 | value4 |
Example: Login Functionality
Feature File:
login.feature
Feature: Login Feature Scenario Outline: Valid login with different users Given User is on Login page When User enters username "<username>" and password "<password>" Then Login should be successful Examples: | username | password | | user1 | pass123 | | user2 | pass456 | | admin | admin@123 |
Step Definition File (Java)
package stepdefinitions; import io.cucumber.java.en.*; public class LoginSteps { @Given("User is on Login page") public void user_is_on_login_page() { System.out.println("User is on Login page"); } @When("User enters username {string} and password {string}") public void user_enters_credentials(String username, String password) { System.out.println("Username: " + username); System.out.println("Password: " + password); } @Then("Login should be successful") public void login_should_be_successful() { System.out.println("Login successful!"); } }
How It Runs
Cucumber will run the scenario 3 times, once for each row in the Examples table:
- user1 / pass123
- user2 / pass456
- admin / admin@123
Benefits of Scenario Outline
Reusability of test steps.
Better test coverage for multiple inputs.
Easy to maintain and extend.
No comments:
Post a Comment