Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Selenium Automation Testing

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
13 May 2021CPOL4 min read 6.6K   15   1
An overview of Selenium Automation testing tools, their advantages, and limitations.
This article gives an overview of Selenium Automation testing tools, their advantages, and their limitations. It explains steps to set up Selenium IDE and record a simple test. It also explains Selenium .NET WebDriver and how to develop a sample test project using C# and Visual Studio.

Introduction

Selenium is a suite of tools for web browser automation that uses the best techniques available to remotely control browser instances and emulate a user’s interaction with the browser. It's the most and widely used tool for automation testing.

Selenium offers three main tools:

  1. Selenium IDE
  2. Selenium web driver
  3. Selenium Grid

This article explains Selenium IDE and Webdriver.

Background

There are scenarios in most projects to do regression testing any time the new changes are being released. Automation tools help us reduce testing time, bugs and improves quality.

Using the Code

Selenium IDE

Selenium IDE is a browser plugin that records and plays back user’s interactions with the browser. Let us see how to use Selenium IDE in the chrome browser.

  1. Add Selenium IDE extension in Chrome: Open Chrome browser -> Extensions -> Open Chrome Web Store - > search for “Selenium IDE” -> Add to Chrome -> you should now see Selenium IDE icon in the browser.

    Image 1

  2. Click on Selenium IDE icon -> click on Create a new project -> Enter Project name (example: DemoSeleniumIDEPrj):

    Image 2

  3. Provide the application URL (C# sample web application project can be downloaded from sampleaspnetwebapplication). This sample application has a login page and a home page. Click on Rec on the top right corner. This should open the application login page in a browser.

    Image 3

  4. Enter Username and password -> click on Submit. Application redirects to the Employee List page.

    Image 4

    Image 5

  5. IDE should record the above steps and you can run the test again to see if it performs the same steps again. A successful run should show the test is green.

    Commands in selenium are written using Selenese language. Commands (for example: open, type, click, etc.) help Selenium to understand what actions or operations to perform.

    Image 6

  6. Test in Selenium IDE can be exported as the script using one of the languages as shown below. C# NUnit option should create .cs file.

    Image 7

Limitations

  1. Data-driven testing cannot be done.
  2. Database testing cannot be done.
  3. Dynamic operations cannot be tested in web applications.
  4. There is no way to export result reports.
  5. Cannot use it for extensive operations.

Selenium WebDriver

WebDriver is an API (Application Programming Interface) to create and run tests and it is a cross-platform testing framework. It interprets commands and performs actions on web elements. It supports testing frameworks like Junit, NUnit, TestNG, etc. WebDrivers for various programming languages can be downloaded from the official Selenium website.

WebDriver C# API reference: Dotnet

WebDriver API Commands are broadly classified into three categories:

  1. Browser Commands
    • Fetching a web page: driver.get("www.google.com")
    • Get current web page title: driver.getTitle();
    • Get url of current web page: driver.getCurrentUrl();
  2. Navigation Commands
    • Refresh the current web page: driver.navigate().refresh();
    • Click forward button in existing browser window: driver.navigate().forward();
  3. Web Element Commands
    • Clear element: driver.findElement(By.id("UserName")).clear();
    • Click element: driver.findElement(By.id("UserName")).click();

WebDriver Architecture:

Image 8

To create and run the C# WebDriver test, you will need the following:

  • Visual Studio
  • Test framework (we will use NUnit in the below sample)
  • Selenium WebDriver
  • Chromedriver executable

Follow the below steps to set up and run the web driver test.

  1. Open Visual Studio -> Create new class library project (File -> New -> Project)

    Image 9

  2. Add WebDriver and NUnit framework to Visual Studio project using NuGet. NuGet is a dependency management tool and pulls all the packages like WebDriver and NUnit from repositories. Click on Tools -> NuGet Package Manager -> Manage NuGet packages for solution.

    After adding packages, the Installed tab should show as below:

    Image 10

  3. Add “Script exported from Selenium IDE (.cs file)” to the Visual Studio project. Exporting scripts from Selenium IDE will save coding time. You can also create a new test and write a Selenium script in it.

    Script file should look like below:

    C#
    // Generated by Selenium IDE
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;
    using OpenQA.Selenium.Interactions;
    using NUnit.Framework;
    [TestFixture]
    public class DemotestTest {
      private IWebDriver driver;
      public IDictionary<string, object> vars {get; private set;}
      private IJavaScriptExecutor js;
      [SetUp]
      public void SetUp() {
        driver = new ChromeDriver();
        js = (IJavaScriptExecutor)driver;
        vars = new Dictionary<string, object>();
      }
      [TearDown]
      protected void TearDown() {
        driver.Quit();
      }
      [Test]
      public void demotest() {
        driver.Navigate().GoToUrl("http://localhost:64031/");
        driver.Manage().Window.Size = new System.Drawing.Size(1066, 824);
        driver.FindElement(By.Id("username")).Click();
        driver.FindElement(By.Id("username")).SendKeys("demo");
        driver.FindElement(By.Id("password")).SendKeys("demopwd");
        driver.FindElement(By.Id("btnsubmit")).Click();
        driver.Close();
      }
    }
  4. To find element by xpath (example: driver.FindElement(By.XPath("//input[@id='username']")));), use Chropath. Chropath is Chrome extension. Once added, you can see it as below under developer tools (F12).

    Image 11

  5. To run tests “ChromeDriver” is needed. Download ChromeDriver, unzip it and put it in the Windows path (example: c:\windows in windows 10). You can test ChromeDriver by going to command prompt ->c:\windows ->Chromedrive.exe. You will see “Starting ChromeDriver”.

    Run the test using test explorer. This should open the application in Chrome browser and perform the steps written. Steps are executed sequentially. Test turns green if the test is passed.

    Image 12

Points of Interest

Advantages

  1. Open-source tool: It's free and no purchase needed.
  2. Browser and Platform independent: Since it's developed using JavaScript, it supports most browsers like Chrome, Firefox, Internet Explorer, Edge, Safari, Opera and it supports operating systems like Windows, Mac, and Linux.
  3. Web Drivers for Multiple Programming languages: In order to write automation scripts, Web drivers are available for programming languages like C#, Java, Ruby, Python, and JavaScript.
  4. Time-saving and less error-prone: Saves a lot of time in performing repeated tests and avoids manual errors.

Conclusion

This article explained Selenium IDE and WebDriver, performing automation testing using these, writing test scripts using WebDriver and NUnit.

History

  • 13th May, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Solution Architect working in NJ, USA. Have extensive experience in MS technologies like .Net, SharePoint, Dynamics 365, Office 365 products, and Cloud solutions like Azure and AWS.

Comments and Discussions

 
QuestionChrome driver Chrome version matching Pin
CodingCoyote14-May-21 7:29
professionalCodingCoyote14-May-21 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.