Unit Testing With Selenium Web Driver - Part One

Introduction

Selenium is one of the most popular tools used for automating web applications. Selenium is an open source tool, which is used for the execution of test scripts or test cases on the web application. As Selenium is open source, it supports various programming languages, such as Java, C#, PHP, JavaScript, Ruby, Python etc.

Selenium is language independent, which means Selenium is developed using Java, and it can be used with either C#, Java, PHP, JavaScript etc. Selenium can be executed on various operating systems like Windows, Linux, MacOS etc. As Selenium is language independent, it is also a platform independent. That means the framework which is developed using C# or Java can be executed in different platforms or Operating systems like Windows OS, Mac OS, Linux etc.

Selenium is a library or API which consists of Classes, Methods, Interfaces, etc, which can be used and integrated with various programming languages mentioned above. Selenium is written using one of the most famous programming languages; i.e. Java, and it's cross-platform.

Selenium supports various programming languages and various platforms. For these reasons, most of the top companies prefer Selenium rather than using Quick Test Professional (QTP) and CodedUI, which are both licensed versions. Selenium supports automation with various browsers such as Google Chrome, Edge Browser (Windows 10), Firefox Browser, Internet Explorer, Safari etc.

Each browser has its own driver, for executing the test scripts on their browsers, such as, for executing the Selenium Scripts on Chrome we have Chrome drivers; for executing the Selenium Scripts on Internet Explorer, we have Internet Explorer drivers; for executing the Selenium Scripts on Safari, we have Safari drivers, for executing Selenium Scripts on Opera, we have Opera drivers.

Internet Explorer(IE) driver and GoogleChrome driver support both 32-bit and 64-bit versions, which we can download and use based on our system requirements. Selenium Webdriver and Selenium Grid are the most commonly used tools in the IT industry for the development of testing products. In Selenium, earlier versions like Selenium 1.0 and Selenium 2.0, we aren't required to set up Firefox drivers for executing the scripts on the Firefox Browser. By default, Selenium has support for Firefox.

In Selenium 3.0, all the vendors like Google, Apple, Microsoft, Firefox have their own drivers to work with Selenium.
 
Selenium was developed as an internal project to test internal applications at ThoughtWorks, which was developed in client side technology like JavaScript by Jason Huggins. It is also called Selenium Core or Selenium IDE. Jason Huggins tested various internal applications and gave demos to different colleagues. Additionally, they were excited about the success of the first version. Later, Paul Hammant joined his team and developed the other version of Selenium; i.e., (Selenium RC) then this tool became open-source to work with the different Browsers and different platforms.

Simon Stewart at ThoughtWorks developed an Automation tool for the browser known as Webdriver and later they merged these Selenium RC with Webdriver called Selenium Webdriver(Selenium 2.0). Philippe Hanrigou at ThoughtWorks developed Selenium Grid in 2008. Selenium Grid is used for configuring single hub and multiple nodes. The current hub is capable of running multiple test cases on the client machine as well as remote machines, which reduces the time as well as the resource.

Finally, Selenium 3.0 was developed this year with the new features. It is a combination of Selenium 2.0 + Selenium 1.0, which means it supports Selenium 2.0 features but it doesn’t have support for Selenium 1.0; i.e., Selenium Core.

Please refer to my article, to learn more about Selenium 3.0.
When Selenium was in development, one of the most popular testing tools was called QTP (Quick Test Professional) which was developed by "Mercury Interactive" before it was acquired by HP. QTP was Selenium's competitor, they have named their product based on the element selenium. As we know the chemical element term Selenium (Se) is capable of poisoning or detoxifying Mercury. so the term or the product was named as Selenium.

In my previous articles/blogs on Selenium web driver, we have seen the execution of various HTML controls using Selenium C#. As we know, using Selenium we can automate anything which we see on the web page or web application, like Alert message, prompt message, message box, textbox, dropdown buttons (single click, double click), hyperlink, checkbox, radio button etc. and we can even automate the applications developed using Ext.js(Sencha Applications) or KendoUI etc. In order to identify the controls which are available on the web page, we use various control properties like Id, Name, XPath, ClassName, TagName, CssSelector, RelativeXPath, AbsoluteXPath etc.

In order to automate a control which is available on the web page, we use the above-mentioned control properties like Id, Name, XPath etc. We can directly give the control properties to identify the specific control and automate them. In some cases or some scenarios, there are no control properties except className which includes a space in it, so if that is the case, then we need to use either XPath or CssSelector.

For taking XPath, we need to click on F12 Key to inspect element or Ctrl+Shift+I. Once we select the element, right click on the particular element. One popup menu appears. Navigate towards Copy--> Copy XPath. If there is any space in the control property like ClassName, then we cannot access that control by using ClassName property. Instead, we need to access that control by using CssSelector control property. In order to use CssSelector, we need to replace spaces with a dot(.).

For Example

ClassName="name of the element on webpage" ; then, we need to use a CssSelector like
  1. CssSelector=".name.of.the.element.on.webpage"
In some applications like Sencha or ExtJs Application, we cannot access a control with control properties like Id, Name, XPath etc. because when the page refreshes, the control properties change dynamically in runtime.

In a few cases or scenarios, the control will have a unique className property; due to this reason, we can either use CssSelector or Relative XPath. For Using RelativeXPath , we need to create our own XPath by using two forward slashes like "//" or we can use a relative XPath extension which is provided by Google.

If we want to access a specific control if it doesn't contain any control properties and we cannot give Xpath, as it changes every time dynamically, then we need to access the control with its parent control, which has a unique control property.

In ExtJs or sencha application, not all controls will change properties dynamically. But there will be a few controls with unique control properties. If the control has a unique property like className, then we can use it directly, otherwise, we need to access the control by its parent control by taking relative XPath. For taking relative XPath, we need to access from its parent control whichever is having a unique control property like -
  1. //div[@class="uniqueclassproperty/div/a/input[2]"]  
If we want to access the second input control on a webpage, we need to use it as input [index value of input control] that is input[2].
 
Introduction to Selenium 3.0
 
In the earlier versions of Selenium like Selenium 2.0, we do not need to setup Firefox driver and Selenium doesn’t have any Firefox driver. By default, Selenium supports the Firefox driver.
 
Example 1
 
In this example, we don't require any fFrefox driver's path. By default, Selenium 2.0 has built-in support for Firefox.
  1. Using System;  
  2. Using OpenQA.Selenium.Firefox;  
  3. [TestMethod]  
  4. public void LaunchFireFox() {  
  5.     FireFoxDriver driver = new FireFoxDriver();  
  6.     driver.Navigate().GoToUrl("http://www.google.com");  
  7.     driver.Manage().Window.Maximize();  
  8.     driver.Quit();  
  9. }  
In Selenium's latest version, i.e., Selenium 3.0, we have a separate driver for Firefox (Geckodriver).

You can download Gecko driver from Selenium official Website.
 
Example 2

In this example, we need to download and save the driver in some folder path like D:/>FirefoxDrivers\Drivers\GeckDriver.exe 
  1. Using System;  
  2. Using OpenQA.Selenium.Firefox;  
  3. [TestMethod]  
  4. public void LaunchFirefox() {  
  5.     FireFoxDriverService driver = new FireFoxDriverService("Path of the GeckoDriver(D:/>FirefoxDrivers\Drivers\)""name of executable file(example geckodriver.exe)")  
  6.     // code to Launch browser  
  7. }  
Firefox Browser version is supported by 47+. If we want to work with the older versions i.e. less than 47 version, there is no need to install Gecko driver but working with more than 47+ either in Selenium 2.0 or Selenium 3.0 requires Gecko driver  is installed and configured.

You can download Gecko driver from the below link provided. 
 
http://www.seleniumhq.org/download/
 
In this version, we also have a support for Microsoft’s Edge Browser, which is Microsoft’s own Browser.

Following are the prerequisites to work with Edge Browser .

The client machine should be installed with Windows 10 Operating system.

It supports Java greater than version 8 to run Java code (hub). Support for Safari (Apple OS) Browser is provided by Safari drivers (10.0 version+). More than 9+ versions of Internet Explorer are supported in Selenium 3.0. Now Selenium 3.0 has become a W3C (Worldwide Web Consortium) standard. Selenium 3.0 removed Selenium Core but supports Selenium RC indirectly through back-end Webdriver.

Pre-requisites
  1. Java Runtime Environment(JRE) latest version recommended.
  2. Selenium drivers like Chromedriver, IEDriver, Geckodriver, operadriver, safaridriver etc.
  3. Browsers like Chrome, Firefox, Internet Explorer, opera etc.
For unit testing of Selenium webdriver, we need to add unit test project by navigating through File-->New-->Project and add Unit testing project like below.

 
 
Select Unit Test project and give some name for the application as Selenium and click OK. Right-click on references and click on Manage NuGet Packages like below.

 
 
In this search for Selenium and click on browse to get all the packages related to Selenium. Add or install Selenium Web Driver and Selenium Support from NuGet Packages like below.

 
This will be added to the solution explorer or Selenium Project within the references like below.

 
 
Once we have created the Unit testing project with the name as "Selenium", the sample solution structure will look like.
 
 
 
By default, it will come with [TestClass] and [TestMethod] attributes which are part of Microsoft.VisualStudio.TestTools.Unitesting dll.
[TestClass] attribute can be used with a Class, and [TestMethod] is used for Unit testing Methods etc.

If we want to perform Unit testing using Selenium then Mbunit.Framework dll supports various Attributes like [TestFixture] [Test][Parallelizable] etc.

The mentioned above attributes are part of Mbunit.Framework.dll which can be downloaded from Manage Nuget Packages.

Gallio Bundle is the test runner of selenium scripts, if we want to execute our selenium scripts then we can build the solution and give the dll to gallio, which inturn executes the scripts, before executing the scripts on gallio it will identify the [TestFixture] and [Test] attributes.

While running unit test method, test runner will identify with the following attributes like TestClass and TestMethod if it is part of codedUI.

If we want to execute our script using selenium then we need to replace the TestClass attribute with TestFixture and TestMethod attribute with Test, because selenium's Test runner will identify and execute its scripts by identifying the above attributes like TestFixture and Test.

Whenever we create a Unit testing project, by default Microsoft provides us with [TestMethod] and [TestClass] attributes, which are part of Microsoft.VisualStudio.TestTools.Unitesting dll which helps us in the execution of CodedUI scripts.

If we want to execute the Selenium Scripts whatever we write under the [Test] method,  then we need to build the solution and give the dll to gallio bundle as part of selenium execution. If we didn't replace the [TestMethod] or [TestClass] attributes with Mbunit.Framework attributes like [TestFixture] and [Test] then gallio cannot identify the attributes which are part of Microsoft.VisualStudio.TestTools.Unitesting dll, so it won't execute the scripts and throws an exception like MethodNotFoundException.

If we want to execute with gallio bundle test runner then we need to replace the attributes to Mbunit.Framework attributes like below.
  1. using System;  
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  3. using MbUnit.Framework;  
  4. namespace selenium {  
  5.     [TestFixture]  
  6.     public class UnitTest1 {  
  7.         [Test]  
  8.         public void TestMethod1() {  
  9.             // Code for Selenium execution..  
  10.         }  
  11.     }  
  12. }  
The above piece of code contains the attributes of Mbunit.Framework, which can build the above solution and provide the dll to gallio bundle, it will identify the attributes of Mbunit.Framework, and execute the selenium execution code with its test runner.
Selenium supports various browsers to execute its scripts like Chrome, firefox, safari, Internet Explorer, Edge browser (For Windows 10 O.S), opera etc.

Each browser has its own driver for example if we want to execute selenium scripts on chrome browser we have ChromeDriver which is part of OpenQA.Selenium.Chrome dll.

 
 
In the next article, we will see how to download Selenium drivers and  the execution of Selenium scripts. Thanks. I hope this helps you. Please provide me with feedback for my article.


Similar Articles