Run Selenium C# Tests on Real Devices

Test your website using Selenium C# Automated Tests in real user conditions on real device cloud

Get Started free
Home Guide Selenium with C# : How to start running Automated Tests

Selenium with C# : How to start running Automated Tests

By Ganesh Hegde, Community Contributor -

The importance of automation testing cannot be overstated. Within narrow deadlines and changing customer demands hovering over them, QAs must automate numerous tests, test cases, and projects to meet said deadlines, save time, effort and get error-free results. 

Selenium is undoubtedly the most popular tool for test automation. Quite often, Selenium needs to be used with C# and Visual Studio IDE together for a better-automated testing experience. 

Using Selenium, C#, and Visual Studio together provides a unique opportunity to create a robust, scalable, and flexible automation framework. This article will discuss how to do exactly that.

What is C#?

C# (also referred to as C Sharp) is a modern, object-oriented, and type-safe programming language. It enables developers to build many types of secure and robust applications that run in the .NET ecosystem. C# has its roots in the C family of languages and will be immediately familiar to C, C++, Java, and JavaScript programmers.

Can I use Selenium with C#?

Yes, you can use Selenium with C#. Selenium enables you to write automated tests in C#.

What is Visual Studio IDE?

Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It provides rich development and testing experience with Intellisense, multiple programming language support, and easy debugging. 

Using Visual Studio one can easily set up all configurations required for development and testing, write and run tests, and get instant results. This tool is quite easy to set up and use, even for beginners.

Visual Studio Community Version is completely free. One can download and enjoy the features of Visual Studio IDE seamlessly. 

Getting Started with Selenium and C#

Before running a test with Selenium and C#, we need to complete the basic prerequisites.

  1. Install Visual Studio
  2. Create Selenium C# automation framework .NET Core 5.0 from scratch using Visual Studio
  3. Add Chromedriver to Selenium C# Project using Visual Studio
  4. Add GeckoDriver to Selenium C# Project

How to Install Visual Studio

Visual Studio IDE is available in both free and paid versions. The community version of Visual Studio is free. 

  • Download Visual Studio Community Edition from Microsoft official website.

  • Click on Free Download, and the .exe file will be downloaded.
  • Click on the .exe file to start the Visual Studio Installer.
  • Click on the installer file, the Privacy and License Terms window appears, click on Continue to start installation.
  • Visual Studio Installer downloads and prepares Visual Studio for installation.
    Note: This is Visual Studio Installer, not the Actual Visual Studio
  • Once the Preparation is complete, the Visual Studio installation window appears. Users will have to choose a set of tools. To build an automation framework one can choose the and Web development option. Click on Install to proceed.
  • Installation begins and its progress percentage will be shown accordingly.
    Note: The installation takes some time depending on the internet speed.
  • Once the installation is complete, the Restart pop up window appears. Click on Restart.
  • After a successful restart of the system, the Visual Studio setup pop up will appear. Click on Not now, Maybe later.Note: Sometimes, after restart the above window doesn’t appear. If it doesn’t appear automatically just Open Visual Studio from the Windows Start Menu.
  • Choose a Theme for Visual Studio, and click on Start Visual studio.
  • Visual Studio prepares settings for First Use.
    Note: This stage takes some time to finish.
  • Once the above step is completed, the Get Started Window opens.
  • Visual Studio Installation is complete.

How to create Selenium C# automation framework .NET Core 5.0 from scratch using Visual Studio

  • Open Visual Studio from the Windows Start Menu.
  • Click on Create New Project.
  • Search for NUnit Template. From the Search Results choose C# NUnit Test Project (.NET core).Note: Selenium C# project can be created using MSTest libraries as well, but Nunit is most widely used with C# and Selenium.
  • In the Configuration Section, enter the required details:

