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.
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:
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.
Hi,
I have this kind of error in console:
Indentation problem at line 6 (near " goutte: ~").
OR
A colon cannot be used in an unquoted mapping value at line 7 (near " selenium2: ~").
////// My behat.yml file is here..
default:
extensions:
Behat\MinkExtension:
# base_url: https://imc2-qa.csid.co/log...
base_url: https://imc2-qa.csid.co/log...
goutte: ~
selenium2: ~
suites:
default:
contexts:
- FeatureContext
- Behat\MinkExtension\Context\MinkContext