Scenario Outline

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

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

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

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

Leave a comment!

This tutorial uses a very old version of Symfony. The fundamentals of Behat are still valid, but integration with Symfony will be different.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.4.0, <7.3.0",
        "symfony/symfony": "^2.7", // v2.7.4
        "twig/twig": "^1.22", // v1.22.1
        "sensio/framework-extra-bundle": "^3.0", // v3.0.16
        "doctrine/doctrine-bundle": "^1.5", // v1.5.1
        "doctrine/orm": "^2.5", // v2.5.1
        "doctrine/doctrine-fixtures-bundle": "^2.2", // v2.2.1
        "behat/symfony2-extension": "^2.0" // v2.0.0
    },
    "require-dev": {
        "behat/mink-extension": "^2.0", // v2.0.1
        "behat/mink-goutte-driver": "^1.1", // v1.1.0
        "behat/mink-selenium2-driver": "^1.2", // v1.2.0
        "phpunit/phpunit": "^4.8" // 4.8.18
    }
}