Cucumber Scenario Outline Tutorial

Reading Time 4 minutes

Passing parameters in Cucumber is a powerful feature but we can extend this by utilizing ‘Cucumber Scenario Outline’.

Cucumber Scenario Outline

When writing Scenarios in Feature files it is always ideal to write specific Scenarios as to ensure that they make sense and bring value. For instance if we take the following example:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Then we can see that the Scenario is very specific. If you want to write more Scenarios around checking ‘page load’ for other pages then you should have a Scenario for each page:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Scenario: To check that the Java Cucumber tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java Cucumber Tutorial" page
Then the page title should be visible

Scenario: To check that the Git tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Git Tutorial" page
Then the page title should be visible

Looking at the above Feature file you can easily see that the Feature file contains three Scenarios. Although there are multiple Scenarios we also take advantage of passing in parameters as to reduce the number of step definition methods that we need to write. If you look through all three Scenarios then you should be able to see that the test steps between all three Scenarios are very similar, the only difference being the parameter. With this in mind it does feel like we are repeating ourselves a lot here since the Scenarios are pretty much the same. For instances such as this were the Scenarios are very similar with the exception being the parameters that are passed in, we can use Cucumber Scenario Outline to pass in multiple parameters into a single Scenario.

Using Cucumber Scenario Outline

Cucumber Scenario Outline look very similar to Scenarios but the only different is that the parameter which we pass in don’t sit in the Scenario itself but instead in an ‘Examples’ table directly below the Scenario. If we convert the above three Scenarios to instead use a Scenario Outline then this is what it would look like:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario Outline: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "<page>" page
Then the page title should be visible
Examples:
|page|
|Java WebDriver Tutorial|
|Java Cucumber Tutorial|
|Git Tutorial|

So what is happening in the Feature file above? There are a number of differences in the Scenario Outline Feature file above so lets go through them. If you notice instead of passing in an actual value in the ‘When’ step we are now passing in a reference to a ‘page’ parameter. The value of the ‘page’ is obtained from the ‘Examples’ table. In the previous Feature file we had three Scenarios but this has been condensed into a single Scenario Outline in this Feature file. Cucumber Scenario Outline works slightly differently as they run multiple times as opposed to Scenarios which run only the one time. When looking at the ‘Examples’ table the first row is used to identify the name of the parameter reference which we will use in the Scenario Outline. This means for each row other than the first row in an Examples table a Scenario Outline will run itself. This means since there are three data rows in the ‘Examples’ table the Scenario Outline will run three times where each time it run’s it will substitute the value of ‘page’ from the ‘Examples’ table.

Cucumber Scenario Outline really help to slim down having to write similar Scenarios and also help us to run the same Scenario Outline using multiple parameters. This is a great way of reducing Scenario duplication and also a way of managing better Feature files.

And that’s it for this tutorial folks.

Video Tutorial on YouTube

cucumber scenario outline

Direct Video Link

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.