Skip to main content
Looking to test on every commit or PR? Introducing plans for high-scale testing on Chrome and Firefox. Read more

Run Tests in Parallel

Run C# tests in parallel to achieve faster builds

On BrowserStack, you can run multiple Selenium Webdriver tests at the same time across various browser, device and OS combinations. This is “Parallel Testing”. Parallel Testing gives you the same benefits as running a multi-threaded application.

With Parallel Testing, you can run the same test on different browser/device combinations i.e. cross-browser testing, or run different tests on the same or different browser/device combinations. Parallel Testing will help you reduce the run time of your test suite, resulting in faster build times and faster releases.

You can start testing in parallel using one of the popular test frameworks which work with C# and Selenium or refer the following section. We have provided getting started guides on some of the popular frameworks below:

Run tests in parallel without a framework

You can achieve parallel testing in multiple ways even if you are not using one of the popular test frameworks which has feature for enabling parallel testing. We show two such ways below but this is not an exhaustive list:

Using a multi-threaded program

The following sample script shows a multi-threaded C# program. These are the salient points of the script:

  • The same script is run across 4 different browser and OS combinations viz. Chrome on Win10, Firefox on Win10, Safari on MacOS, Edge on Win10.
  • The sampleTestCase function passes capabilities to executetestwithcaps function based on the browser.
  • The executetestwithcaps function takes capabilities as the argument and executes the same script on each set of capability as parallel sessions.
using System.Threading;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace MultiThreading
    class MultiThreading
        static void Main(string[] args)
            //Creating Threads and defining the browser and OS combinations where the test will run
            Thread t1 = new Thread(obj => sampleTestCase("Chrome", "latest", "Windows", "10", "Sample test on Chrome", "Parallel-build-csharp"));
            Thread t2 = new Thread(obj => sampleTestCase("Firefox", "latest", "Windows", "10", "Sample test on Firefox", "Parallel-build-csharp"));
            Thread t3 = new Thread(obj => sampleTestCase("Safari", "latest", "OSX", "Catalina", "Sample test on Safari", "Parallel-build-csharp"));
            Thread t4 = new Thread(obj => sampleTestCase("Edge", "latest", "Windows", "10", "Sample test on Edge", "Parallel-build-csharp"));

            //Executing the methods
        static void sampleTestCase(String browser, String version, String os, String os_version, String test_name, String build_name)
            switch (browser)
                case "Safari": //If browser is Safari, following capabilities will be passed to 'executetestwithcaps' function
                    OpenQA.Selenium.Safari.SafariOptions safariCapability = new OpenQA.Selenium.Safari.SafariOptions();
                    safariCapability.AddAdditionalCapability("os_version", os_version);
                    safariCapability.AddAdditionalCapability("browser", browser);
                    safariCapability.AddAdditionalCapability("browser_version", version);
                    safariCapability.AddAdditionalCapability("os", os);
                    safariCapability.AddAdditionalCapability("name", test_name); // test name
                    safariCapability.AddAdditionalCapability("build", build_name); // Your tests will be organized within this build
                    safariCapability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
                    safariCapability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
                case "Chrome": //If browser is Chrome, following capabilities will be passed to 'executetestwithcaps' function
                    OpenQA.Selenium.Chrome.ChromeOptions chromeCapability = new OpenQA.Selenium.Chrome.ChromeOptions();
                    chromeCapability.AddAdditionalCapability("os_version", os_version, true);
                    chromeCapability.AddAdditionalCapability("browser", browser, true);
                    chromeCapability.AddAdditionalCapability("browser_version", version, true);
                    chromeCapability.AddAdditionalCapability("os", os, true);
                    chromeCapability.AddAdditionalCapability("name", test_name, true);
                    chromeCapability.AddAdditionalCapability("build", build_name, true);
                    chromeCapability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME", true);
                    chromeCapability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY", true);
                case "Firefox": //If browser is Firefox, following capabilities will be passed to 'executetestwithcaps' function
                    OpenQA.Selenium.Firefox.FirefoxOptions firefoxCapability = new OpenQA.Selenium.Firefox.FirefoxOptions();
                    firefoxCapability.AddAdditionalCapability("os_version", os_version, true);
                    firefoxCapability.AddAdditionalCapability("browser", browser, true);
                    firefoxCapability.AddAdditionalCapability("browser_version", version, true);
                    firefoxCapability.AddAdditionalCapability("os", os, true);
                    firefoxCapability.AddAdditionalCapability("name", test_name, true);
                    firefoxCapability.AddAdditionalCapability("build", build_name, true);
                    firefoxCapability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME", true);
                    firefoxCapability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY", true);
                case "Edge": //If browser is Edge, following capabilities will be passed to 'executetestwithcaps' function
                    OpenQA.Selenium.Edge.EdgeOptions edgecapability = new OpenQA.Selenium.Edge.EdgeOptions();
                    edgecapability.AddAdditionalCapability("os_version", os_version);
                    edgecapability.AddAdditionalCapability("browser", browser);
                    edgecapability.AddAdditionalCapability("browser_version", version);
                    edgecapability.AddAdditionalCapability("os", os);
                    edgecapability.AddAdditionalCapability("name", test_name);
                    edgecapability.AddAdditionalCapability("build", build_name);
                    edgecapability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
                    edgecapability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
                default: //If browser is IE, following capabilities will be passed to 'executetestwithcaps' function
                    OpenQA.Selenium.IE.InternetExplorerOptions ieCapability = new OpenQA.Selenium.IE.InternetExplorerOptions();
                    ieCapability.AddAdditionalCapability("os_version", os_version, true);
                    ieCapability.AddAdditionalCapability("browser", browser, true);
                    ieCapability.AddAdditionalCapability("browser_version", version, true);
                    ieCapability.AddAdditionalCapability("os", os, true);
                    ieCapability.AddAdditionalCapability("name", test_name, true);
                    ieCapability.AddAdditionalCapability("build", build_name, true);
                    ieCapability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME", true);
                    ieCapability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY", true);

        //executetestwithcaps function takes capabilities from 'sampleTestCase' function and searches for 'BrowserStack' on
        static void executetestwithcaps(DriverOptions capability)
            IWebDriver driver = new RemoteWebDriver(
              new Uri(""), capability
            IWebElement query = driver.FindElement(By.Name("q"));
            // Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page starts with 'BrowserStack'
            if (string.Equals(driver.Title.Substring(0, 12), "BrowserStack"))
                ((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \" Title matched!\"}}");
                ((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title not matched \"}}");

Using a multi-threaded program and using ThreadPoolExecutor to manage threads

In case of the above implementation, all the threads are dispatched at the same time. You can also manage the dispatching of threads by applying additional conditions, but, in that case, you would have to manually keep track of which thread has completed its execution and depending on that, you will be required to spawn new threads keeping in mind you do not exceed the limit of parallel tests that is supported by your BrowserStack account.

You can choose to not manage the threads manually and use the C# ThreadPool to do the same for you by following the steps mentioned in the documentation for threadpool in C#

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