Tuesday, February 17, 2015

Web Integration and BDD testing with Selenium, Specflow and MSTest

I occasionally participate in a workshop the LitheSpeed folks where we teach Test First Development using standard unit tests and Business Driven Development using Gherkin.  C# and Java developers participate in a mixed class. The first set of labs involves building a library first using developer style unit tests. The second set of labs involves building a library using BDD techniques.  The class wraps up with building a BDD style set of tests against an existing web site.

I've created a small shell project on GitHub that contains both a basic Selenium unit test and a basic Specflow driven BDD style Selenium test. This makes it easy to start testing without the overhead of creating a new VS solution.  I built this project in VS2013 which has good NuGet, Specflow and Selenium plugin and Nuget support.

The project uses NuGet Package Restore to pull down the necessary dependencies in an almost Maven like fashion. The Selenium Web Testing development team continues to improve their project integrating with NuGet so that you can run Selenium tests without PATH configuration or additional installation. The sample project includes references to the ChromeDriver and the IEDriver in case you want to run the same test in either browser.

Simple Selenium Google search

This class file creates a new test browser instance every time the class is called to run all the tests in it.  It tears down the connection after every test in this class has run. We could run one instance for all tests in all classes or run a new browser instance for each tests.  The "one instance per class" provides some level of isolation while providing decent performance.

This very simple test opens a Google search window, enters a search string, submits the search and then verifies the search string is in the window title.   It is a pretty simple test

...

...

Simple Specflow driven Selenium Google search

Specflow BDD tests consist of two parts. The first part is the feature stored in a .feature file that describes the scenario in business terms using the Gherkin syntax.

GoogleSeleniumSpecflowtest.feature in the test project has the following scenario.  Each line in the scenario maps to a step in the test functions. 
Scenario: Search for Something
    Given I want to search with Google
    When When I search for "freemansoft"
    Then That should be in the title bar

SpecFlow breaks the scenario down into a set of cooperating methods where each item in blue text becomes its own method. Scenarios can mix and match Given/When/Then clauses from other tests re-using the code that implements those pieces.

...

...

We can create the same scenario for searching with Bing.
Scenario: Search for Something
    Given I want to search with Bing
    When When I search for "freemansoft"
    Then That should be in the title bar
We reuse all the When/Then steps creating only a new Given method

...

...

Specflow Note

This example uses the Specflow Context to pass data between the different sections of a scenario.  This is the recommended mechanism for passing data between steps since it lets Specflow manage the lifetime of the data.

Conclusion

This  sample on GitHub is really meant to act as a starting place for writing your own tests in whichever fashion you chose. Hopefully you find it useful for jumpstarting your efforts at improving your product by writing good UI  and functional tests.

Created 2/17/2015

No comments:

Post a Comment