Skip to end of metadata
Go to start of metadata

With Sauce Labs you can run automated tests of your web apps against a wide variety of device/operating system/browser combinations.  This topic provides basic information and example code to help you get up and running with your C# tests on Sauce. 

Example Only

The code in this topic is presented as an example only, since your tests and testing environments may require specialized scripting. This information should be taken only as an illustration of how you could set up your tests with Sauce Labs, and is not directly supported by Sauce.


Quick Start

Configuring Selenium tests to run on Sauce Labs is simple. The basic change is to switch from using a local Selenium driver to using a remote driver pointed at, specifying your Sauce Labs account credentials and desired browser configuration.

Local C# Testing Example
IWebDriver driver = new FirefoxDriver();
Remote C# Testing on Sauce Example
IWebDriver driver;
DesiredCapabilities desiredCapability = DesiredCapabilities.Firefox()// set the desired browser
desiredCapability.SetCapability("platform", "Windows 7"); // operating system to use
driver = new RemoteWebDriver(new Uri("");

Code Sample


This example test opens Google, verifies that “Google” is the title of the page, and then searches for Sauce Labs. It connects to Sauce Labs, run commands to remote control the target browser, and reports the results. It also includes the code for running tests in parallel and reporting the results to your Sauce Labs dashboard. 

You can use the Platform Configurator to specify the desired capabilities for any browser/platform combination you want for your test. 

You can clone this script directly from our GitHub repo.

PNUnit_Test.cs Sample Script
using NUnit.Framework;
using System;
using Selenium;
using System.Web;
using System.Text;
using System.Net;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;

namespace Saucey_Selenium {
    [TestFixture("chrome", "45", "Windows 7", "", "")]
    public class SauceNUnit_Test
        private IWebDriver driver;
        private String browser;
        private String version;
        private String os;
        private String deviceName;
        private String deviceOrientation;

        public SauceNUnit_Test(String browser, String version, String os, String deviceName, String deviceOrientation)
            this.browser = browser;
            this.version = version;
            this.os = os;
            this.deviceName = deviceName;
            this.deviceOrientation = deviceOrientation;

        public void Init()
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.SetCapability(CapabilityType.BrowserName, browser);
            caps.SetCapability(CapabilityType.Version, version);
            caps.SetCapability(CapabilityType.Platform, os);
            caps.SetCapability("deviceName", deviceName);
            caps.SetCapability("deviceOrientation", deviceOrientation);
            caps.SetCapability("username", "SAUCE_USERNAME");
            caps.SetCapability("accessKey", "SAUCE_ACCESS_KEY");
            caps.SetCapability("name", TestContext.CurrentContext.Test.Name);

            driver = new RemoteWebDriver(new Uri(""), caps, TimeSpan.FromSeconds(600))


        public void googleTest()
            StringAssert.Contains("Google", driver.Title);
            IWebElement query = driver.FindElement(By.Name("q"));
            query.SendKeys("Sauce Labs");

        public void CleanUp()
            var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
            ((IJavaScriptExecutor)Driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));

Selenium 3 Example

The use of Desired Capabilities is deprecated for C# in Selenium 3. This example illustrates how to set the platform/operating system/browser for C# using Selenium 3.

using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System;

namespace SeleniumNunit
    public class GettingStartedTest
        IWebDriver Driver;
        /// <summary>
        /// This is a simple Sauce Labs test to help you get started.
        /// This does NOT follow the recommeded best practices for test automation.
        /// </summary>
        public void SampleSauceTest()
            //TODO please supply your Sauce Labs user name in an environment variable
            var sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);
            //TODO please supply your own Sauce Labs access Key in an environment variable
            var sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User);

            ChromeOptions options = new ChromeOptions();
            options.AddAdditionalCapability(CapabilityType.Version, "latest", true);
            options.AddAdditionalCapability(CapabilityType.Platform, "Windows 10", true);
            options.AddAdditionalCapability("username", sauceUserName, true);
            options.AddAdditionalCapability("accessKey", sauceAccessKey, true);
            options.AddAdditionalCapability("name", TestContext.CurrentContext.Test.Name, true);

            Driver =  new RemoteWebDriver(new Uri(""), options.ToCapabilities(),

        public void CleanUpAfterEveryTestMethod()
            var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
            ((IJavaScriptExecutor)Driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
            if(Driver != null)

Running Local Tests

Developing websites/apps within your local network is secure and efficient. The drawback to this method is that local assets are not publicly-accessible on the Internet, so the browsers/devices in the Sauce Labs cloud can't load and test your app. The solution is to use Sauce ConnectSauce Connect is a proxy server that creates a secure tunnel connection between the Sauce Labs virtual machine that runs your test and your local  network. You can also use Sauce Connect to test applications and websites that are located within your corporate firewall. Sauce Connect is not required to run tests on Sauce Labs, only in situations where the application or website you want to test is not publicly accessible. 

Running Tests in Parallel

Now that you’re running tests on Sauce, you may wonder how you can run your tests faster. One way to increase speed is by running tests in parallel across multiple virtual machines.

You can run your tests in parallel at two levels, and you can run your tests in parallel across multiple browsers. For example, if you have 10 tests and want to run on five browsers, this would be parallelism of five. You can also run tests across browsers and each test in parallel. Using our previous example this would be more like 50 parallel tests. Doing this requires that your tests are written in a way that they do not collide with one another, as described in our Best Practice topics avoiding external test dependencies and avoiding dependencies between tests.

Match Thread Count to Concurrency Limit

You should match your thread count to your concurrency limit, which is shown in the My Account section of your user profile information on the Sauce Labs dashboard.  

For more information, check out the example scripts in our GitHub repo.  

Reporting on Test Results

Selenium is a protocol focused on automating the actions of a browser, and as such, it doesn't encompass the notions of a "test" that "passes" or "fails." Because of this, you need to include a section in your code that explicitly gets the status of the test, and then sends it to the Sauce Labs dashboard. 

C# Code for Reporting Test Results to the Sauce Labs Dashboard
        public void Cleanup()
            // Gets the status of the current test
            bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
                // Logs the result to Sauce Labs
                ((IJavaScriptExecutor)driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
                // Terminates the remote webdriver session