In this section, we will learn how to create a Selenium test script in the C# programming language with the help of Visual Studio and NUnit Framework.
In this tutorial, we will learn the following topics:
Selenium is one of the most important automation testing tools because it supports multiple programming languages like Java, Python, C#, Ruby, Perl, and PHP, etc.
And to automate the test scripts in various browsers like Google Chrome, Firefox, Safari, Internet Explorer, and Opera on different operating systems such as Windows, Mac, Linux.
Selenium supports other testing tools such as TestNG and JUnit to manage the test case and to generate the test reports.
For more detailed about Selenium, refers to the below link:
https://www.rookienerd.com/selenium-tutorial
C# is an object-oriented programming language, which runs on the .Net framework, and it is pronounced as C-sharp.
C# language is developed to run the CLR, which stands for Common Language Runtime.
In C#, we can break the program into parts with the help of functions; that's why it is also called a structured programming language.
For further details about C# language, refer to the below link:
https://www.rookienerd.com/c-sharp-tutorial
In this section, we will understand how to download, install the Visual Studio and configure Selenium with Visual Studio, NUnit Framework and execute the test scripts in Visual Studio using C# programing language.
To use selenium with C#, follow the below process:
Following are the process, to configure Selenium with visual studio in C#:
Visual Studio is a C# IDE [Integrated Development Environment], which is used to develop applications on various platforms like Windows, Mac, etc.
Here, we are downloading and installing the Visual Studio for Windows platform.
To download the latest version of Visual Studio for Windows platform, refer to the below link: https://visualStudio.microsoft.com/downloads/
After downloading the Visual Studio for Windows platform, we will be ready to install it.
To install the Visual Studio, follow the below process:
Once the installation is done, we are ready to create a new project on the Visual Studio.
To create a project on the Visual Studio, follow the below process:
Once the project creation is done, we will add the References of Selenium WebDriver and Chrome driver with the help of the NuGet Package Manager in the Visual Studio.
Follow the below process, to add references of Selenium WebDriver in Visual Studio:
Follow the below process, for adding the references to the Chrome Driver in Visual Studio:
To write a Selenium test script using C# Programming language, follow the below steps:
| Steps | Actions | Input | Expected Result |
|---|---|---|---|
| 1. | Create reference for the browser | The reference for the browser should be created. | |
| 2. | Navigate to the Google home page. | https://www.google.com/ | The Google home page must be displayed. |
| 3. | Identify the Google search text box and pass the value. | rookienerd tutorials | The Google search box should be identified, and the value should be entered. |
| 4. | Identify and Click on the Google search button. | The Google search button should be identified, and clicked. | |
| 5. | Close the Browser. | The Browser should be closed. |
We are creating our test scripts step by step to give you a complete understanding of each component in detail.
Step1
To create a reference for the browser, we will follow the below process:
| C# | Java |
|---|---|
| IWebDriver | WebDriver |
| IWebElement | WebElement |
Here the sample code:
IWebDriver driver = new ChromeDriver();
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
Step2
In this step, we are going to navigate https://www.google.com/ URL in the browser.
And the sample code is as below:
driver.Navigate().GoToUrl("https://www.google.com/");Step3
To identify the Google search box, follow the below process:
In this step, we are trying to locate the Google search text box with the help of its Name attribute.


