Your first C# test with SpecFlow

SpecFlow is an open-source .NET utility which allows you to write tests using Cucumber-compatible Gherkin syntax.

Prerequisites

You can install SpecFlow via the NuGet Gallery

  • Create a new project in Visual Studio.
  • In the Visual Studio Tools menu, go to Library Package Manager > Manage Nuget Package for Solution.
  • This will open the Manage NuGet Packages dialog. Click Online, then Next.
  • In the Search Packages field, enter SpecFlow and click Search.
  • Select SpecFlow from the search results and click Install.
  • For more details, please visit SpecFlow installation Document.


    Sample Test

    This is a sample test case in SpecFlow:

    // Google Feature
    Feature: Google
    
    Scenario Outline: Can find search results
      Given I am on the google page for  and 
      When I search for "TestingBot"
      Then I should see title "TestingBot - Google Search"
      
      Examples:
        | profile | environment |
        | single    | chrome      |

    // Google Steps
    [Binding]
    public class SingleSteps
    {
      private IWebDriver _driver;
      readonly TestingBotDriver _tbDriver;
    
      public SingleSteps()
      {
        _tbDriver = (TestingBotDriver)ScenarioContext.Current["tbDriver"];
      }
    
      [Given(@"I am on the google page for (.*) and (.*)")]
      public void GivenIAmOnTheGooglePage(string profile, string environment)
      {
        _driver = _tbDriver.Init(profile, environment);
        _driver.Navigate().GoToUrl("https://www.google.com/ncr");
      }
    
      [When(@"I search for ""(.*)""")]
      public void WhenISearchFor(string keyword)
      {
        var q = _driver.FindElement(By.Name("q"));
        q.SendKeys(keyword);
        q.Submit();
      }
    
      [Then(@"I should see title ""(.*)""")]
      public void ThenIShouldSeeTitle(string title)
      {
        Thread.Sleep(5000);
        Assert.That(_driver.Title, Is.EqualTo(title));
      }
    }

    Now you need to create the TestingBotDriver we will be using:


    [Binding]
    public sealed class TestingBot
    {
      private TestingBotDriver tbDriver;
      private string[] tags;
    
      [BeforeScenario]
      public void BeforeScenario()
      {
        tbDriver = new TestingBotDriver(ScenarioContext.Current);
        ScenarioContext.Current["tbDriver"] = tbDriver;
      }
    
      [AfterScenario]
      public void AfterScenario()
      {
        tbDriver.Cleanup();
      }
    }

    public class TestingBotDriver
    {
      private IWebDriver driver;
      private string profile;
      private string environment;
      private ScenarioContext context;
    
      public TestingBotDriver(ScenarioContext context)
      {
        this.context = context;
      }
    
      public IWebDriver Init(string profile, string environment)
      {
        NameValueCollection caps = ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection;
        NameValueCollection settings = ConfigurationManager.GetSection("environments/" + environment) as NameValueCollection;
    
        DesiredCapabilities capability = new DesiredCapabilities();
    
        foreach (string key in caps.AllKeys)
        {
          capability.SetCapability(key, caps[key]);
        }
    
        foreach (string key in settings.AllKeys)
        {
          capability.SetCapability(key, settings[key]);
        }
    
        String key = Environment.GetEnvironmentVariable("TB_KEY");
        if (key == null)
        {
          key = ConfigurationManager.AppSettings.Get("key");
        }
    
        String secret = Environment.GetEnvironmentVariable("TB_SECRET");
        if (secret == null)
        {
          secret = ConfigurationManager.AppSettings.Get("secret");
        }
    
        capability.SetCapability("key", key);
        capability.SetCapability("secret", secret);
    
        driver = new RemoteWebDriver(new Uri("http://" + ConfigurationManager.AppSettings.Get("server") + "/wd/hub/"), capability);
        return driver;
      }
    
      public void Cleanup()
      {
        driver.Quit();
      }
    }

    Now we need to create the config file which the code above uses to determine various settings.


    
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
    
        <sectionGroup name="capabilities">
          <section name="single" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </sectionGroup>
    
        <sectionGroup name="environments">
          <section name="chrome" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </sectionGroup>
      </configSections>
    
      <appSettings>
        <add key="key" value="key" />
        <add key="secret" value="secret" />
        <add key="server" value="hub.testingbot.com" />
      </appSettings>
    
      <capabilities>
        <single>
          <add key="screenrecorder" value="true" />
        </single>
      </capabilities>
    
      <environments>
        <chrome>
          <add key="browser" value="chrome" />
        </chrome>
      </environments>
      
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /></startup><specFlow>
        <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
      <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --><unitTestProvider name="NUnit" /></specFlow></configuration>
    

