top of page

Selenium with C# : How to start running Automated Tests

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.

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.

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 , Visual Studio creates Selenium C# framework with NUnit Test Runner.additional Information section, choose Target Framework : .NET 5.0 (Current)Click on Create.

  • After clicking Create .cs extension will be created by default i.e The default framework contains dependencies for NUnit and NUnit Test Adapter. One file with 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: (Tools > NuGet Package Manager > Manage NuGet Package for Solution) checkbox (the current project) — Navigate to NuGet Package Manager — Type Selenium Support - Click on Selenium Support from Search Results - Choose the Project Name- 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.

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.

This step is required to run tests on Firefox.

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.

  • 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.

Source Code: SeleniumTest.cs

In this case, 3 tests are being created.

//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     

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

            string path = Directory.GetParent

            //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 = 
           list product-menu')]/li"));

            Assert.AreEqual(menuItem.Count, 4);
        public void verifyPricingPage()

            IWebElement contactUsPageHeader = 
           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

  • Tests start executing.

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

  • Tests start executing.

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

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.

Source: Medium - Ganesh Hegde

The Tech Platform

bottom of page