IWebElement ele = driver.FindElement(By.Name("q"));Step4
In this step, we are passing the value in the Google search box.
Here the sample code:
ele.SendKeys("rookienerd tutorials");Step5
To identify the Google search button, follow the below process:
IWebElement ele1 = driver.FindElement(By.Name("btnK"));
//click on the search button
ele1.Click();Step6
In the last step, we will close the browser after the completion of all the actions performed on the browser.
Here the sample code:
driver.Close();
Our final test script will look like this after completing all the test scenarios.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
Console.Write("test case started ");
//create the reference for the browser
IWebDriver driver = new ChromeDriver();
// navigate to URL
driver.Navigate().GoToUrl("https://www.google.com/");
Thread.Sleep(2000);
// identify the Google search text box
IWebElement ele = driver.FindElement(By.Name("q"));
//enter the value in the google search text box
ele.SendKeys("rookienerd tutorials");
Thread.Sleep(2000);
//identify the google search button
IWebElement ele1 = driver.FindElement(By.Name("btnK"));
// click on the Google search button
ele1.Click();
Thread.Sleep(3000);
//close the browser
driver.Close();
Console.Write("test case ended ");
}
}
}Once we are done with writing the Selenium test script, we will run our test script.
Follow the below process, to run the test script:
Before writing the test script with the help of NUnit, we will understand NUnit Framework, downloading and installing the NUnit Framework in Visual Studio
NUnit is a unit testing framework for all .Net languages. It is open-source software, and it has been completely redesigned to take advantage of many new features .Net language.
It also supports a wide range of .Net languages platforms.
The NUnit3 Test Adapter is an extension that allows us to run the NUnit test inside the Visual Studio.
To download the NUnit3 Test Adapter in the Visual Studio, follow the below steps:
To add the reference for NUnit, follow the below process:
To create a NUnit class in the Visual studio, follow the below process:
To write a Selenium test scripts using NUnit, follow the below process:
For our testing purpose, we will perform Login operation on the Facebook Application.
| Steps | Actions | Input | Expected Result |
|---|---|---|---|
| 1. | Open the Google Chrome browser. | The Google Chrome browser should be opened. | |
| 2. | Navigate to the Facebook login page. | https://www.facebook.com | The Facebook login page must be displayed. |
| 3. | Identify the username text box and pass the value. | Username=xyz11@gmail.com | The username text box should be identified, and the value should be entered. |
| 4. | Identify the password text box and pass the value. | Password=####### | The Password text box should be identified, and the value should be entered. |
| 5. | Identify the Log in button and click it. | The Login button should be identified and clicked. | |
| 6. | Close the browser. | The browser should be closed. |
We are creating our test script step by step to give you a complete understanding of how we write the test script with the help of the NUnit framework in Visual studio.
Step1
To access the Google Chrome browser, we will create the IWebDriver as a global variable.
Here the sample code:
//create the reference for the browser IWebDriver driver = new ChromeDriver();
We will divide our code into different parts while writing the code using NUnit with the help of NUnit test methods like:
The syntax for the NUnit test method: public void MethodName()
Example:
public void Initialize()
{
//open the browser
}
public void ExecuteTest()
{
//perform browser operations
}
public void EndTest()
{
//close the browser
}Step2
In the next step, we will navigate to the given URL under the Initialize() method.
Here the sample code:
public void Initialize()
{
//navigate to URL
driver.Navigate().GoToUrl("https://www.facebook.com/");
//Maximize the browser window
driver.Manage().Window.Maximize();
Thread.Sleep(2000);
}Step3
In this step, we will identify the username text box of the Facebook login page under the ExecuteTest() method.
Follow the below process:
Here the sample code:
public void ExecuteTest()
{
//identify the username text box
IWebElement ele = driver.FindElement(By.Id("email"));
//enter the username value
ele.SendKeys("xyz11@gmail.com");
Thread.Sleep(2000);
Console.Write("username value is entered \n");Step4
After that, we will identify the password text box of the Facebook login page, so for this follow the below process:
Here the sample code:
//identify the password text box
IWebElement ele1 = driver.FindElement(By.Name("pass"));
//enter the password value
ele1.SendKeys("########");
Console.Write("password is entered");Step5
Once we identified the username or password textbox, we will find the Log in button and perform click operation.
Here the sample code:
//click on the Log in button
IWebElement ele2 = driver.FindElement(By.Id("u_0_b"));
ele2.Click();
Thread.Sleep(3000);
Console.Write("login button is clicked")Step7
In the last step of our test script, we will close the browser under the EndTest() method.
Here, the sample code for closing the browser:
public void EndTest()
{
//close the browser
driver.Close();
}After combining all steps together, our script will look like this:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace SeleniumTest
{
class Sample1
{
//create the reference for the browser
IWebDriver driver = new ChromeDriver();
public void Initialize()
{
//navigate to URL
driver.Navigate().GoToUrl("https://www.facebook.com/");
//Maximize the browser window
driver.Manage().Window.Maximize();
Thread.Sleep(2000);
}
public void ExecuteTest()
{
//identify the username text box
IWebElement ele = driver.FindElement(By.Id("email"));
//enter the username value
ele.SendKeys("xyz11@gmail.com");
Thread.Sleep(2000);
Console.Write("username value is entered");
//identify the password text box
IWebElement ele1 = driver.FindElement(By.Name("pass"));
//enter the password value
ele1.SendKeys("########");
Console.Write("password is entered");
//click on the Login button
IWebElement ele2 = driver.FindElement(By.Id("u_0_b"));
ele2.Click();
Thread.Sleep(3000);
Console.Write("login button is clicked");
}
public void EndTest()
{
//close the browser
driver.Close();
}
}
}To run the test scripts, follow the below process:
[Test] public void ExecuteTest()
Our final code will look like this after adding all three attributes:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using NUnit.Framework;
namespace SeleniumTest
{
class Sample1
{
//create the reference for the browser
IWebDriver driver = new ChromeDriver();
[SetUp]
public void Initialize()
{
//navigate to URL
driver.Navigate().GoToUrl("https://www.facebook.com/");
//Maximize the browser window
driver.Manage().Window.Maximize();
Thread.Sleep(2000);
}
[Test]
public void ExecuteTest()
{
//identify the username text box
IWebElement ele = driver.FindElement(By.Id("email"));
//enter the username value
ele.SendKeys("xyz11@gmail.com");
Thread.Sleep(2000);
Console.Write("username value is entered \n");
//identify the password text box
IWebElement ele1 = driver.FindElement(By.Name("pass"));
//enter the password value
ele1.SendKeys("########");
Console.Write("password is entered \n");
//click on the Login button
IWebElement ele2 = driver.FindElement(By.Id("u_0_b"));
ele2.Click();
Thread.Sleep(3000);
Console.Write("login button is clicked \n");
}
[TearDown]
public void EndTest()
{
//close the browser
driver.Close();
}
}
}