Project Name: Any Name (ex: SeleniumCsharp)
Location: Desired Location (Ex: C:/MyFolder)
Solution Name: Any Name (Ex: SeleniumCsharp)
After entering the details, click Next.

  • In the additional Information section, chooseTarget Framework : .NET 5.0 (Current)Click on Create.
  • After clicking Create, Visual Studio creates Selenium C# framework with NUnit Test Runner.
    The default framework contains dependencies for NUnit and NUnit Test Adapter. One file with .cs extension will be created by default i.e UnitTest1.cs.
  • Add Selenium Dependencies for the project.To run Selenium tests using C# and NUnit, add Selenium dependencies:- Click on the Tools Menu– Click on NuGet Package Manager
    – Click on Manage NuGet Package for Solution
  • Search for Selenium Webdriver and add the package.In the NuGet Package Explorer window:– Click on Browse,
    – In the Search box type Selenium
    – Choose Selenium.Webdriver by Selenium
    – On the window to the right, select the checkbox which shows Project Name
    – Click on Install
  • If it prompts for the License Acceptance Window, click on Accept.
  • Wait for the installation to finish.
  • Install the Selenium Support Package from NuGet:
    – Navigate to NuGet Package Manager (Tools > NuGet Package Manager > Manage NuGet Package for Solution)
    – Type Selenium Support
    – Click on Selenium Support from Search Results
    – Choose the Project Name checkbox (the current project)
    – Click on Install
  • Wait for the installation to finish.
  • Once Selenium Webdriver and Selenium Support NuGet package is installed one should see the dependencies added in the project:

All required dependencies have been added for a Selenium C# project.

In order to execute tests, one must have the desired browser driver. For example, to run tests on Chrome, testers need Chromedriver. To run tests on Firefox they need GeckoDriver, etc.

How to Add Chromedriver to Selenium C# Project using Visual Studio

  • Navigate to Solution Explorer in Visual Studio.
  • Right click on the Project (ex: SeleniumCsharp)
    Note: Right Click on the Project Name, not on the Solution Name.
  • Click Add.
  • Click New Folder.
  • Name the new folder drivers.
  • Download ChromeDriver by navigating to the Chromium webpage.
  • Click on ChromeDriver <XX.XX.XXX>.
  • A new web page will open up and ask for the target Operating System (Windows, in this case).
  • Click on the right option and ChromeDriver will be downloaded as a Zip file.


  • Extract the zip folder to the desired project location: the previously created folder drivers, in this case. To do that, simply right click on the drivers folder in Visual Studio, and choose Open Folder in Explorer.

This will open the driver folder in Windows File Explorer. Now, the ChromeDriver zip file is downloaded to the required location, extract the ChromeDriver zip here.

Alternatively, right click on zip file > Click on Extract All > Specify folder location. 

After extraction, the drivers folder should contain chromedriver.exe.

How to Add GeckoDriver to Selenium & C# Project using Visual Studio

Note: This step is required to run tests on Firefox.

  • Navigate Gecko driver Download web page:
    – Navigate to Mozilla Github
    – Click on Releases under Downloads


  • Choose the correct platform for GeckoDriver and download.
    – In the Releases page, scroll down to the Assets section.
    – Choose the desired GeckoDriver for the Operating System being used (Windows, in this case).
  • After completion of Step 2, the zip file containing geckodriver.exe will be downloaded.
  • Extract the GeckoDriver to the drivers folder created earlier.Note: Step 4 is similar to Chromedriver download. Simply extract geckodriver.exe and place it in drivers folder.After completing this step, the drivers folder should contain two .exe files namely chromedriver.exe and geckodriver.exe.

How to write the first test using Selenium, C# and Nunit using Visual Studio IDE

In the existing project, there already exists a file created by default named UnitTest1.cs. Rename that to SeleniumTest.cs (for easy readability).

Double click on the SeleniumTest.cs file and write a few tests. 

In this case, 3 tests are being created.

  • Test 1: Navigates to the BrowserStack homepage and verifies the company logo.
  • Test 2: Verifies for all menu items count on the BrowserStack homepage.
  • Test 3: Navigates to the BrowserStack pricing section and verifies the header.

BrowserStack Automate Banner 8

Source Code: SeleniumTest.cs

//Inside SeleniumTest.cs

using NUnit.Framework;

using OpenQA.Selenium;

using OpenQA.Selenium.Chrome;

using OpenQA.Selenium.Firefox;

using System;

using System.Collections.ObjectModel;

using System.IO;

