Selenium with C#: How to Start Running Automated Tests

Friday, February 14, 2025

Selenium is a popular library for web automation testing. It supports multiple programming languages, and using Selenium with C# for testing offers additional benefits, such as improved test quality and seamless integration within development projects. 

Every QA expert and reliable software testing company should know how to run Selenium C# tests. Therefore, here we provide a detailed guide for the same explaining the importance of C# for automated testing, how to set up your tests, and how to execute them using Visual Studio Code. So, let’s get started.

1. Selenium Overview

Selenium is a popular web automated testing suite. It is an open-source tool used to automate the testing of web functionalities across different browsers. Selenium is compatible with all major browsers, such as Internet Explorer, Google Chrome, Mozilla Firefox, Safari, etc. 

It also allows you to write test scripts and automate them in browsers using various programming languages like PHP, C#, Perl, Java, Ruby, Python, etc. The versatility of Selenium goes even one step further. 

Its browser automation capabilities are primarily useful for web app testing. So, it can’t support the testing of mobile and desktop applications. Although it’s a testing tool, Selenium doesn’t provide any reporting features either. To address these limitations, Selenium allows integration with other testing tools, including JUnit and TestNG. 

Selenium Trends on GitHub
Stars: 31.2k stars
Forks: 8.3k forks
Watching: 1.3k

2. C# Overview

C# is an object-oriented, modern programming language. Developers can use it to create a variety of applications on the .NET platform. If you are familiar with any C-based language like C, C++, Java, or JavaScript then using C# will be easy because they all share similar roots. 

C# is a structured programming language that runs on the Common Language Runtime (CLR). It helps break a program into smaller sections using functions. C# offers a wide range of features, including polymorphism, inheritance, and encapsulation. 

Supporting type-safe programming, C# enables programmers to create robust and reliable .NET applications. Developers can also use it to build console applications, as well as Windows and web-based apps. 

3. Why is C# Used For Automation Testing?

Here are a few reasons why you should use C# for automation testing: 

  • C# has a simple syntax that is easy to read and understand, making it a suitable choice for writing and maintaining tests. 
  • C# offers a rich set of frameworks and libraries that help create automated tests. 
  • The NuGet package manager for C# provides numerous packages that make automation testing easier. 
  • C# provides a wide range of built-in features and functionalities that enhance the development and debugging process.

4. Getting Started with Selenium and C#

Install the Visual Studio Code on your system and open it from the Windows Start menu. once it opens, click on the “Create New Project” option.

You need to choose a suitable template for your new project. So, go to the browse tab and search for the NUnit template. From the results, select the C# NUnit test project (.NET Core). 

Normally, you can use MSTest libraries to create a Selenium C# project but NUnit is the most widely used option for Selenium testing with C#. Add appropriate details in the configuration section to provide additional information about your newly created project. 

Project Name: TatvaSeleniumCsharp
Location:D:\Automation Projects\
Solution Name: TatvaSeleniumCsharp

Once you insert details, click on the Next button.

In the input field for the target framework, enter .NET 8.0 (Current) and click on the Create button.  

Once you click “Create”, Visual Studio will build a Selenium C# framework with an NUnit Test runner. It comes with built-in dependencies for the NUnit and NUnit test adapter. The framework will also create a file with a .cs extension by default. For example, the file named Unittest1 will be viewed as Unittest1.cs. 

To move ahead with the Selenium tests using C# and NUnit, it is important to add Selenium dependencies. We can do so by opening the Tools menu, clicking on Nuget Package Manager, and selecting the “ Manage NuGet Packages for Solution” option. 

Open the NuGet Package Explorer window and navigate to the browser tab. Search for “Selenium” and select “Selenium.Webdriver” by Selenium from the results. On the right side of the NuGet Package Explorer window, you will find a list of your different projects. Click on the checkbox next to the name of your newly created project, and then click on “Install”. 

If it prompts you to use a License Acceptance Window, then click on Apply and wait for installation to finish. 

The next step would be to go to NuGet Package Manager in Tools and click on “Manage NuGet Package for Solution”. Search for “Selenium Support” and click on it from the given results. On the right side of the window, select the checkbox with your project name. In the end, click the Install button and wait till it is installed.

Once you install the Selenium WebDriver and Selenium Support NuGet Package, you will see the previously added dependencies in your Selenium with C# project.

5. How to Run Selenium C# Tests?

