Wednesday, 11 April 2018

How to write Selenium Test using NUnit Framework

In this article, we will learn the usage of NUnit Framework and we will also see how to download and install NUnit & NUnit adapter. But before that let’s just understand that why we need to use the NUnit Framework for Selenium test cases.

1) Go to Test >> Windows >> Test Explorer.
NUnitTest-11

2) On the left hand side of the Visual Studio, a new window will appear if already not opened and this window is the Test Explorer Window.
NUnitTest-12

3) Go to Build >> Build Solution.
NUnitTest-13
Notice that the Test Explorer window is still empty and Visual Studio is not able to find any test to run for the build. Even though if you are following the previous two tutorials, you will relies that we got one FirstTestCase in the project. Even in the Output window it says 0 succeeded and 0 failed.
NUnitTest-14
Now let’s just install Nunit Framework and Nunit Adapter and see what happens after that.


What is NUnit Framework?

NUnit is a unit testing framework for performing unit testing based on the .NET platform. It is a widely used tool for unit testing and is preferred by many developers today. NUnit is free to use. NUnit does not create any test scripts by itself. You have to write test scripts by yourself, but NUnit allows you to use its tools and classes to make unit testing easier. The points to be remembered about NUnit are listed below:
  • NUnit is not an automated GUI testing tool.
  • It is not a scripting language, all tests are written in .NET supported languages, e.g., C#, VC, VB.NET, J#, etc.
What is NUnit Adapter?
The NUnit Test Adapter allows you to run NUnit tests inside Visual Studio.


Steps to Download and Install Nunit Framework using the NuGet Packages?

1) Go to References >> Manage Nuget Packages… from the Solution Explorer window from the right side.
InstallNunit_1
Note: Same can be done by going to Tools >>Nuget Package Manager.

2) Type Nunit in the Search box. This will take few seconds to find the right tool.  Locate (search for) NUnit in the center panel and highlight it by selecting it and Click Install.
InstallNUnitAdp_3


Steps to Download and Install NUnit Adapter using the Extension Manager?

1) From within Visual Studio, select Tools >> Extensions and Updates..
InstallNUnitAdp_1

2) Search for NUnit Test Adapter and once found click on Install button.
InstallNUnitAdp_2
Note: Same can be downloaded from the Nuget package as well, below is the difference between Nuget and Extension.


How to choose between Extension and NuGet package

The Extension will apply to Visual Studio itself, and will work for all projects you use. All users of your solution need to install the Extension.
The Package will apply to the solution, and will work for any other user too, as it follows the solution.

Steps to Create a NUnit Test

The first thing need to do is to create a new class  and write test code in that.
1) Right Click on the Project on the Solution Explorer and select Add >> Class.NUnitTest-1

2) Give your Class name ‘NUnitTest‘ and click on Finish button. This will bring up totally a sweet class creation window.
3) Write the selenium code for the following steps:
  • Start a FireFox Browser
  • Open Website in the browser started by Selenium
  • Close the Browser
Code:

4) Divide the code in to three different parts:
  • Start a FireFox Browser : Initialize
  • Open Website in the browser started by Selenium : OpenTestApp
  • Close the Browser : EndTest
Code:
NUnitTest-2

5) Now Build the solution again by doing Build >> Build Solution.
NUnitTest-3

Take a look at the Test Explorer window, it is still Empty and not found any test to run for the build.

6) Now use NUnit Annotations to define your test. Use [SetUp], [Test] & [TearDown] for the three methods created above for each action. And again run the Build
Code:
NUnitTest-5
Note: Noticed that the Test is appeared in the Test Explorer window. So like this you can define unlimited number of Tests in the single class file but the SetUp method will run once before the every Test and TearDown method will also run once after every Test. Each test can be named differently like this.

7) Its the time to run the NUnit test. To run the test, Right Click in the Code window and select Run Tests.
NUnitTest-6

Selenium will start a Firefox browser, open the website and close it. Once the test run is finished, in the Test Explorer window the text above the OpenAppTest will change from Not Run Tests to Passed Tests.
NUnitTest-7

Steps to Create a New Package

