---
title: SpecFlow C# Automated Selenium Testing Framework
description: SpecFlow C# Automated Selenium testing. Run your tests on our Selenium
  and Appium grid.
source_url:
  html: https://testingbot.com/support/web-automate/selenium/csharp/specflow
  md: https://testingbot.com/support/web-automate/selenium/csharp/specflow/index.md
---
**SpecFlow reached End of Life (EOL) on December 31, 2024.** For new projects, we recommend using [Reqnroll](https://testingbot.com/support/web-automate/selenium/csharp/reqnroll), the community-maintained successor to SpecFlow. Reqnroll is fully compatible with SpecFlow and provides an easy migration path.

# Your first C# test with SpecFlow

See our [SpecFlow example repository](https://github.com/testingbot/specflow-example) for a simple example on how to run SpecFlow tests in parallel on TestingBot.

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

## Installation

You can install SpecFlow via the [NuGet Gallery](https://www.nuget.org/)

- 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](https://specflow.org/getting-started/#InstallSetup).

### Sample Test

This is a sample test case in SpecFlow:

    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 |
        | single | chrome |

    [Binding]
    public class SingleSteps
    {
        private readonly ScenarioContext _scenarioContext;
        private IWebDriver _driver;
        private TestingBotDriver _tbDriver;
    
        public SingleSteps(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
            _tbDriver = (TestingBotDriver)_scenarioContext["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. The hooks class accepts `ScenarioContext` via constructor injection, `ScenarioContext.Current` is deprecated and not safe under parallel execution.

    [Binding]
    public sealed class TestingBotHooks
    {
        private readonly ScenarioContext _scenarioContext;
        private TestingBotDriver _tbDriver;
    
        public TestingBotHooks(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
        }
    
        [BeforeScenario]
        public void BeforeScenario()
        {
            _tbDriver = new TestingBotDriver(_scenarioContext);
            _scenarioContext["tbDriver"] = _tbDriver;
        }
    
        [AfterScenario]
        public void AfterScenario()
        {
            _tbDriver.Cleanup();
        }
    }

    public class TestingBotDriver
    {
        private IWebDriver driver;
        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;
    
            // Build the right options class for the requested browser. ChromeOptions hardcodes
            // browserName=chrome, so we can't just always use it.
            string browserName = settings["browserName"] ?? "chrome";
            DriverOptions options = browserName.ToLowerInvariant() switch
            {
                "firefox" => new FirefoxOptions(),
                "microsoftedge" => new EdgeOptions(),
                "safari" => new SafariOptions(),
                _ => new ChromeOptions()
            };
    
            options.BrowserVersion = settings["browserVersion"];
            options.PlatformName = settings["platformName"];
    
            // Standard W3C capabilities go on the typed setters above; everything else
            // (vendor-specific TestingBot caps) goes inside tb:options.
            var tbOptions = new Dictionary<string, object>();
            foreach (string key in caps.AllKeys)
            {
                tbOptions[key] = caps[key];
            }
    
            string tbKey = Environment.GetEnvironmentVariable("TESTINGBOT_KEY")
                ?? ConfigurationManager.AppSettings.Get("key");
            string tbSecret = Environment.GetEnvironmentVariable("TESTINGBOT_SECRET")
                ?? ConfigurationManager.AppSettings.Get("secret");
    
            tbOptions["key"] = tbKey;
            tbOptions["secret"] = tbSecret;
    
            options.AddAdditionalOption("tb:options", tbOptions);
    
            driver = new RemoteWebDriver(new Uri("https://" + ConfigurationManager.AppSettings.Get("server") + "/wd/hub"), options);
            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="API_KEY" />
        <add key="secret" value="API_SECRET" />
        <add key="server" value="hub.testingbot.com" />
      </appSettings>
    
      <capabilities>
        <single>
          <add key="screenrecorder" value="true" />
        </single>
      </capabilities>
    
      <environments>
        <chrome>
          <add key="browserName" value="chrome" />
          <add key="browserVersion" value="latest" />
          <add key="platformName" value="WIN11" />
        </chrome>
      </environments>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
      </startup>
    </configuration>

## Specify Browsers & Devices

To let TestingBot know on which browser/platform/device 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="platformName" value="WIN11" />
        <add key="browserName" value="chrome" />
        <add key="browserVersion" 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.

  ![OS selected](https://testingbot.com/assets/environments/svg/windows11-0e1b28bc0fdd5034d3e4d3dc8d346c500a8c6522facf4b45d0da56537c1f1c6d.svg) Windows 11 › ![Browser Selected](https://testingbot.com/assets/environments/svg/chrome-c4081ff447d2d898d4afcb8f074a907c960e6f007716c1a1d119eee6803c4042.svg) Chrome 139 

Loading environments...

Please wait while we load the available browsers and platforms.

    

## Testing Internal Websites

We've built [TestingBot Tunnel](https://testingbot.com/support/tunnel), to provide you with a secure way to run tests against your staged/internal webapps.  
Please see our [TestingBot Tunnel documentation](https://testingbot.com/support/tunnel) for more information about this easy to use tunneling solution.

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

1. [Download our tunnel](https://testingbot.com/support/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 securely through the tunnel to TestingBot and back:

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

## Run tests in Parallel

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.

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="edge" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </sectionGroup>
      </configSections>
    
      <appSettings>
        <add key="key" value="API_KEY" />
        <add key="secret" value="API_SECRET" />
        <add key="server" value="hub.testingbot.com" />
      </appSettings>
    
      <capabilities>
        <parallel>
          <add key="screenrecorder" value="true" />
        </parallel>
      </capabilities>
    
      <environments>
        <chrome>
          <add key="browserName" value="chrome" />
          <add key="browserVersion" value="latest" />
          <add key="platformName" value="WIN11" />
        </chrome>
        <firefox>
          <add key="browserName" value="firefox" />
          <add key="browserVersion" value="latest" />
          <add key="platformName" value="WIN11" />
        </firefox>
        <safari>
          <add key="browserName" value="safari" />
          <add key="browserVersion" value="latest" />
          <add key="platformName" value="SONOMA" />
        </safari>
        <edge>
          <add key="browserName" value="MicrosoftEdge" />
          <add key="browserVersion" value="latest" />
          <add key="platformName" value="WIN11" />
        </edge>
      </environments>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
      </startup>
    </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 | edge |

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

### Queuing

Every plan we provide comes with a limit of parallel tests.   
 If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.

## Mark tests as passed/failed

To see if a test passed or not in our member area, or to send additional meta-data to TestingBot, you can use our API.

Please see the example below on how to notify TestingBot about the test success state:

    [TearDown]
    public void CleanUp()
    {
        bool passed = TestContext.CurrentContext.Result.Status == TestStatus.Passed;
        try
        {
            // Logs the result to TestingBot
            ((IJavaScriptExecutor)driver).ExecuteScript("tb:test-result=" + (passed ? "passed" : "failed"));
        }
        finally
        {
            // Terminates the remote webdriver session
            driver.Quit();
        }
    }

## Other C# Framework examples

- [Reqnroll](https://testingbot.com/support/web-automate/selenium/csharp/reqnroll)

Reqnroll is the community-maintained successor to SpecFlow for BDD testing with Gherkin syntax. **Recommended for new projects.**

- [NUnit](https://testingbot.com/support/web-automate/selenium/csharp/nunit)

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

- [PNunit](https://testingbot.com/support/web-automate/selenium/csharp/pnunit)

With PNUnit you can run several tests in parallel.

- [MSTest](https://testingbot.com/support/web-automate/selenium/csharp/mstest)

MSTest framework is a test framework which is included, by default, with Microsoft Visual Studio.

- [xUnit](https://testingbot.com/support/web-automate/selenium/csharp/xunit)

xUnit.net is a modern, extensible testing framework for .NET, widely used in the .NET ecosystem.

### Looking for more help?

Have questions or need more information? Reach out via email or Slack.

[Email us](https://testingbot.com/contact/new)[Slack Join our Slack](https://join.slack.com/t/testingb0t/shared_invite/zt-3bcw9xch-jk19~6XPs_xBrsAgAedkCw)
