Pages

Friday, July 01, 2011

SpecFlow and Selenium - Quick Start

Here are brief steps on how to get started with SpecFlow and Selenium. This scenario assumes an ASP.NET MVC web application as the "code" and NUnit as the "test project".

Web application

  1. Create new MVC web application

Test project

  1. Create a code library project. 
  2. Add reference to NUnit framework.

Selenium

  1. Download Selenium IDE for Firefox. This component is used for generating tests by scripting user behavior.
  2. Download Selenium DotNet package. Add reference to this package into the Test project.
  3. Run Selenium IDE in Firefox and script your test with the web application. 
  4. Export the generated test into a C# file.
  5. Add this file to the Test project.
  6. Download Selenium Server and run with something like

java -jar selenium-server-standalone-2.0rc3.jar

This will start Selenium server on port 4444 by default. You can configure which browser you want the Selenium to start but by default this is Firefox.

SpecFlow

  • Download and install SpecFlow. It will add new Items to the File->New menu in Visual Studio. The most important one is SpecFlow Feature item.
  • If you install SpecFlow NuGet package, it will add the minimum required settings to the app.config in the Test project. Apparently this is not required if the test runner is NUnit. Contents of the app.config look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
  </configSections>
  <specFlow>
    <!-- For additional details on SpecFlow configuration options see https://github.com/techtalk/SpecFlow/wiki/Configuration -->
    <language feature="en-US" tool="" />

    <unitTestProvider name="NUnit" />

    <generator allowDebugGeneratedFiles="false" />

    <runtime detectAmbiguousMatches="true"
             stopAtFirstError="false"
             missingOrPendingStepsOutcome="Inconclusive" />

    <trace traceSuccessfulSteps="true"
           traceTimings="false"
           minTracedDuration="0:0:0.1"
           listener="TechTalk.SpecFlow.Tracing.DefaultListener,  
                   TechTalk.SpecFlow" />
    
  </specFlow>
</configuration>
  • Add reference to SpecFlow. This will be automatically added if you install NuGet package.
  • Create your first SpecFlow Feature, edit the content to set up your feature and scenario(s).
  • Run the test on the Test project. Copy the code from the error message into a new file in StepDefinitions directory.

That's it!

If you want to run Selenium in SpecFlow scenarios, check SpecFlow Examples on GitHub (link), specifically SeleniumSupport class (link). More on this in the follow-up post Selenium with SpecFlow.

No comments: