All IT Courses 50% Off
Selenium Tutorials

Selenium C# Webdriver: Nunit

Selenium Overview:

Selenium is a web Automation open-source Testing tool supporting multiple browsers and multiple operating systems. It also allows testers to use multiple programming languages like Java, C#, Python, .Net, Ruby, PHP, and Perl for coding automated tests. Selenium supports other testing tools like TestNG and JUnit to manage the test case and generate the test reports.

C # Overview:

C# is an object-oriented programming language, which is derived from C++ and Java. C# also allows developers to build applications using Visual Studio on the .Net platform. The below are the key features of C#.

  • It is an Object-Oriented programming language.
  • It also supports the development of console, windows, and web-based applications.
  • It also provides features such as Encapsulation, Inheritance, and Polymorphism.

How to download and install Visual Studio?

Visual Studio (VS) is a C# IDE (Integrated Development Environment) used to develop applications on various platforms like Windows, Mac, etc.

Here, we are downloading and installing Visual Studio for the Windows platform.

For downloading the latest version of Visual Studio for Windows platform, refer to the below link: https://visualStudio.microsoft.com/downloads/.

All IT Courses 50% Off

Once we have clicked on the above link, in the Downloads section, we will then click on the Free Download button available under the Community area, as shown in the below screenshot:

Selenium with C# Tutorial

Once we have clicked on the Free Download button, it will download the visual Studio’s exe file.

After that, open the .exe file as shown in the below screenshot:

Selenium with C# Tutorial

After downloading the Visual Studio for the Windows platform, we will be ready to install it.

For installing the Visual Studio, follow the below steps:

Once we have double-clicked on the downloaded executable file, the Visual Studio Installer window will then appear on the screen, where we can click on the Continue button for further process, as shown in the below screenshot:

Selenium with C# Tutorial

After clicking on the continue button, it start installing the files for the Visual Studio.

Selenium with C# Tutorial

Once the above process is done, installing -visual Studio-community 2019-16.4.3 window will appear on the screen. We can select the Universal Windows Platform development and .Net desktop development options available under the Desktop & Mobile section.

And click on the Install button as shown in the below screenshot:

Selenium with C# Tutorial

The installation process takes some time to install because of the bigger file size, as shown in the below screenshot:

Selenium with C# Tutorial

When the installation process is complete, we will click on the Restart button first before starting the visual Studio.

Selenium with C# Tutorial

Once the system is reboot/restart, we will now search “Visual Studio 2019” on the Start menu and then double-click on it.

The below pop-up will appear on the screen, and we click on the Not now, maybe later when we do not have an account, as shown in the below screenshot:

Selenium with C# Tutorial

In the next window, we will select the theme for the Visual Studio.

After selecting the theme, we will click on the Start Visual Studio button as shown in the below image:

Selenium with C# Tutorial

Now we got the Get started window of Visual Studio.

Selenium with C# Tutorial

How to create a new project in the Visual Studio?

Once the installation is done, we are now ready to create a new project on Visual Studio.

To create a project on Visual Studio, follow the below process:

  1. Right-click on the File menu, go to the New, and select the Project option in the given pop-up menu, as shown in the below screenshot:
Selenium with C# Tutorial
  1. A window named Create a new project will appear on the screen, where we will select C# language in the All language drop-down menu.
  2. Then, select Windows platform options from the All platforms drop-down list.
  3. And search for the .net Framework template in the search field, and select the Console App (.NET Framework) from it.
  4. Then click on the button Next, as shown in the below screenshot:
Selenium with C# Tutorial

Once we have clicked on the next button, Configure your new project window will appear on the screen, where we provide our Project name [SeleniumTest], and clicking on the Create button as observed in the below screenshot:

Selenium with C# Tutorial

Once the project is created successfully, the below window will appear on the screen:

Selenium with C# Tutorial

How to set up Visual Studio with Selenium WebDriver?

Once the project creation is complete, we will then add the References of Selenium WebDriver and Chrome driver with the NuGet Package Manager’s help in the Visual Studio.

Add Selenium WebDriver References

Follow the below steps to add references of Selenium WebDriver in Visual Studio:

In the Solution Explorer window, right-click on the References and select the Manage NuGet Packages option in the given pop-up menu, as shown in the below screenshot:

Selenium with C# Tutorial

The window Manage NuGet Packages will open, where we will search Selenium and select the WebDriver, and click on the Install button as shown in the below image:

Selenium with C# Tutorial

Then, click on OK button in the window Preview change, as shown in the below image:

Selenium with C# Tutorial

Once the package got installed successfully, it will give the below output message.

Selenium with C# Tutorial

Add Chrome Driver References

Follow the below steps for adding the references to the Chrome Driver in Visual Studio:

For performing an action on the chrome browser, we will again right-click on the References and select the Manage NuGet Packages option from the pop-up menu.

Then search chrome driver in search field, and select the Chrome.WebDriver and click on Install button as shown in the below image:

Selenium with C# Tutorial

And, then click on the Ok button in the Preview Changes window for the further process.

Selenium with C# Tutorial

What is NUnit Framework?

NUnit is a unit test framework for all the .Net languages which is open-source software, and it has been completely designed to take advantage of many new features of .Net language.

It supports a wide range of .Net languages platforms.

Download the NUnit3 Test Adapter:

The NUnit3 Test Adapter is an extension allowing us to run the NUnit test inside the Visual Studio.

For downloading the NUnit3 Test Adapter in the Visual Studio, follow the below steps:

First, go to the Extensions and select Manage Extensions option present in the Visual Studio.

Selenium with C# Tutorial

The window Manage Extensions will appear on the screen, where we can search for NUnit and then click on the Download button for NUnit 3 Test Adapter, as shown in the below screenshot:

Selenium with C# Tutorial

Once we have clicked on the Download button, it will ask us to close our Visual Studio for modifying the changes, and then click on the Modify button, as shown in the below screenshot:

Selenium with C# Tutorial

Once we click on the Modify button, it starts installing and modifying the NUnit 3 Test Adapter.

When all the modifications are completed successfully, we got the below window:

Selenium with C# Tutorial

As we can now see in the Manage Extensions window, the NUnit 3 Test Adapter is successfully installed.

Selenium with C# Tutorial

Once done with the NUnit 3 Test Adapter installation process, we will add the references for NUnit.

Adding the References for NUnit

For adding the reference for NUnit, follow the below steps:

Right-click on References now in the Solution Explorer, and select the Manage NuGet Packages option in the pop-up menu.

Selenium with C# Tutorial

In the window Manage NuGet packages, we will then search for NUnit in the search field, and select the very first option as NUnit, and click on the Install button as shown in the below screenshot:

Selenium with C# Tutorial

Once we click on the Install button, Preview changes window will appear on the screen where we have clicked on the OK.

Selenium with C# Tutorial

Once the package got installed successfully, we will get the below output message.

Selenium with C# Tutorial

Selenium and NUnit framework:

Integration of selenium webdriver with the NUnit framework allows a tester to differentiate between the various test classes. NUnit also allows testers to use annotations like SetUp, Test, and TearDown to perform actions before and after running the test case.

NUnit framework can be integrated with the Selenium by creating an NUnit test class and running the test class using the NUnit framework.

Below are the steps required to create and run a test class using the NUnit framework.

  1. Right-click on the project SeleniumTest, then go to Add and select the Class option from the given pop-up menu.
Selenium with C# Tutorial
  1. The window Add New Item will appear on the screen, where we provide the class name as Sample1, and then click on the Add button as shown in the below screenshot:
Selenium with C# Tutorial
  1. Once we clicked on the Add button, the below window will appear on the screen:
Selenium with C# Tutorial
  1. After successfully creating the class, we are ready to write our test script using the NUnit. 

Step 1: 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 the code into different parts while writing the code using NUnit with the help of NUnit test methods like:

  • For Open the browser: Initialize
  • Perform browser operations: ExecuteTest
  • Close the browser: EndTest

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  
} 

Step 2: In the next step, we will navigate 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);  
} 

Step 3: In next step, we will now identify the username text box of the Facebook login page under the ExecuteTest() method.

Follow the below steps:

  • Right-click on the text box username, and click on the Inspect Element, as shown in the below screenshot.
Selenium with C# Tutorial
  • The developer tool window will open all the specific codes used in the development of the username text box.
Selenium with C# Tutorial
  • Copy the value of id attribute as “email” and paste it.

Here is 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");  

Step 4: After that, we will now identify the password text box of the Facebook login page, so for this follow the below steps:

  • Right-click on the text box password, and click on the Inspect Element option in the pop-up menu as shown in the below image:
Selenium with C# Tutorial
  • It will launch a developer tool window containing all the specific codes, which are used in the development of the password text box.
Selenium with C# Tutorial
  • Copy the value of name attribute as pass and paste it into the code.

Here is 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"); 

Step 5: Once we have identified the username or password textbox, we will find the Log in button and perform the click operation.

  • Right-click on the Log-in button, and select the Inspect option from the pop-up menu, as shown in the below image:
Selenium with C# Tutorial
  • The developer tool window will get launched with having all the specific codes used in the development of the log in button, as shown in the below screenshot:
Selenium with C# Tutorial

Copy the value of Id attribute u_0_b and paste it in the code.

Here is 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")  
Step 7: In the last step, we will close the browser under the EndTest() method.
Here is the sample code for closing the browser:
public void EndTest()  
{  
//close the browser  
driver.Close();      
}  
After combining all the 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();      
}  
}  
} 

Execute the Test Script:

For running the test scripts, follow the below steps:

  • Go to the Test in the toolbar section and select Test Explorer, as shown in the below screenshot:
Selenium with C# Tutorial
  • The Test Explorer window will then appear on the screen and to view all the available tests, we have to build our solution.
Selenium with C# Tutorial
  • For building the solution, go to Build and select Build Solution from the given pop-up menu, as shown in the below screenshot:
Selenium with C# Tutorial
  • Once we are done with the Build solution, we cannot see our Tests on the test explorer window because we have not added the NUnit attributes.
  • The NUnit contains some inbuilt attributes, but here we are going to use SetUp, Test, and TearDown
  • So, we will add the Test attribute in our code just like below:
[Test]  

public void ExecuteTest()  

  • While adding Test attribute, it will give the below error.
  • To overcome this error, we will then use the reference of the NUnit framework as shown in the below image:
Selenium with C# Tutorial
  • Now, we will go to the Test Explorer and Build the solution once again, and we get the ExecuteTest on the Test Explorer.
  • After that, we will be going to run the selected test as shown in the below image:
Selenium with C# Tutorial
  • After running the test script, it will open the browser, as shown in the below screenshot:
Selenium with C# Tutorial
  • The test will then fail because we have not called the Initialize method anywhere in the code.
  • So we need to tell the NUnit Framework by adding two more attributes called SetUp and TearDown into the code.
Selenium with C# Tutorial

Our final code will then look like this after adding all the 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();      
}  
}  
} 
  • After that, once again, perform the build solution and run the selected test.
  • Our test then got passed in the Test Explorer window, as shown in the below screenshot:
Selenium with C# Tutorial
  • And the above test script will then launch the Google Chrome browser and automate all the test scenarios, as shown in the below screenshot:
Selenium with C# Tutorial
  • If we want to see output, we will pass some value and see the output by clicking on the output link.
  • And we can also see the output for all the operations that we perform on the console as shown in the below screenshot:
Selenium with C# Tutorial
Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button