Scenario Outline and Data Tables

Scenario Outline

A Scenario Outline runs the same steps once per row of its Examples table: Cucumber's version of a DataProvider.

Example

search.feature

Scenario Outline: Search returns products
  Given the user is on the home page
  When the user searches for "<term>"
  Then at least <count> products are shown

  Examples:
    | term       | count |
    | headphones | 5     |
    | laptop     | 3     |
    | saree      | 10    |

Example output

Scenario Outline: Search returns products   # features/search.feature:3
...
3 Scenarios (3 passed)
9 Steps (9 passed)
0m21.418s

Data Tables

A data table passes a small table to one step. Cucumber converts it to List<Map<String, String>>:

Scenario: Checkout with several items
  Given the cart contains
    | product          | qty |
    | Boat Rockerz 450 | 1   |
    | USB-C Cable      | 2   |
  When the user checks out
  Then the order total is "₹1,797.00"
@Given("the cart contains")
public void cartContains(DataTable table) {
    for (Map<String, String> row : table.asMaps()) {
        productPage.open(row.get("product")).addToCart(Integer.parseInt(row.get("qty")));
    }
}

Practice

Exercise: Write a Scenario Outline for registration with three invalid emails (abc, abc@, @test.in) that all show "Enter a valid email".

Show answer
Scenario Outline: Invalid email is rejected
  Given the user is on the registration page
  When the user registers with email "<email>"
  Then the error "Enter a valid email" is shown

  Examples:
    | email    |
    | abc      |
    | abc@     |
    | @test.in |