Selenium with C#

How To Create Selenium Test using NUnit Framework

Selenium Test with NUnit

If you have worked on Java then you must have used JUnit or TestNG for unit testing. In the same way we have NUnit which works in slimier way. This post will guide you complete details about NUnit and how to build Selenium Test using NUnit Framework. Before we start let us discuss few points so that you can understand everything.

 

1-What is Unit Testing?

Unit Testing is a level of software testing where individual units or components of software are tested. In a Procedural Programming, a unit could be an entire module, but is more commonly an individual function or procedure. In Object-oriented Programming, a unit is often an entire interface, such as a class, but could be an individual method. Unit tests are short code fragments created by programmers or occasionally by White box testers during the development process. It forms basis for component testing

 

2-Types of unit test frameworks available.

A lot of unit testing frameworks are available for .NET. Most commonly used are

  1. MSTest
  2. NUnit
  3. NET or XUnit   

 

3-Comparison between MSTest, NUnit and XUnit

Selenium Test using NUnit Framework

 

4- What is  NUnit

NUnit provides a console runner, which is used for batch execution of tests. The console runner works through the NUnit Test Engine, which provides it with the ability to load, explore and execute tests. Tests can run in parallel. Strong support for data driven tests. Supports multiple platform.

 

5-NUnit installation

Steps to install NUnit in visual studio.

Step 1: Create a C# project by using the steps mentioned in previous blog.

Step 2: Select the project created in right hand side and right click on it. Go to References -> Right click and select Manage NuGet Packages.

NUnit Start

 

Alternative way is

NUnit Alternative

  1. Click on Tool
  2. Select NuGet Package Manager.
  3. Select Manage NuGet Packages for solutions.

 

Step 3:  In the next screen

search for NUnit

  1. Click on Browse.
  2. Enter NUnit in search field and hit enter.
  3. Select the NUnit from the list of result.
  4. Click on Install.

 

Step 4: In next screen, click on OK.

NUnit confirmation

 

Step 5: Once the NUnit is installed, we can see the following message.

NUnit installed

 

Once NUnit is installed, let’s install NUnit Adapter. We can install NUnit Adapter in 2 ways

  1. Using Extension Manager.
  2. Using NuGet Package Manager.

 

Let’s see the installation of NUnit Adapter using Extension Manager.

Step 1:

Adapter Start

  1. Click on Tools.
  2. Click on Extension and Updates.

 

Step 2: In the next screen,

Search NUnit Adapter

  1. Click on Online
  2. Enter NUnit Adapter in Search field in the right side and hit enter.
  3. Once the result is found, Select the latest NUnit Test Adapter and click on It will start the download once u restart Visual Studio.

Downloading Adapter

Adapter Complete.png

 

6-How to use NUnit with Selenium.

 Let’s see how to use NUnit with selenium with an example.

 Step 1: Create C# project by using steps mentioned in previous blog.

First NUnit Program

  1. Right click on Project.
  2. Select Add
  3. Select Class

 

Step 2: In the next screen,

Creating Class

  1. Select Visual C# items which will be selected by default.
  2. Select Class
  3. Enter the Name of the class in Name field
  4. Click on Add

 

Step 3: Write code which

  • Opens a browser.
  • Open some application URL and perform some action.
  • Close the browser.

Let’s try this with Firefox browser.

Add Firefox driver in the similar way we added chrome driver in previous blog.

Code without Annotations look like.

        IWebDriver driver; //Declare WebDriver instance
        public void Initialize()
        {
            //create a firefox driver instance
            driver = new FirefoxDriver();

        }
        public void LaunchApp()
        {
            //Maximize the browser
            driver.Manage().Window.Maximize(); 

            //Launch a website
            driver.Url = "https://vistasadprojects.com/mukeshotwani-blogs-v2/";
        }
        public void ShutDown()
        {
            driver.Close();
        }

Code with Annotations looks like

Code without NUnit

 

Step 4: Build the class.

Build Solution

  1. Click on Build
  2. Select Build Solution.

 

Once Build is completed, we may get CS5001 error which says Main method is missing.

CS5001_Error

 

In order to resolve it

  1. Go to Project.
  2. Select NUnit_Project Properties.

Error Resolve

In the next screen,

Select Class Library and save the changes (Ctrl +S).

 Class_ Library

 

Step 5: Once build is completed, open Test Explorer.

  1. Click on Test
  2. Select Windows
  3. Click on Test Explorer.

Open Test Explorer

Test Explorer will be opened in left side which contains all the tests present

Test Explorer View

 

Step 6: Select the test which u want to run and click on Run Selected Test.

Run Selected Test

Test will start its execution.

Once execution is completed we can see the result as below.

Test executed

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

2 thoughts on “How To Create Selenium Test using NUnit Framework

  1. Prasad says:

    Hi,

    Generally, can we create a maven project using selenium with C#.

    Thanks

    1. Hi Prasad,

      There are some theories of Maven support for C# but I don’t think of any fully practical solution for C#. If you find something solid then let me know…:)

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.