Buy Access to Course
09.

Scenario Outline

Share this awesome video!

|

Keep on Learning!

Add a second scenario to search: searching for a product that is not found. I'm lazy, so I'll copy the first scenario.

18 lines | features/search.feature
// ... lines 1 - 13
Scenario: Search for a word that does not exist
// ... lines 15 - 18

The Given is the same in both, so we can move that to a Background:

18 lines | features/search.feature
// ... lines 1 - 5
Background:
Given I am on "/"
// ... lines 8 - 18

It is ok not to have any Given's in the scenario and start directly with When. I'll change the search term to "XBox" - none of those in the dino store. This is really going to bum out any dino's that are looking to play Ghost Recon.

When there are no matches - the site says "No Products Found". Cool, let's plug that into our Then line:

18 lines | features/search.feature
// ... lines 1 - 13
Scenario: Search for a word that does not exist
When I fill in "searchTerm" with "XBox"
And I press "search_submit"
Then I should see "No products found"

Run this:

./vendor/bin/behat

We didn't use any new language, so everything runs and passes!

Scenario Outlines

To remove duplication, we can take things a step further with a Scenario Outline. It looks like this. Copy one of the scenarios and change it to start with Scenario Outline:.

The search term is different in the scenarios, so change it to <term>. For the "should see" part, replace that with <result>:

18 lines | features/search.feature
// ... lines 1 - 8
Scenario Outline: Search for a product
When I fill in "searchTerm" with "<term>"
And I press "search_submit"
Then I should see "<result>"
// ... lines 13 - 18

Now, we have two variable parts. To fill those in, add an Examples section at the end with a little table that has term and result columns. For the first row, use Samsung for the term and Samsung Galaxy for the result. The second row should be XBox and the No products found message:

18 lines | features/search.feature
Feature: Search
// ... lines 2 - 8
Scenario Outline: Search for a product
// ... lines 10 - 13
Examples:
| term | result |
| Samsung | Samsung Galaxy S II |
| XBox | No products found |

Now we remove both of the scenarios -- crazy right? You'll see this table format again later. The table doesn't need to be pretty, but I like to reformat it with cmd + option + L.

Time to try this out.

Perfect! It only prints out the Scenario Outline once. But below that, Behat prints both examples in green as it executes each of them. Senario Outlines are the best way to reduce duplication that goes past just the first Given line.