Before running tests on a local grid, you need the following:

1. To enable your test code to interact with WebElements on the page, add Selenium.WebDriver Nuget package. It offers different methods for performing interactions. 

A. Follow the process given below to add a NuGet package to your project.
Go to Solution Explorer and then right-click on the project name. Selecting the option of “Manage NuGet packages” will open a panel showing the packages already included in your project and the ones you can install.

B. Now, go to the browse tab and search for Selenium. From the results, select the option for Selenium WebDriver. You can see a detail panel on the right side. Select an appropriate option from the Version drop-down and click on the Install button.

2. As all the tests are conducted in a browser, we need to add a suitable browser driver. In this case, we will be adding ChromeDriver for the Chrome browser. You can easily add it as a NuGet package by following the steps shown below:

A. Before adding the Selenium.WebDriver.ChromeDriver, ensure that the driver is of the same version as your system’s browser. 

B. If you are using any other browser of your choice then pick a relevant driver for it and add it to your project in the same way. When using Microsoft Edge, you will need to add Selenium.WebDriver.MSEdgeDriver and Selenium.WebDriver.GeckoDriver for Mozilla Firefox. 

Here, we are going to use the demo website for our automation test scenario. 

Test Scenario: 

  1. Go to the Demo Automation Website Page
  2. Enter your login credentials in the input field. 
  3. Click on the Login button. 
  4. Validate that you have the correct username.

Test Execution: 

Now, it’s time to start our Selenium with C# test. We will rename the class created earlier to tests and use it here.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace NUnitTestProject
{
public class Tests
{
IWebDriver driver;
[SetUp]
public void Setup()
{
// Setup ChromeDriver
driver = new ChromeDriver();
}
[Test]
public void Test1()
{
// Navigate to URL
driver.Navigate().GoToUrl("https://practice.automationtesting.in/");
// Maximize browser window
driver.Manage().Window.Maximize();
// Click on the My Account
driver.FindElement(By.XPath(".//*[text() = 'My Account']")).Click();
// Enter Username
driver.FindElement(By.Id("username")).SendKeys("demotestuser@mailinator.com");
// Enter Password
driver.FindElement(By.Id("password")).SendKeys("DemoTest#@11");
//Click on Login button
driver.FindElement(By.XPath(".//*[@name = 'login']")).Click();
// Verify Username
String Username = driver.FindElement(By.XPath("//*[@id='page-36']/div/div[1]/div/p[1]/strong")).Text;
Assert.AreEqual("demotestuser", Username);
}
[TearDown]
public void TearDown()
{
driver.Quit();
driver.Dispose();
}
}
}

Code Explanation:

All the necessary packages are imported into the main test file before initiating the test class. The next step is to create a new instance of IWebDriver. Here, we are using the Setup method to instantiate the browser driver.

Now, for the real test, we will use NUnit’s [Test] attribute mentioned earlier. The steps of the test methods are as shown below: 

With the help of Navigate() GoToUrl method, we move to the URL in Selenium with C#. Use the FindElement() method to locate page elements for interaction. However, it will be quicker to use an ID locator. 

Similarly, the SendKeys() method can be used for entering text, the Click() method is used for clicking buttons, and the Assert IsTrue() method to check whether everything is functioning as expected. In case of errors, it also allows you to add customized text for easy debugging. Lastly, the [TearDown] attribute when used with the quit() method, enables you to close the browser once the test is complete. 

Test Execution:

After preparing the test, you can create the project to run the test on your local system. Go to the project name and right-click to select “Build”. You can also use a Ctrl+B shortcut. You should now see your test in the Test Explorer. If the test runs without any issues and it passes, the system will close the browser window at the end.

6. Conclusion

This guide shows you how to set up and start using Selenium with C# for test automation. We learned why it is important to use C# for automated testing and covered a step-by-step process for Selenium C# tests. Contact us in case you have any queries or are looking for reliable software testing experts. 

Thanks for reading! 

FAQs 

Which is better Java or C# for Selenium?

Java is a better option when working on large, enterprise-level projects that demand high scalability and performance. However, if Microsoft is your targeted platform, then working with C# is the best because it works seamlessly with the entire .NET ecosystem. 

Is C# good for automation testing?

C# is a popular choice among developers and automation testing companies because it provides a comprehensive set of testing frameworks and libraries and it is easy to use.

Comments


Your comment is awaiting moderation.