Configuring capabilities

To let TestingBot know on which browser/platform you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.


<environments>
  <tb>
    <add key="platform" value="WINDOWS" />
    <add key="browserName" value="Chrome" />
    <add key="version" value="latest" />
  </tb>
</environments>

To see how to do this please select a combination of browser version and platform in the drop-down menus below


1. Select a Platform
2. Select a Browser


Testing Internal/Staged Websites

We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.


The example below shows how to easily run a Java WebDriver test with our Tunnel:


1. Download our tunnel and start the tunnel:

java -jar testingbot-tunnel.jar key secret

2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub' like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.


This way your test will go securily through the tunnel to TestingBot and back:


<appSettings>
    <add key="key" value="key" />
    <add key="secret" value="secret" />
    <add key="server" value="localhost:4445" />
</appSettings>

Other Options

We offer many other test options, for example: disable video recording, specifying a custom Firefox Profile, loading Chrome/Firefox/Safari extensions, running an executable before your test starts, uploading files, ...
See our list of test options for a full list of options to customize your tests.

Parallel Testing

Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.

You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.
TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.


Parallel Example


To run multiple tests at the same time with SpecFlow, modify the config file like this:


// App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />

    <sectionGroup name="capabilities">
      <section name="parallel" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>

    <sectionGroup name="environments">
      <section name="chrome" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="firefox" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="safari" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      <section name="ie" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>
  </configSections>

  <appSettings>
    <add key="key" value="key" />
    <add key="secret" value="secret" />
    <add key="server" value="hub.testingbot.com" />
  </appSettings>

  <capabilities>
    <parallel>
      <add key="screenrecorder" value="true" />
    </parallel>
  </capabilities>

  <environments>
    <chrome>
      <add key="browser" value="chrome" />
    </chrome>
    <firefox>
      <add key="browser" value="firefox" />
    </firefox>
    <safari>
      <add key="browser" value="safari" />
    </safari>
    <ie>
      <add key="browser" value="ie" />
    </ie>
  </environments>
  
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /></startup><specFlow>
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
  <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --><unitTestProvider name="NUnit" /></specFlow></configuration>

// Add to AssemblyInfo.cs
[assembly: Parallelizable(ParallelScope.Fixtures)]

The test cases you want to run in parallel need to be written like this:


// Google Feature for parallel run
Feature: Google

Scenario Outline: Can find search results
  Given I am on the google page for <profile> and <environment>
  When I search for "TestingBot"
  Then I should see title "TestingBot - Google Search"
  
  Examples:
    | profile | environment |
    | parallel  | chrome      |
    | parallel  | firefox     |
    | parallel  | safari      |
    | parallel  | ie          |

You can now run the tests in parallel by running the tests with fixture parallel from Test Explorer.


Queueing

Every plan we provide comes with a limit of concurrent VMs (how many tests you can run in parallel).
For example: if you have a plan with 5 concurrent VMs, it is possible to start more tests. TestingBot will queue the additional tests and run the tests as soon as slots become available.

Pick a C# Test Framework

  • NUnit

    An unit testing framework that is open source written in C#.

  • PNunit

    With PNUnit you can run several tests in parallel.

  • SpecFlow

    SpecFlow allows you to run Automated .NET tests using Cucumber-compatible Gherkin syntax.