Scenario Outline

In the previous chapter we parameterized steps of scenario and instead of two step definition method we have created a common function with the matching pattern. This will work only if there is a single set of parameters to be passed and if we need to test with more data, we need to create more scenario. This will be a lengthy process. To solve such issue cucumber provide us with Scenario Outline.


The Scenario Outline in Cucumber is like a template for scenarios, allowing you to run the same scenario with different sets of data, making your tests more versatile and efficient.


Scenario Outline helps you run the same scenario multiple times with different values. It's great for testing similar situations with various inputs.


It includes an Examples table where you define different sets of data that should be used to fill in placeholders in the scenario.


Placeholder names (denoted with < >) in the scenario are replaced with actual values from the Examples table when the scenario runs.


Scenario Outline is particularly useful when you have a set of steps that remain the same but need to be tested with different inputs or conditions.

Let's understand by modifying example used in the previous chapter. The updated feature file will be as follows.


Gherkin
Copy
Feature: Login functionality
  Scenario Outline: Successful login
    Given the user is on the login page
    When the user enters username <username> and password <password>
    Then the user should be logged in successfully
    Examples:
      | username  | password  |
      | "root"    | "admin"   |
      | "admin"   | "admin"   |
    
  Scenario: Invalid login attempts
    Given the user is on the login page
    When the user enters username "root" and password "pass"
    Then the login should fail with an error message

In the above feature file example code we have updated When statement to include placeholder for username and password parameter and included list of possible successful username and password under example section. Here the first scenario will run for each row in the example table and will replace associated placeholder with the row cell value.


The step definition for the updated feature file remain the same as we don't have to make changes to existing functions. The step definition will look like the following.


Java
Copy
@Given("the user is on the login page")
public void navigateToLoginPage() {
	// Code to navigate to the login page
	System.out.println("User is on the login page");
}

@Then("the user should be logged in successfully")
public void verifySuccessfulLogin() {
	// Code to verify successful login
	System.out.println("User is logged in successfully");
}

@Then("the login should fail with an error message")
public void verifyLoginFailure() {
	// Code to verify login failure with an error message
	System.out.println("Login failed with an error message");
}

@When("the user enters username {string} and password {string}")
public void theUserEntersUsernameRootAndPasswordAdmin(String username, String password)
{
	System.out.println("The user enters username " + username + " and password " + password + ".");
}

Now run the login.feature file, you will see the first scenario will run for as much row we have in the example table (ie 2 times) and the second scenario will run 1 time.

User is on the login page
The user enters username root and password admin.
User is logged in successfully
User is on the login page
The user enters username admin and password admin.
User is logged in successfully
User is on the login page
The user enters username root and password pass.
Login failed with an error message

3 Scenarios (3 passed)
9 Steps (9 passed)
0m0.404s