The SpecFlow Cook Book

Drivers

Use drivers

The Problem

Your step definitions are becoming complex and common test functionality is being replicated across step definition files. Additionally information about how to interact with the subject under test is starting to bleed into your scenarios.

The Ingredients

  1. A component to provide centralised application logic
  2. Use of ScenarioContext to share components and data between steps
  3. Hooks

The Solution

I’m going on holiday to Europe this year so I’ll use a holiday analogy for this recipe.

A key objective of your SpecFlow feature files is to capture conversations and examples of how your system should behave. However, it is often seen as an anti-pattern to start to write your scenarios in a “do this, do that” form that shows how the system should be used, rather than expressing it’s intentional behaviour in an un-ambiguous manner.

In the following example, I’ve used a simple holiday search web site to help me to choose a destination for my next holiday.


Feature: Holiday Destination Search
	As a potential holiday maker
	I want to search for weather conditions at various locations
	So that I can select a holiday destination

Background:
	Given the following locations
	| Location | Country | Average Temperature |
	| Madrid   | Spain   | 30                  |
	| Marbella | Spain   | 28                  |
	| Nice     | France  | 32                  |

Scenario: Search holiday destinations by country
	When I search for holiday destinations in "Spain"
	Then the list of locations found should contain the following:
	| Location | Country | Average Temperature |
	| Madrid   | Spain   | 30                  |
	| Marbella | Spain   | 28                  |

Assuming I’ve built the holiday search web site and through the Background step have managed to seed the system with data, the key step in the scenario is to perform a search for holiday destinations in Spain.

The


namespace TheSpecflowCookbook.UseDrivers.AcceptanceTests.Steps
{
    [Binding]
    public class SearchSteps
    {
        [Given(@"the following locations")]
        public void GivenTheFollowingLocations(IEnumerable holidayDestinations)
        {
            ScenarioContext.Current.Get().AddHolidayDestinations(holidayDestinations);
        }

        [When(@"I search for holiday destinations in ""(.*)""")]
        public void WhenISearchForHolidayDestinationsIn(string term)
        {
            var driver = ScenarioContext.Current.Get();

            var results = driver.SearchByCountry(term);

            ScenarioContext.Current.Set>(results);
        }

        [Then(@"the list of locations found should contain the following:")]
        public void ThenTheListOfLocationsFoundShouldContainTheFollowing(IEnumerable expectedDestinations)
        {
            var actualDestinations = ScenarioContext.Current.Get>();

            CollectionAssert.AreEquivalent(expectedDestinations, actualDestinations);
        }
    }
}

Any there we have it – Ole !

close
Drivers

Use drivers

Leave a Reply

Paul Marshall

I started Wavecrest Software in 1999 and have achieved success in a range of UK blue chip companies as well as small and medium sized businesses. Proven track record in software development from an extensive hands-on technical lead / senior development background using .NET technologies and Agile methodologies.
Drivers

Use drivers December 16, 2013


Contact


Search