namespace SeleniumCsharp


    public class Tests


        IWebDriver driver;


        public void Setup()


            //Below code is to get the drivers folder path dynamically.

            //You can also specify chromedriver.exe path dircly ex: C:/MyProject/Project/drivers

            string path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;

            //Creates the ChomeDriver object, Executes tests on Google Chrome

            driver = new ChromeDriver(path+@"\drivers\");

            //If you want to Execute Tests on Firefox uncomment the below code

            // Specify Correct location of geckodriver.exe folder path. Ex: C:/Project/drivers

            //driver= new FirefoxDriver(path + @"\drivers\");



        public void verifyLogo()






        public void verifyMenuItemcount()


            ReadOnlyCollection<IWebElement> menuItem = driver.FindElements(By.XPath("//ul[contains(@class,'horizontal-list product-menu')]/li"));

            Assert.AreEqual(menuItem.Count, 4);



        public void verifyPricingPage()



            IWebElement contactUsPageHeader = driver.FindElement(By.TagName("h1"));

            Assert.IsTrue(contactUsPageHeader.Text.Contains("Replace your device lab and VMs with any of these plans"));



        public void TearDown()






The tests have been created, as seen above.

How to run Selenium C# Tests with Example

  • Open Test Explorer window. Click on Test Menu. Click on Test Explorer. New tab named Test Explorer opens up on the left of Visual Studio IDE

  • Click on Run All in the Test Explorer window

  • Tests start executing.
  • Once all your tests finish executing, the results will be marked as shown below:

Talk to an Expert

How to Execute C# Selenium Tests on Firefox

In the source code above under SeleniumTest.cs, ChromeDriver is created in the following line:

driver = new ChromeDriver(path+@"\drivers\");

Now, to run the tests on Firefox, one must create FirefoxDriver Object. To do so, simply comment the Chrome Driver code and uncomment the FirefoxDriver code, as below:

driver= new FirefoxDriver(path + @"\drivers\");

Note: For FirefoxDriver object the code is taking a dynamic path. But alternatively, one can directly specify the path to geckodriver.exe.

driver = new FirefoxDriver(@'C:/MyProject/Project/driver');

The rest of the test execution steps remain the same. 

Now that a Selenium & C# framework has been created using Visual Studio, it can be enhanced to meet the specific needs of teams and organizations. Bear in mind, however, that  Selenium WebDriver tests must be executed on real devices and browsers. Remember that device fragmentation is a major concern for every developer and tester. Every website has to work seamlessly on multiple device-browser-OS combinations. With 9000+ distinct devices being used to access the internet globally, all software has to be optimized for different configurations, viewports, and screen resolutions.

In this state, no emulator or simulator can replicate real user conditions. Software needs to be tested on real devices so that they can work in real-world circumstances such as a low battery, incoming calls, weak network strength, and so on. If an in-house lab is not accessible, opt for a cloud-based testing option that offers real devices.

BrowserStack’s cloud Selenium grid offers 2000+ real devices and browsers for automated testing. That means users can run tests on multiple real devices and browsers by simply signing up, logging in, and selecting the required combinations. Testers can also conduct Cypress testing on 30+ real browser versions across Windows and macOS. Detects bugs before users do by testing software in real user conditions with BrowserStack.

Run Selenium Tests on Real Browsers 

Frequently Asked Questions

1. What is Selenium C# used for?

Selenium C# is used for automating web application testing. It allows developers and testers to write scripts in C# language to simulate user interactions with a web application, test its functionality, and verify that it behaves as expected. With Selenium C#, you can automate repetitive tasks, catch and debug errors early, and ensure the quality and reliability of your web application.

2. Which is better Selenium with Java or C#?

The choice between using Selenium with Java or C# primarily depends on your background, skills, and personal preferences. In terms of performance and capabilities, both Java and C# are equally good.

Java is a widely used programming language and has a large community of developers and testers who have created many resources and libraries that can be leveraged when using Selenium with Java. Java also has a long history of being used for automation testing, which has led to the creation of many testing frameworks and tools that integrate well with Selenium.

C#, on the other hand, is a modern and expressive language that is well-suited for Windows development. It is the primary language for .NET development, and the .NET ecosystem provides a wealth of tools and libraries that can be used in combination with Selenium C#.

3. Why is C# used for testing?

C# is used for testing because it is a modern, statically-typed, object-oriented programming language. Its syntax is easy to read and understand, making it a good choice for writing tests that need to be maintained over time. It provides a comprehensive set of features for creating automated tests. Additionally, C# has a rich set of libraries and frameworks, such as NUnit, that can be used to write tests for a wide range of applications, from desktop and web applications to games and mobile apps.

4. Which framework is best for Selenium C#?
NUnit Framework is the best for Selenium C#. NUnit provides a rich set of features for organizing and structuring tests, making it easy to write and maintain automated tests. Additionally, it supports data-driven testing and parallel test execution, which can help to significantly reduce the time taken to run a suite of tests.
Automation Testing Selenium Selenium Webdriver

Featured Articles

Selenium Automation Framework: A Detailed Guide

Learn Selenium with Java to run Automated Tests

Selenium C# Testing on Real Devices

Run Selenium C# Tests on Real Devices & Browsers for accurate test results