Skip to main content
Introducing the Automate SDK! Get your entire test suite running on BrowserStack in minutes! Learn More.

Selenium with SpecFlow

A guide to run Selenium Webdriver tests with SpecFlow on BrowserStack.

Note: Check out the SpecFlow sample repository on GitHub to access code samples used in this guide.

Introduction

BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with SpecFlow on BrowserStack is simple. This guide will help you:

  1. Run your first test
  2. Mark tests as passed or failed
  3. Debug your app

Prerequisites

Before you can start running your Selenium tests with SpecFlow, ensure you have the SpecFlow libraries installed:

Run your first test

Protip: Selenium 4 is now supported on BrowserStack. To use the Selenium 4 client bindings, modify your existing test scripts as follows:
  • Edit or add capabilities in the W3C format using our W3C capability generator.
  • Add the seleniumVersion capability in your test script and set the value to 4.0.0.

To understand how to integrate with BrowserStack, we will look at two things:

  1. A sample test case written in SpecFlow with C#
  2. Integration of this sample test case with BrowserStack

Sample test case

The sample SpecFlow test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”

// 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 "BrowserStack"
  Then I should see title "BrowserStack - Google Search"

  Examples:
    | profile | environment |
    | parallel  | chrome      |
    | parallel  | firefox     |
    | parallel  | safari      |
    | parallel  | ie          |

Once we have defined the feature file, which contains the test case, we now need to create the step definition. The step definition for the SpecFlow test case shown above is as follows:

// Google Steps
[Binding]
public class SingleSteps
{
  private IWebDriver _driver;
  readonly BrowserStackDriver _bsDriver;

  public SingleSteps(ScenarioContext scenarioContext)
  {
    _bsDriver = (BrowserStackDriver)scenarioContext["bsDriver"];
  }

  [Given(@"I am on the google page for (.*) and (.*)")]
  public void GivenIAmOnTheGooglePage(string profile, string environment)
  {
    _driver = _bsDriver.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 that we have created the feature file and the step definition, we are ready to integrate this SpecFlow test case into BrowserStack.

Integrate with BrowserStack

Now that we have created a feature file and step definitions, we can integrate our SpecFlow test case into BrowserStack. To start, we define two classes which contain methods to configure and create the connection with BrowserStack.

[Binding]
public sealed class BrowserStack
{
  private readonly ScenarioContext _scenarioContext;
  private BrowserStackDriver bsDriver;
  private string[] tags;

  public BrowserStack(ScenarioContext context)
  {
      _scenarioContext = context;
  }

  [BeforeScenario]
  public void BeforeScenario()
  {
    bsDriver = new BrowserStackDriver(_scenarioContext);
    _scenarioContext["bsDriver"] = bsDriver;
  }

  [AfterScenario]
  public void AfterScenario()
  {
    bsDriver.Cleanup();
  }
}
public class BrowserStackDriver
{
  private IWebDriver driver;
  private Local browserStackLocal;
  private string profile;
  private string environment;
  private ScenarioContext context;

  public BrowserStackDriver(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 username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
    if (username == null)
    {
      username = ConfigurationManager.AppSettings.Get("user");
    }

    String accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
    if (accesskey == null)
    {
      accesskey = ConfigurationManager.AppSettings.Get("key");
    }

    capability.SetCapability("browserstack.user", username);
    capability.SetCapability("browserstack.key", accesskey);
    capability.SetCapability("name", "Bstack-[SpecFlow] Sample Test");

    File.AppendAllText("C:\\Users\\Admin\\Desktop\\sf.log", "Starting local");

    if (capability.GetCapability("browserstack.local") != null && capability.GetCapability("browserstack.local").ToString() == "true")
    {
      browserStackLocal = new Local();
      List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
        new KeyValuePair<string, string>("key", accesskey)
      };
      browserStackLocal.start(bsLocalArgs);
    }

    File.AppendAllText("C:\\Users\\Admin\\Desktop\\sf.log", "Starting driver");
    driver = new RemoteWebDriver(new Uri("https://" + ConfigurationManager.AppSettings.Get("server") + "/wd/hub/"), capability);
    return driver;
  }

  public void Cleanup()
  {
    driver.Quit();
    if (browserStackLocal != null)
    {
      browserStackLocal.stop();
    }
  }
}

We now need to create a .config file, which contains the BrowserStack Hub URL and credentials required to connect to the BrowserStack Selenium grid.

  // 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="user" value="YOUR_USERNAME" />
      <add key="key" value="YOUR_ACCESS_KEY" />
      <add key="server" value="hub-cloud.browserstack.com" />
    </appSettings>

    <capabilities>
      <parallel>
        <add key="browserstack.debug" 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>

Run your test on BrowserStack as follows:

Note: Build the solution in Visual Studio 2015 Update 2
Run test with fixture “parallel” from Test Explorer

Mark tests as passed or failed

BrowserStack provides a comprehensive REST API to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as passed or failed based on the assertions in your SpecFlow test cases.

namespace RestApi {
  class ChangeSessionStatus {
    static void Main(string[] args) {
      string reqString = "{\"status\":\"passed\", \"reason\":\"\"}";

      byte[] requestData = Encoding.UTF8.GetBytes(reqString);
      Uri myUri = new Uri(string.Format("https://www.browserstack.com/automate/sessions/<session-id>.json"));
      WebRequest myWebRequest = HttpWebRequest.Create(myUri);
      HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
      myWebRequest.ContentType = "application/json";
      myWebRequest.Method = "PUT";
      myWebRequest.ContentLength = requestData.Length;
      using (Stream st = myWebRequest.GetRequestStream())st.Write(requestData, 0, requestData.Length);

      NetworkCredential myNetworkCredential = new NetworkCredential("YOUR_USERNAME", "YOUR_ACCESS_KEY");
      CredentialCache myCredentialCache = new CredentialCache();
      myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
      myHttpWebRequest.PreAuthenticate = true;
      myHttpWebRequest.Credentials = myCredentialCache;

      myWebRequest.GetResponse().Close();
    }
  }
}

The two potential values for status can either be completed or error. Optionally, a reason can also be passed.

You can find the full reference to our REST API.

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.

  • Text Logs

Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.

  • Visual Logs

Visual Logs automatically capture the screenshots generated at every Selenium command run through your SpecFlow tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.

Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug capability to true.

<capabilities>
  <single>
    <add key="browserstack.debug" value="true" />
  </single>
</capabilities>
  • Video recording

Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.

Note: Video recording increases test execution time slightly. You can disable this feature by setting the browserstack.video capability to false.

<capabilities>
  <single>
    <add key="browserstack.video" value="false" />
  </single>
</capabilities>

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. You can find the complete details to enable all the debugging options.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert