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:
- Selenium IDE
- Selenium web driver
- 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.
- 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.
- Click on Selenium IDE icon -> click on Create a new project -> Enter Project name (example:
DemoSeleniumIDEPrj
):
- 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.
- Enter Username and password -> click on Submit. Application redirects to the Employee List page.
- 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.
- 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.
Limitations
- Data-driven testing cannot be done.
- Database testing cannot be done.
- Dynamic operations cannot be tested in web applications.
- There is no way to export result reports.
- 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:
- 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();
- Navigation Commands
- Refresh the current web page:
driver.navigate().refresh();
- Click forward button in existing browser window:
driver.navigate().forward();
- Web Element Commands
- Clear element:
driver.findElement(By.id("UserName")).clear();
- Click element:
driver.findElement(By.id("UserName")).click();
WebDriver Architecture:
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.
- Open Visual Studio -> Create new class library project (File -> New -> Project)
- 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:
- 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:
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();
}
}
- 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).
- 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.
Points of Interest
Advantages
- Open-source tool: It's free and no purchase needed.
- 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.
- 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.
- 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