×
☰ See All Chapters

Cucumber Scenario Outline and Examples

  • Sometimes there will be multiple same scenarios with different input values. As an example: 

Feature: Login

  Scenario: Admin Login

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as "manu.m@tools4testing.com" and password as "*****"

    And I click on login button

    Then Login success window should be displayed

 

  Scenario: Developer Login

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as "advith@tools4testing.com" and password as "*****"

    And I click on login button

    Then Login success window should be displayed

 

  Scenario: Tester Login

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as "likitha@tools4testing.com" and password as "*****"

    And I click on login button

    Then Login success window should be displayed

  • When multiple scenarios are same, it’s hard to see the essence of each scenario, which is the checking the login credentials for different roles. We can use a scenario outline to specify the steps once and then play multiple sets of values through them. Here’s that scenario again, refactored to use a scenario outline: 

Feature: Login

  Scenario Outline: Login Check

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as <UserName> and password as <Password>

    And I click on login button

    Then Login success window should be displayed

    Examples:

      | UserName                    | Password |

      | manu.m@tools4testing.com    | *****    |

      | advith@tools4testing.com    | *****    |

      | likitha.m@tools4testing.com | *****    |

  • We can have placeholders within the scenario outline using angle brackets <placeholder> where we want real values to be substituted. The scenario outline itself is useless without a table of Examples, which lists rows of values to be substituted for each placeholder. 

  • You can have any number of placeholders. 

  • You can have any number of Scenario Outline elements in a feature and any number of Examples tables under each scenario outline.  

  • Syntax of Data Tables and Examples are same but they are completely different form one another. Data tables supply data to a single step. Examples table represents a whole scenario to be executed by Cucumber.  

  • If the just input data is different from scenarios we can use Scenario Outline and Examples. But what is a step is completely different between same scenarios? As an example: 

Feature: Login

 

  Scenario: Login Check

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as "manu.m@tools4testing.com" and password as "*****"

    And I click on login button

    Then Login success window should be displayed

 

  Scenario: Fake Login

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as "abcd@tools4testing.com" and password as "*****"

    And I click on login button

    Then Error Message should be displayed

  • In the above example between two scenarios only the last step is different, this case be handled with Outcome along with Scenario Outline and Examples.   

Feature: Login

 

  Scenario Outline: Login Check

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as <UserName> and password as <Password>

    And I click on login button

    Then <Outcome>

 

    Examples:

      | UserName                 | Password | Outcome                                  |

      | manu.m@tools4testing.com | *****    | Login success window should be displayed |

      | abcd@tools4testing.com   | *****    | Error Message should be displayed        |

 

  • Cucumber accepts any number of Examples elements to a Scenario Outline, you can group different kinds of examples together, if you want. For example: 

Feature: Login

 

  Scenario Outline: Login Check

    Given I open Chrome browser

    And I navigate to login page

    When I click on login option

    And I provide username as <UserName> and password as <Password>

    And I click on login button

    Then <Outcome>

    Examples: Successful login

      | UserName                 | Password | Outcome                                  |

      | manu.m@tools4testing.com | *****    | Login success window should be displayed |

      | advith@tools4testing.com | *****    | Login success window should be displayed |

 

    Examples: Failed login

      | UserName               | Password | Outcome                           |

      | xyz@tools4testing.com  | *****    | Error Message should be displayed |

      | abcd@tools4testing.com | *****    | Error Message should be displayed |


All Chapters
Author