Skip to main content

Behat: Scenario Outline and Examples

The Scenario Outline keyword can be used to run the same Scenario multiple times, with different combinations of values.

Copying and pasting scenarios to use different values quickly becomes tedious and repetitive. Scenario outlines allow us to more concisely express these examples through the use of a template with placeholders.

So, If we want to repeat all the steps of a Scenario again and again for different sets of data, we implement Scenario Outline.

Consider an example where you have to test login functionality with valid and invalid username and check user is on right page after successful login or failure or you can assert any other thing.

#language: en
@login
Feature: Login Functionality

  Background:
    Given I am on homepage
    When I follow "Sign in"

  @temp
  Scenario Outline: Verify Login Functionality
    And I fill in "email" with "<email>"
    And I fill in "passwd" with "<password>"
    And I press "SubmitLogin"
    Then I should see "<heading>" heading

    Examples:
      | email                     | password  | heading        |
      | goswami.tarun77@gmail.com | Test@1234 | MY ACCOUNT     |
      | wrongusername             | test      | AUTHENTICATION |

In above example in conventional way you will create 2 scenario one for valid user and one for invalid user but with Scenario outline you can save lots of time, efforts and make your feature file easier to manage and maintain.

In example, First username is valid and second is invalid and column "heading" is heading of the page where user will be redirected after successful login or if he fails to login. With right username user will be on "My Account" page and wrong username user will be on "Authentication" page. We are asserting heading of the page in this example.

Scenario outline basically replaces variable/keywords with the value from the example table. Each row in the table is considered to be a scenario. The Scenario Outline uses placeholders, which are contained within < > in the Scenario Outline's steps. So in above example <email>, <password> and <heading> are placeholders. You can think placeholder as a variable which store value of example data.

You specify your Example heading inside angular bracket <> for steps. In above example you can observe heading match with text mention inside <> in steps.

A Scenario Outline is run once for each row in the Examples section beneath it. So above example consist of 2 scenarios and each step will be executed twice for different set of example data.

Video Tutorial

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.