Selenium Automation Test Cases For The .NET Web Application

We know that testing is an essential part of any development process. Prior to every release of a software project/product, it is mandatory to test it end to end and make sure that everything is working as expected. So, Smoke and Regression tests are standards for every new version of software project/product. Imagine how many resources' efforts are required each time we release a new software project/product. We can reduce resource count and time by automating the process instead of doing it manually, at the times when we plan for a new release of project/product.

In this article, I am going to show you:

  • What is Selenium?
  • How Selenium helps you automate your software project/product testing.
  • Selenium setup and configuration process.
  • Writing sample automation test cases.
  • Test the written test cases.

What is Selenium?

Selenium is an automation tool used for writing test cases to automate the web application's functionality. It is an open source tool to write automation test cases and to test the functionality, automatically. And, it supports across multiple browsers and operating systems.
Selenium

Following are the programming languages supported by the Web Driver:

  • .NET
  • Java
  • PHP
  • Python
  • Perl
  • Ruby etc.

Now, let's understand the list of tools used in Selenium test cases, and their usages.

Selenium IDE: Selenium IDE is a tool which facilitates testers to write and test their Selenium test cases.

Selenium RC: It is a tool which facilitates you to write Selenium automated test cases for web applications irrespective of any programming language and any HTTP websites for the JavaScript enabled browsers.

Selenium WebDriver: It makes direct calls to the web browser and retrieves results.

Selenium Grid: It allows you to run your test cases across different browsers in different machines, simultaneously.

Advantages of Selenium Test:

  • Open source.
  • Can be extended with various technologies which expose DOM.
  • Can execute scripts across multiple browsers.
  • It supports multiple OS platforms.
  • It is compatible with mobile devices too.
  • Test executes within the browsers, so focus is not needed while executing the script.
  • Using Selenium Grids, you can execute tests in parallel.
  • Web Driver is faster than Selenium RC because it has simpler architecture.
  • Web Driver directly talks to the web browser whereas Selenium RC needs the help of the RC Server in order to do the same.

Disadvantages of Selenium Test:

  • It supports only web based apps.
  • It does not provide any facility for Object Repository.
  • It does not provide any facility for Recovery Scenario.
  • QTP is still dominating to Selenium.
  • There is no report generation for test methods by default.
  • Users need to depend on programming language for parameterization.
  • It can't readily support  new browsers but Selenium RC supports.

How to Install Selenium Webdriver:

Before going to write automation of Selenium test cases, first we have to setup infrastructure.

In Visual Studio tool, create a sample test project and integrate Selenium WebDriver in C#.

Step 1: Open Visual Studio.
Step 2: Click on Create a new project, select Test project from the right panel, and then select Unit Test Project.

Unit Test project

Step 3: Add Selenium references or drivers into the newly created Unit Test C# project.

references

  1. First, open the Solution Explorer.
  2. Then, expand the Project.
  3. Right click on references and select Manage NuGet Packages option.
  4. Search for Selenium WebDriver.
  5. Select Selenium WebDriver.
  6. Click on Install.

For reference, find the below screenshots:

Screenshot-1,

reference

After successfully installing the WebDriver, you can see the below screenshot:

Screenshot-2:


reference

And in references section, you can see the highlighted below:

references
Now, open the UnitTest1.cs (if you want you can rename the file name) file to start actual coding for automating the test cases for your C# Web application.

coding

The above references can provide all classes and properties required to integrate the Selenium WebDriver.

How to Install Chrome Driver in your Web App?

Follow the below steps to add ChromeDriver into web application C# project.

  1. Firstly, open the 'Solution Explorer'.
  2. Then, expand the 'Project'.
  3. Right click on references and select 'Manage NuGet Packages' option.
  4. Search 'ChromeDriver'.
  5. Select 'ChromeDriver'.
  6. Click on 'Install'.

Screenshot-1,

Manage NuGet Packages

Screenshot-2,

Manage NuGet Packages

After successfully installed ChromeDriver, for reference you can see the below screenshot:

ChromeDriver
Now, to write the Selenium test cases, we need a web page. Let's take the below sample code for your webpage to write Selenium test cases:

Example: Login.html

code

Before start writing test methods, we have to understand the below elements and their usages:

  • By Id
    Finds the Webpage element by its unique control id.
  • By Name
    Finds the Webpage element by the control name.
  • By Class
    Finds the Webpage element by the Class name.
  • By LinkText
    Finds the link element by its exact text it displays
  • By partialLinkText
    Finds webpage element that contains the given text
  • By TagName
    Finds the Webpage element by the Tag name.
  • By XPath
    This is most popular and majorly used for locating webpage element in a easiest way.
  • By CSS selector
    Finds Webpage element by the CSS selector

    Ex:

    code

    For more details, you can go through this link.

Now, it's time to write Selenium Test cases for the above login page:

code

Now, to run the written Test method, right click on the Test method and click on Run Tests or Debug Tests options, as shown below:

run

If the Test method runs successfully without any error, then you will get Passed Tests in your left panel of Visual Studio (Test Explorer), as  shown below:

Test Explorer

If the Test method fails with errors, then you will get Failed Tests in your left panel of Visual Studio (Test Explorer), as shown below:

Test Explorer