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

... 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:

... lines 1 - 5
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:

... 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:


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>:

... 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:

... lines 1 - 13
| 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.

Leave a comment!

  • 2019-01-10 Diego Aguiar

    Hey Murad

    Hmm, the identation get lost in comments. Double check your identation first and if that's not the case, try removing the "selenium2" line, anyways, you are defining a default value, I believe it will behave as the same.


  • 2019-01-10 Murad

    I have this kind of error in console:

    Indentation problem at line 6 (near " goutte: ~").
    A colon cannot be used in an unquoted mapping value at line 7 (near " selenium2: ~").
    ////// My behat.yml file is here..

    # base_url:
    goutte: ~
    selenium2: ~

    - FeatureContext
    - Behat\MinkExtension\Context\MinkContext