Now we are moving to learning Selenium, we will be doing lot of practice for Selenium Code and so it makes sense to create a different package for all the Selenium Test. To achieve that, we need to create a new folder in to the project solution.
1) Right Click on the Project and select Add >> New Folder and name it Selenium Basics.
CreatePackage_2
The Solution Explorer will look like this now.
CreatePackage_3

Set Up Selenium WebDriver with Visual Studio in C#

In this chapter we will learn to Set Up Selenium WebDriver with Visual Studio in C# which cover the following topics:
  • How to Download Selenium WebDriver
  • How to run first Selenium Test

Before moving on, let’s just first change the name of the test case, which got created by default by the Visual Studio at the time of the Project Creation. As of now, the name of the test case is Program, let's just change this to something more meaningful, like FirstTestCase.
CreateNewProject_3

1) In the Solution Explorer window, which is on the right side of the Visual Studio in the above image. Right Click on the Program.cs and Select Rename.
SetUpSelenium_1

2) Notice that the text Program.cs is selected by default, now just type the new test case name ‘FirstTestCase‘.
SetUpSelenium_2

3) The new name will start reflecting everywhere in the project or code window.
CreateNewProject_4


Steps to Download Selenium WebDriver

1) Go to Tools >> Nuget Package Manager >> Manage Nuget Packages for Solution....
InstallNUnitAdp_1
Note: Above screenshot is wrongly taken, please select Nuget Package Manager >> Manage Nuget Packages for Solution…

2) In the Seach Box, search for Selenium.WebDriver or Selenium. This will take few seconds to populate the Selenium. Once done, just selectSelenium.WebDriver and click on Install to start the installation process.
InstallSelenium_1

3) Once Visual Studio is finished with the successful installation of the Selenium WebDriver, it will generate the output logs.
SetUpSelenium_3
Now that we have the Selenium DLLs referenced in our project it time to test the configuration by running a quick test.

Steps to Write the First Selenium C Sharp Test

1) At the top of your project code after the last ‘using’ namespace add the following Selenium namespaces:

2) Add the following code in your static void Main section:

Now the Code window of the project should look like this:
FirstTest_2
Note: We will come back to the understanding of the code later, once we start with Selenium Tutorial, but as of now just understand that the above code is creating an instance of Firefox driver and opening an URL in it.

3) Run the test by clicking on the Start button given on the top bar.
FirstTest_3
Notice that the Visual Studio started a Console application and just followed that it initiated a Firefox driver and opened the website.

Download and Install Visual Studio



Visual Studio Community is a free full-featured IDE for developers building apps across devices, desktops, and the cloud. The community edition includes all the capabilities needed for a student, open source contributor, or small team to create compelling applications – powerful productivity features, cross-platform mobile tools for Windows, iOS, and Android, as well as access to extensions.
Visual Studio Community supports Visual Basic, C#, and C++. This makes it suitable for a beginner as the software is free to download and install. Follow these simple steps below to install and setup Visual Studio Community.
In this tutorial, we will cover the following topics to set up Visual Studio for the Selenium Scripts:
  • Download Visual Studio Community
  • Install Visual Studio Community
  • Create a New Project in Visual Studio

Steps to Download Visual Studio Community

Download_Visual_Studio_1

2) Save the .exe file to your disk.
Download_Visual_Studio_2


Steps to Install Visual Studio Community

1) Double click on the saved file to start the installation.
Download_Visual_Studio_3

2) Click on the Run button.
Download_Visual_Studio_4

3) Click ‘Install‘. Be sure to read and agree to the License Terms and Privacy Policy.
Download_Visual_Studio_5
Note: Change the default location if you like to and let the ‘Type of Installation’ be default as Typical, unless you know what you are doing.

4) Wait for the install to complete.
Download_Visual_Studio_6
Note: Installation would take around 10 to 15 mins.

5) Click on Launch button to start the Visual Studio.
Download_Visual_Studio_7


Steps to Create a New Project

1) Click on New Project.
CreateNewProject

2)  Select Visual C# from the left, then select Console Application from the middle. It will give the option to mention the Project name, location & solution name. Give the sensible name to the project, just for the sake of simplicity, enter ToolsQA and give the preferred location to the project. Click on OK button to proceed.
CreateNewProject_2
Note: Let the solution name be same as ToolsQA.

Now you will end up with the below screens on your machine.
CreateNewProject_3