5. Scenario Outlines & Examples

Estimated reading: 3 minutes 21 views

Scenario Outlines in Cucumber are a great way to write data-driven tests. Instead of duplicating the same scenario multiple times with different data, you can define a single scenario with placeholders and provide the data in an Examples Table. This allows Cucumber to run the same scenario with different sets of inputs.

What is a Scenario Outline?

A Scenario Outline is used to run the same scenario multiple times with different inputs. Instead of writing multiple scenarios with identical steps, you define a template with placeholders (e.g., <username>, <password>) in the scenario steps. The Examples Table then fills these placeholders with different sets of data.

Syntax of a Scenario Outline

				
					Scenario Outline: <Scenario Description>
  Given <Step with placeholders>
  When <Step with placeholders>
  Then <Step with placeholders>

  Examples:
    | <Column1> | <Column2> |
    | <Value1>  | <Value2>  |
    | <Value3>  | <Value4>  |

				
			
  • The placeholders <Column1>, <Column2>, etc., are replaced by the values in the Examples table.
  • Each row in the Examples table runs a new instance of the scenario with the data from that row.

Example: Login Scenario Outline

Here’s a simple example that tests the login functionality with different sets of credentials.

Feature File

				
					Feature: Login functionality

  Scenario Outline: User logs in with different credentials
    Given the user is on the login page
    When the user enters "<username>" and "<password>"
    Then the user should be redirected to the dashboard

    Examples:
      | username     | password     |
      | user1        | pass1        |
      | user2        | pass2        |
      | invalidUser  | wrongPass    |

				
			
  • Given: The user is on the login page.
  • When: The user enters a username and password.
  • Then: The user should be redirected to the dashboard page.

The placeholders <username> and <password> in the scenario steps will be replaced by the values in the Examples table. This allows you to test multiple credentials without repeating the same scenario.

Step Definitions (Java)

				
					@Given("^the user is on the login page$")
public void userIsOnLoginPage() {
    // Simulate opening the login page
    System.out.println("Opening login page...");
}

@When("^the user enters \"([^\"]*)\" and \"([^\"]*)\"$")
public void userEntersCredentials(String username, String password) {
    // Simulate entering the credentials
    System.out.println("Entering username: " + username + " and password: " + password);
}

@Then("^the user should be redirected to the dashboard$")
public void userIsRedirectedToDashboard() {
    // Simulate checking if the user is redirected to the dashboard
    System.out.println("User is redirected to the dashboard.");
}

				
			
  • The userEntersCredentials step definition takes two parameters: username and password, which are populated from the Examples table.
  • The scenario will run three times with the following inputs:
    • user1 and pass1
    • user2 and pass2
    • invalidUser and wrongPass

Benefits of Scenario Outlines

  1. Data-Driven Testing: Allows you to run the same test with different sets of data without duplicating the scenario.
  2. Cleaner Feature Files: Instead of writing multiple similar scenarios, you can use a single template with an Examples table.
  3. Efficiency: It’s easy to scale tests with many data combinations by simply adding rows to the Examples table.

Conclusion

Scenario Outlines combined with Examples Tables make it easy to test your application with multiple sets of data, ensuring better coverage and avoiding repetitive code. This method keeps your feature files clean and maintainable while providing flexibility to handle various inputs and test cases efficiently.

Leave a Comment

Share this Doc

5. Scenario Outlines & Examples

Or copy link

CONTENTS