Skip to main content

Selenium with NUnit

Your guide to running Selenium Webdriver tests with NUnit on BrowserStack.

Note: Code samples in this guide can be found in the nunit-browserstack repo in GitHub.


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

  1. Running your first test
  2. Integrating your tests with BrowserStack
  3. Marking tests as passed or failed
  4. Debugging your app


  • You need to have BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
  • Before you can start running your Selenium tests with NUnit, install NUnit and NUnit3TestAdapter using NuGet Gallery.

Running 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 run Selenium tests with NUnit on BrowserStack Automate, follow the below steps:

  1. Clone the nunit-browserstack repo on GitHub with BrowserStack’s sample test, using the following command:

     git clone
  2. Open the solution NUnit-BrowserStack.sln in Visual Studio.

  3. Update App.config file within nunit-browserstack/NUnit-BrowserStack/ directory with your BrowserStack Username and Access Key.

     <?xml version="1.0" encoding="utf-8" ?>
         <sectionGroup name="capabilities">
           <section name="single" type="System.Configuration.AppSettingsSection, System.Configuration, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
         <sectionGroup name="environments">
           <section name="chrome" type="System.Configuration.AppSettingsSection, System.Configuration, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
         <add key="user" value="YOUR_USERNAME" />
         <add key="key" value="YOUR_ACCESS_KEY" />
         <add key="server" value="" />
           <add key="browserstack.debug" value="true" />
           <add key="browser" value="chrome" />
  4. Now, execute your first test on BrowserStack using the following steps:

    1. Build the solution in Visual Studio
    2. Run the test with fixture single from Test Explorer
  5. View your test results on the BrowserStack Automate dashboard.

Protip: You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

Details of your first test

Following is the sample NUnit test case that you ran above. The test searches for “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”.

[TestFixture("single", "chrome")]
public class SingleTest : BrowserStackNUnitTest
  public SingleTest(string profile, string environment) : base(profile, environment) { }

  public void SearchGoogle()
    IWebElement query = driver.FindElement(By.Name("q"));
    Assert.AreEqual("BrowserStack - Google Search", driver.Title);

Integrating your tests with BrowserStack

The integration of your test with BrowserStack is working with the help of BrowserStackNUnitTest.cs file which contains the methods to configure and create the connection with BrowserStack as shown below:

public class BrowserStackNUnitTest
  protected IWebDriver driver;
  protected string profile;
  protected string environment;
  private Local browserStackLocal;

  public BrowserStackNUnitTest(string profile, string environment)
    this.profile = profile;
    this.environment = environment;

  public void Init()
    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);

    String appId = Environment.GetEnvironmentVariable("BROWSERSTACK_APP_ID");
    if (appId != null)
      capability.SetCapability("app", appId);

    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)

    driver = new RemoteWebDriver(new Uri("http://"+ ConfigurationManager.AppSettings.Get("server") +"/wd/hub/"), capability);

  public void Cleanup()
    if (browserStackLocal != null)

Marking 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 pass or fail based on the assertions in your NUnit test cases.

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

      byte[] requestData = Encoding.UTF8.GetBytes(reqString);
      Uri myUri = new Uri(string.Format("<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;


You can find the full reference to our REST API.

Debugging 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 JUnit 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.

    <add key="browserstack.debug" value="true" />

Sample Visual Logs from Automate Dashboard: BrowserStack Automate Visual Logs

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 capability to false.
    <add key="" value="false" />

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.

Next steps

Once you have successfully run your first test on BrowserStack, you might want to do one of the following:

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?


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