Google+

A Brief Overview on Selenium Test Automation Tool



1. Introduction to Selenium



Overview


The whole and sole purpose of this post is to give you a high level and big picture on Selenium Test Automation Tool. I believe this post will be very useful for the beginners, who wants to know the in and out of Selenium at a high level, so that they can begin their journey on Selenium well. Without any doubt, this post will also be very useful for the ones, who believe they have already learnt Selenium and also the people who are in the process of learning Selenium, as they can cross check their yet to date learning and see whether they are upto the mark in Selenium.

"The main motive of this post is to make everyone aware of Selenium and plan their learnings in a better and organized way"





What is Selenium?


In simple words, Selenium is a Test Automation Tool developed by the company named "ThoughtWorks" from the year 2004.


Things that Selenium can automate


Selenium can automate Web Applications which generally run in Web Browsers. For example, Selenium can automate web applications like www.gmail.com, as it runs over web browser.


Things that Selenium cannot automate


Selenium cannot automate Desktop / Window based applications which don't run on the web browsers. For example, Selenium cannot automate desktop / window based applications like MS Word, MS Excel etc. Luckily most of the applications in the market are Web Applications.


Selenium is an open source and free tool


Unlike other proprietary tools which require us to purchase license for using them, Selenium is an open source and free tool. We don't have to spend a single penny for using Selenium tool. Read more at the below link:



Different components in Selenium


Selenium is not a single tool, instead it is a set of tools. The below are the four different components which we together call as Selenium:

  • Selenium IDE
  • Selenium RC
  • Selenium WebDriver
  • Selenium Grid


Browsers supported by Selenium


Most of the projects automate their applications for Firefox, Chrome and Internet Explorer browser types. The below are the 5 browser types that Seleniums supports in total:

  • Firefox
  • Chrome
  • Internet Explorer
  • Safari
  • Opera


Operating Systems supported by Selenium


Selenium supports all the famous operating systems in the market. The below are the 3 operating systems that Selenium supports:

  • Windows
  • Macintosh
  • Linux



Programming languages supported by Selenium


Selenium supports multiple programming languages for creating automation scripts. Hence we can use any of the below programming languages supported by Selenium:

  • Java
  • C#
  • Ruby
  • Python
  • Perl
  • Haskell
  • Objective-C
  • R
  • JavaScript



Out of all the languages supported, Selenium has more support for Java language and most of the projects use Java, C#, Ruby & Python for automation the scripts using Selenium. And also, we can automate the scripts in Selenium using Java language, even though the application under test is developed using C# language.



Different versions of Selenium


So far, Selenium was released into the market in 3 different versions. The below are the three different versions of Selenium:

  • Selenium 1
  • Selenium 2
  • Selenium 3


The latest version of Selenium is Selenium 3 and it got released into the market on Oct 13, 2016.


History of Selenium


Selenium Core


Selenium was created by Jason Huggins in 2004. To avoid repeated execution of test cases day by day, he created a javascript program and named it as JavaScriptRunner which was later renamed as Selenium core. Over a period of time, Applications were only allowing the internal JavaScript programs by treating the external JavaScript programs as a security breach. This became a drawback for Selenium Core as it uses JavaScript programs to interact with the Applications under test. To overcome this problem while testing using Selenium Core, application testers used to install the Selenium Core's JavaScript programs into the Application code's local copy.

Selenium RC


To overcome the drawback of Selenium core, Paul Hammant has created a proxy server named 'Selenium RC' to trick the application under test that the JavaScript programs are in the same local machine where the application is residing, even though it is not. The drawback of Selenium RC approach is, we have to use a proxy server named 'Selenium RC' to communicate between the Application code and Automation code.

Selenium Grid


Selenium Grid was developed by Partick Lightbody, to reduce the time of Automation scripts execution by running the scripts in parallel on different machines. i.e. Instead of executing all the scripts on a single machine, to reduce the time of execution, all the scripts will be divided across different machines and executed simultaneously.

Selenium IDE


Selenium IDE was developed by Shinya Kasatani, to record the tests like recording a video and execute the recorded tests like playing a video. Hence Selenium IDE is a record and playback tool. Selenium IDE is release into the market as a Firefox add-on/plugin/extension and can be installed on the top of the default Firefox browser. Once installed, we can simply record the tests on firefox browser and playback when required.

Selenium 1


Selenium 1 is nothing but the combination of Selenium IDE, Selenium RC and Selenium Grid.



Selenium WebDriver


Selenium WebDriver was created by Simon Stewart in 2006. As the browsers and web applications were becoming more powerful and not allowing external JavaScript programs. While Selenium RC was tricking the Browser that the Selenium Core's JavaScript program is part of the Application code, by using the proxy server as a communicator. But Selenium WebDriver used the concept of Native Drivers to directly interact with the Application under test and hence eliminated the need for a proxy server. Selenium WebDriver got the support from the largest vendors of the Browsers like Firefox, Chrome, Internet Explorer, Opera and Safari to make Selenium a native part of their browsers. Hence the browsers won't treat Selenium JavaScript programs as an external. The native drivers available for the supported browsers are FirefoxDriver, ChromeDriver, InternetExplorerDriver, SafariDriver and OperaDriver. The development of these drivers is done by the Selenium guys, even though the drivers are native to browsers.


Selenium 2


Later Selenium 1 (i.e. Selenium IDE + Selenium RC + Selenium WebDriver) merged with Selenium WebDriver to make Selenium more powerful and got released into the market as Selenium 2 on July 8th, 2011. Earlier to this merger, there used to be separate teams for developing Selenium RC an Selenium WebDriver. As there two teams identified that Selenium can become more powerful on merging Selenium RC and Selenium WebDriver, they have released the merged version of Selenium as Selenium 2 into the market.


i.e. Selenium 2 is the combination of Selenium 1 and Selenium WebDriver.



Selenium 3


Selenium 3 is the latest version of Selenium and is released into the market on Oct 13, 2016.

Change # 1  -  The big change in this version is that the Selenium RC which is part of Selenium 2 is dropped out in Selenium 3. Selenium RC which was internally implementing Selenium Core's JavaScript program libraries is now replaced with the backed WebDriver API implementation. i.e. What ever the tasks that can be performed only by Selenium RC's JavaScript implementation, can now be performed using the backed WebDriver API implementation which is more flexible. There is no change in the WebDriver implementation from Selenium 2 to Selenium 3. Only the Selenium RC JavaScript libraries got replaced with backed WebDriver API implementation from Selenium 2 to Selenium 3. There is less impact due to this change, as most of the users are writing tests in WebDriver, but there is significant impact on the users who are writing tests in Selenium RC, as Selenium RC wont be supported by Selenium team in future releases and hence should migrate to WebDriver for future support.


Change # 2 -  Another big change in Selenium 3 is that, we have to use Mozilla's gecko driver for running Selenium automation scripts on Firefox browser. Till Selenium 2, development and support of Selenium driver for firefox is provided by Selenium guys using FirefoxDriver class in Selenium, but from Selenium 3 the development and support for Selenium driver for firefox browser will be provided by Mozilla Firefox Browser vendor using the gecko driver. Hence from Selenium 3, we have to use gecko driver in order to execute the Selenium Automation Scripts on Firefox Browser.

Change # 3 - Apple company will be providing the support for Safari Driver for executing the Selenium Automation scripts on Safari browser of macOS Sierra Operating System.

Change # 4 - Microsoft company will be providing the support for Edge Driver for executing the Selenium Automation scripts on Edge Browser.

Hence major change in Selenium 3 from Selenium 2 is the dropping out of Selenium RC JavaScript libraries implementation by replacing them with Selenium WebDriver's API implementation. And also the other major change is that the browser vendors have taken the ownership to support the drivers which are required to run the automation scripts on the browsers. i.e. Development support for gecko driver is provided by Mozilla, safari driver is provided by Apple and edge drive is provided by Microsoft.



2. Selenium Prerequisites



Prerequisites required for learning Selenium


The below are the different prerequisites that are required to know before learning Selenium:

  • Software Testing Basics
  • Automation Basics
  • Core Java
  • Web Technologies like HTML, CSS, XML, XPATH, DOM and JavaScript


Software Testing Basics required for learning Selenium


The below are the few software testing basics that are required for learning Selenium:

  • What is Software Testing ?
  • Why do we perform Testing ?
  • What is a Defect ?
  • Why do we find Defects ?
  • What are Test Cases ?
  • What is Manual Testing ?
  • What is Regression Testing ?
  • What is Automation Testing ?

If you are new to testing, go through the below blog post to get started. 


Automation Basics required for learning Selenium


The below are the few automation basics that are required for learning Selenium:

  • What is Automation Testing ?
  • How can an Automation tool perform testing without a tester ?
  • What is the purpose for automating the testing ?
  • Advantages of Automation testing
  • Applications suitable for Automation Testing
  • Which tests can be automated ?
  • What is the goal of Automation Testing ?
  • What are the most popular Automation tools ?

If you want to learn all of the above Automation basics, go through the below post:


Java concepts required for learning Selenium


Java programming language play a vital role in Selenium Automation. It's not required to learn the complete Java, instead learning of Core Java is enough for Selenium. The below are the different Core Java concepts that are required in Selenium at a high level are:

  • OOPS Concepts
  • Primitive Data Types
  • Variables and Values
  • Operators
  • Decision Making Statements
  • Iterative Statements
  • Methods
  • Constructors
  • Arrays
  • Strings
  • Access Modifiers
  • Wrapper Classes
  • Exception Handling
  • Files
  • Collections Framework
  • And other miscellaneous Core Java concepts 




"Having the knowledge on Core Java is enough for Selenium. It is not required to learn the Advanced Java Concepts."


Web Technologies required for learning Selenium


Apart from learning Core Java, its required to learn different Web Technologies for Selenium. The below are the different Web Technologies that are required for learning Selenium:

  • HTML
  • CSS
  • XPath
  • DOM
  • JavaScript
  • XML


If you want to learn the HTML concepts, go through the below post:




3. Locators and Add-ons



Purpose of Locators in Selenium


Locators are used in Selenium to identify the Graphical User Interface elements on the web pages. Lets say if you want to click on the 'Login' button on the facebook page, we have to use the locator of 'Login' button in Selenium Automation code. Similarly to automate any Application using Selenium, we have to provide the locators of the Graphical User Interface elements on the Application into the Selenium code. Once the required locators for automating the application are provided to the Selenium code, Selenium uses the provided locators to identify the respective GUI elements on the application.


Different types of Locator Strategies


The below are the different types of locators that we can provide in Selenium code for identifying the web elements on the application:

  • ID
  • Name
  • Link Text
  • Class Name
  • XPath Expressions
  • CSS Selectors
  • DOM



Locators Priority


As all the different types of locator strategies can be used to locate the same GUI element on the Application, we have to choose a single locator strategy out of all the available locator strategies. As a first priority, we have to use ID strategy, followed by Name, followed by Class Name and followed by Link Text. But, if the web element is not getting identified using the ID/Name/ClassName/LinkText locator strategies, then we have to go for CSS Selectors locator strategy. Still, if the Web Element is not getting identified using the CSS Selector strategy, then we have to finally go for XPath selector locator strategy. And finally, if the Web Element is not getting identified using the XPath selector locator strategy. then we have to go for DOM locator strategy. 


XPath Expressions versus CSS Selectors 


Out of all the locator strategies XPath expressions locator strategy is the powerful one, but CSS Selectors locator strategy is the recommended one. 

The below is the reason behind recommending CSS Selectors over XPath Expressions:

  • CSS Selectors are speed in identified GUI elements and hence improves the performance of your Selenium Automation scripts.

But practically speaking, the performances difference between CSS Selectors over XPath Expressions in identifying the GUI elements is negligible. Hence this is not the main reason for recommending CSS Selectors over the powerful XPath Expressions.

The below is the reason behind why XPath Expressions are powerful over CSS Selectors:

  • XPath Expressions can traverse backward and forward in HTML code, for identifying the GUI elements, where as CSS Selectors can only traverse forward i.e. Using XPath Expressions, we can locate the child element using the parent element's unique ids, names etc. And also using the XPath Expressions, we can locate the parent element using the child element's unique ids, names etc. But CSS Selectors, cannot locate the parent element using the child element's unique ids, names etc. Hence XPath Expressions are powerful in identifying GUI elements over CSS Selectors. 

By looking the above explanation, XPath Expression needs to be recommended over CSS Selectors. But the truth is, in some cases XPath Expressions are recommended over CSS Selectors and in some other cases, CSS Selectors are recommended over XPath Expressions. Now, lets find out why:

XPath Expressions are not suitable, if you want your Automation Scripts to run on Internet Explorer Browser. By using XPath Expressions for identifying GUI elements, Selenium may not be able to identify few GUI elements on the web pages which are rendered on Internet Explorer Browser. Hence if your requirement is to run the Selenium Automation Scripts on Internet Explorer Browser, CSS Selectors are recommended over XPath Expressions. But, if the CSS Selectors are not able to identify the GUI elements, then the powerful XPath Expressions needs to be used.

But if your requirement is to run your Automation Scripts on Firefox and Chrome browser only, then XPath Expressions are the best choice over CSS Selectors, as they are more powerful than CSS Selectors.

Hence, in case of Internet Explorer, CSS Selectors are recommended over XPath Expressions. But if the browsers list for running automation scripts don't have Internet Explorer Browser, then powerful XPath Expressions can be used over CSS Selectors. 

Add-ons required for generating Locators


Though we can manually create XPath Expressions and CSS Selectors by understanding their syntax, Firefox browser is providing few add-ons which can be installed and used for any of the below :

  • Automatically generates the XPath Expressions / CSS Selectors
  • Helps us in manually creating the locators in a better way
  • Automatically generates the locators and the required Selenium statement.

Firefox Add-ons: Firebug, Firepath and WebDriver Element Locator are few Add-ons which does this job.





4. Selenium Components



Selenium Tools Set


Selenium is not a single tool, instead it is a set of tools. The below are the four different tools which we together call as Selenium:

  • Selenium IDE
  • Selenium RC
  • Selenium WebDriver
  • Selenium Grid


We can also call these tools set as Selenium components. Its not mandatory to use all these components to automate applications, instead we select them for automation based on our Applications requirement. 


Selenium IDE


Selenium IDE is a 'Record and Playback' tool and is available for us to use in the form of a Firefox Add-on. So, before understanding the Selenium IDE, we have to first know about 'Record and Playback' tools.

In simple terms, Record and Playback tools are used to automate any application by recording the tests and playing back the recorded tests:

Recording the tests

  • Recording the tests is just like recording a video using cellphone
  • Recording in 'Record & Playback' tool is performed to create automated tests
  • Practically speaking 'Record' option of 'Record & Playback' tool will be turned on and then the activities performed on the Application which is displayed in a browser will be recorded until the 'Record' option is turned off.

Playing the recorded tests

  • Playback is just like playing the recorded video
  • Playback in 'Record & Playback' tool is performed to execute the recorded automated tests
  • Practically speaking 'Play' option of 'Record & Playback' tool will be selected to execute the recorded automated tests

Advantages of using 'Record & Playback' tools

  • Very easy for Automating the Tests - Can be used by the non programmers as programming knowledge is not required for using 'Record & Playback' tools
  • Takes less time for Automating the Tests - It wont take much time to automate the tests using 'Record & Playback' tool, as we simply record the application actions over browser and later execute the recorded actions using play options.
  • Suitable for less complex projects - 'Record & Playback' tools are very useful for automating the projects whose complexity is less, as the creation of automation tests will be easy and the time taken to automate the tests will be less when compared to other ways.

Disadvantages of using 'Record & Playback' tools

  • Automation scripts maintenance is more - When any buttons or links or UI changes in the application, we need to re-record all the earlier recorded automation tests which are getting impacted due to the changes. i.e. We need to use 'Record & Playback' tool to re-record all the impacted automation tests. But when we use programming for creating automation scripts, this maintenance will be reduced, as we will update the changes in a single file in the automation framework to which all the tests will refer, instead of editing the changes in all the automation tests which got impacted by these changes. Hence, even though the creation of automation scripts using 'Record & Playback' tool is easy,  the maintenance of the recorded scripts will be high.
  • Re-usability will be less - If we want to automate 100 tests, where all the tests needs to be performed after logging into the application, then using 'Record and Playback' tool we need to record login functionality for all the 100 tests separately. But if we use programing for automating tests instead of ‘Record and Playback’ tool, then as part of framework we can have a single file where we can keep the reusable code for login functionality and use the same reusable code with the 100 tests. Hence the re-usability will be less in case of automating the tests using ‘Record and Playback’ tool.
  • Not suitable for complex applications - Due to the maintenance and re-usability problems in ‘Record and Playback’ tools, automating the test cases for complex Application is not recommended. As complex applications contains huge list of tests and it will be difficult to     re-record the huge list of tests when anything changes in the application. And also due the huge list of scenarios, the scope for re-usability will increase. Hence ‘Record and Playback’ tools are not suitable for complex applications.  

Selenium IDE

Selenium IDE as mentioned earlier is a ‘Record and Playback’ tool and is one of the 4 components of Selenium. Selenium IDE is provided by Firefox in the form of an Add-on / Plug-in and is free to install on your Firefox browser.

"As part of learning Selenium IDE, the basic understanding of using Selenium IDE is enough and is not required to learn in detail."

As you have now understood the usage of ‘Record and Playback’ tool, the following are the different posts using which you can explore the functioning of ‘Selenium IDE’:


Hence Selenium IDE is a ‘Recording and Playback’ tool and is useful for creating automation tests for simpler applications in an easiest and fastest way. And no programming language is required for using this tool.


Selenium RC


Selenium RC used to be the leader in the market for a long period of time until Selenium WebDriver is introduced into the market in 2006. Understanding Selenium RC will help us in understanding the power of the latest Selenium WebDriver. Selenium RC stands for Selenium Remote Control and is nothing but an API. Before understanding Selenium RC, we have to first understand the term API.

API stands for Application Programming Interface. API in simple terms is nothing but a group of functions and wont have any Graphical User Interface. Selenium RC is also an API and has its own set of functions which help us in automating the applications. For example, Selenium RC’s API has a function called click( ), which will help us clicking on the GUI elements on the Web Applications say Buttons, Links etc. Selenium guys did all the hard work and have already written the code for the functioning of methods like click( ) and provided them in the form of an API. We just need to use the click( ) method from the API on the GUI elements like Buttons and Links, to perform operations on them as part of Selenium Automation.

But Selenium RC is an outdated now, almost all projects now moved to a new API known as WebDriver. Since WebDriver API is the latest API of Selenium, we can simply ignore using Selenium RC. Even though Selenium RC is outdated, the below details of Selenium RC are explained for General Knowledge sake.

Server is required for executing the automation scripts 

Selenium Remote Control Server is required for executing the automation scripts as shown below:


Selenium RC Server acts as the middle man between ‘Selenium RC Automation Scripts’ and the Browser as explained below:

  • Need to launch ‘Selenium RC Server’, as Selenium RC Server acts as the middle man between ‘Selenium RC Automation Scripts’ and the Browser. On executing the ‘Selenium RC Automation Scripts’, ’Selenium RC Server’ first injects a JavaScript program called Selenium Core into the browser. Once the Selenium Core is injected into the Browser by ‘Selenium RC Server’, Selenium Core injected into the browser will start receiving instructions by ‘Selenium RC Server’ from your test program. On receiving the     instructions, Selenium Core will execute the instructions on the residing browser using the JavaScript commands. The browser will accept the instructions from Selenium core and the response of the executed instructions will be received by Selenium RC Server. Selenium RC Server will receive the responses of the browser and then displays the results. Selenium RC Server will then fetch the next instructions from the Automation scripts and repeats the same cycle. The below diagram depicts the same.


Selenium WebDriver


Selenium WebDriver is introduced in 2006 and is the latest leader in the market. Selenium WebDriver is nothing but an API. Before understanding Selenium WebDriver, we have to first understand the term API. 

API stands for Application Programming Interface. API in simple terms is nothing but a group of functions and wont have any Graphical User Interface. Selenium WebDriver is also an API and has its own set of functions which help us in automating the applications. For example, Selenium WebDriver’s API has a function called click( ), which will help us clicking on the GUI elements on the Web Applications say Buttons, Links etc. Selenium guys did all the hard work and have already written the code for the functioning of methods like click( ) and provided them in the form of an API. We just need to use the click( ) method from the API on the GUI elements like Buttons and Links, to perform operations on them as part of Selenium Automation.
The older API of Selenium i.e. Selenium RC is an outdated now, almost all projects now moved to a new API known as WebDriver. Since WebDriver API is the latest API of Selenium, we can simply ignore Selenium RC. 

Advantages of Selenium WebDriver over Selenium RC
  • Server is not required for executing the automation scripts
  • Supports Android and iPhone testing
  • Interacts natively with the applications running over supported browsers
  • Has complex and large API
  • API’s are fully object oriented
  • Supports moving mouse cursors
  • Simpler Architecture
  • Faster, as it directly speaks to the browsers
  • Realistic interaction with GUI elements (For example, wont enter text into the disabled textbox)
  • Simpler, as it wont contain duplicate and confusing commands
  • Supports headless browsers like HTMLUnit, PhantomJS etc. 

Selenium WebDriver interacts natively with the Browsers and hence wont require proxy server as a mediator:

Selenium WebDriver interacts with the browsers directly without requiring proxy server as shown below:


Conclusion

Selenium WebDriver is the latest API of Selenium and has successfully replaced the outdated Selenium RC API. The main advantage of Selenium WebDriver is that is natively interacts with the browsers and hence wont require any proxy server as a mediator for executing the automation scripts. 


Selenium Grid


Selenium Grid is neither a tool having GUI, nor an API like Selenium RC / WebDriver. Instead Selenium Grid is just a concept, which will allow us to distribute the multiple tests across multiple machines, multiple Operating Systems and multiple browsers for execution.

Selenium Grid speed up the execution process when there are huge number of tests. i.e. Huge list of tests will be distributed across multiple machines and executed at the same time for reducing the execution time. For example, if one test is executing on machine1, at the same time another test will get executed on machine2. 

Example: Lets say, we have 10 tests where each test takes 1 minute each. If you are not using Selenium Grid, then one test will be executed after another in the same machine and hence takes 10 minutes of time for executing 10 tests. But if we use Selenium Grid to distribute these tests across 5 machines, then 5 tests will be executed at the same time and hence it takes only 2 minutes for executing all the 10 tests. 

Hub and Nodes

Using Hub and Nodes concept, Selenium Grid will distribute the tests across multiple machines, operating systems and browsers. Selenium Grid uses Hub as the central machine and uses it to distribute the tests for execution across multiple machines which are knows as Nodes. The below diagram depicts how Selenium Grid uses Hub and Nodes concept to distribute the tests across multiple machines, OS and browsers:




Versions of Selenium Grid

Selenium Grid has the two versions i.e. Selenium Grid 1 and Selenium Grid 2. Selenium Grid 1 can be used with the outdated Selenium RC and hence Selenium Grid 1 can be ignored. Selenium Grid 2 can be used with the latest Selenium WebDriver and hence it is the latest Selenium Grid version used in the market. 

Conclusion

Selenium Grid is a concept using which we can define multiple machines as Hub & Nodes, and distribute the tests across multiple machines, operating systems and browsers. The purpose of Selenium Grid is to run multiple tests simultaneously across multiple machines to reduce execution time of tests.  





5. Unit Testing Frameworks



Unit Testing


Unit Testing is a Software Testing Methodology in which individual tests are developed for each small unit of a Program. i.e. A Software Program is broken into the possible smallest units and individual tests are written to test all the smallest units. All the tests created for testing the smallest units of the program are known as Unit Test. The Methodology of Testing the Software Program using the created Unit Tests is known as Unit Testing. If you are still not able to visualize what is the term Unit in Unit Testing, lets understand it in a realistic way i.e. Unit is a smallest piece of software code. Still not convinced, lets me give an example of Unit. Example for Unit is a function or method in any programming language. Hope it is clear now :)

Unit Testing Frameworks


There are various Unit Testing Frameworks in the market. The below are few famous Unit Testing Frameworks which can be used with the famous Programming languages.
  • JUnit
  • TestNG
  • NUnit and many more.

The whole and sole purpose of these Unit Testing Frameworks is to make the process of developing and executing the Unit Tests in an easier way. Hence by using Unit Testing Frameworks, we can easily develop and execute the Unit Tests.


Purpose of using Unit Testing Frameworks in Selenium


Before understanding how Unit Testing Frameworks are used in Selenium, we have to understand that Unit testing is generally done by developers. i.e. Developer break the complete application code into smaller units and test all the units separately. As explained in the above sections of this chapter, Unit Testing is a process of breaking the code into smaller units and testing the broken units is known as Unit Testing. Unit Testing can be performed in both manual and by automation.

In order to automate this unit testing process, developer use different Unit Testing Frameworks based on the programming languages they have used. i.e. While the developer continue developing the application code by simultaneously creating unit test for the developed code, the existing unit tests will be automatically run by the Unit Testing Framework on the earlier developed code, to check the newly developed code is not breaking the earlier developed code. As this is done automatically by the Unit Testing frameworks, developers don't have to waste their time in manually testing the units tests on earlier written code, each time they develop new code. Though there are many other advantages of automating the unit testing process using the Unit Testing Framework, the one that I explained in this para is the main purpose of automating the Unit Testing process.

Now, lets understand the purpose of Unit Testing Frameworks in Selenium:

Unit Testing Frameworks play a major role in developing different types of Test Automation Frameworks in Selenium. 

Unit Testing Frameworks can be used in Selenium to automate/support/perform any of the below:

  • Controls the flow of test case execution
  • Grouping the test cases in to separate groups is possible
  • Prioritizing the test cases is possible to prioritize which test needs to be executed first and which next.
  • Parameterizing the tests in such a way that the same tests can run multiple times using the different sets of data
  • Supports reading the data from the external sources like Excel files etc. 
  • Parallel execution of tests to save time of test execution by executing multiple tests at the same time
  • Supports generating text logs to later find-out what are the different things that happened while the tests were executing
  • Supports generating reports to find out the test results after tests execution

The below diagram, depicts the purpose of  Unit Testing Frameworks in Selenium:





Unit Testing Frameworks supported by Selenium


There are various Unit Testing Frameworks in the market. The below are few famous Unit Testing Frameworks which can be used with the famous Programming languages.

As we have understood the purpose of using Unit Testing Frameworks in Selenium Automation, now lets find out the different unit testing frameworks that are supported by Selenium based on different programming languages.

Programming languages and their compatible Unit Testing Frameworks:

The below are the different Unit Testing frameworks that are supported by Selenium and categorized according to their compatible Programming languages.
  • Java : TestNG and JUnit Unit Testing Frameworks
  • C# : NUnit Unit Testing Frameworks
  • PHP: (Behat+Mink) Unit Testing Frameworks
  • Python : unittest , pyunit , py.test and robot framework Unit Testing Frameworks
  • Ruby : RSpec and Test::Unit Unit Testing Frameworks
  • JavaScript : WebDriverJS, WebDriverIO and NightwatchJS

The below diagram depicts the above categorization of Unit Testing frameworks according to different programming languages:



Hence these are different Unit Testing Frameworks supported by Selenium based on the programming languages. And these Unit Testing Frameworks play an important role in building Test Automation Frameworks in Selenium.


TestNG Unit Testing Framework


If we are using Java programming language in Selenium Automation tests, then we have to choose either JUnit or TestNG as our Unit Testing Frameworks. But in Selenium Project, TestNG is preferred over JUnit, as  TestNG Unit Testing Framework is more powerful and is very much suitable for Selenium. 

TestNG is a unit testing framework, which plays a major role in developing Test Automation frameworks using Java Programming language.


Advantage of TestNG Unit Testing Framework


The below are the different advantages of using TestNG Unit Testing Framework:

  • Test Case creation is made easier
  • Groups the Test Cases
  • Prioritizes the Test Cases
  • Executes the Test Cases
  • Generates Logs
  • Generates Reports
  • Reads Keywords / data from the Excel files
  • Supports Parameterization
  • Supports Parallel execution of Tests

The below diagram depicts the advantages of TestNG Unit Testing Frameworks:



Installing TestNG


In order to use TestNG Unit Testing Framework in Selenium Java Project, we have to perform the below tasks:

  • Install TestNG Extension in Eclipse IDE (Click here for detailed explanation)
  • Configure TestNG Jar files in the Selenium Java Project (Click here for detailed explanation)


TestNG Annotations


TestNG provide a huge set of Annotations. The main reason behind TestNG’s popularity is nothing but the TestNG annotations, which can be used for configuring the test cases and test cases in Selenium Automation. There annotations needs to be used with Java methods. i.e. a line before the beginning of the Java methods. When a particular TestNG Annotation is used before the Java Method,  Java method will behave differently based on the specified Annotation. TestNG annotations begin with @ symbol. On using TestNG annotations with Java method, the syntax of the Java method will like below:

Syntax:

@AnnotationType
public void methodName(){

}


Different TestNG Annotations

If you are planning to use TestNG for executing the Selenium Automation code, then the usage of main( )  method in Java is no more required. Using TestNG annotations, we can specify which methods needs to be executed first and which methods can be executed later and so on.  The below are the different TestNG annotations that we use in Selenium Automation:






6. Other Third Party APIs



Third Party APIs


Technically speaking Selenium WebDriver is an API. API is nothing but a set of commands / methods for performing the operations. Selenium WebDriver’s API has set of commands / methods which will help us in performing operations on Web Elements like clicking a button, selection a drop down option, entering text in to the text box fields and so on. 

In Selenium Automation, apart from using its own API commands / methods (i.e. Selenium WebDriver API), we need to use other third party API’s for performing the below:
  • Third party APIs for implementing various Unit Testing Frameworks in Selenium Automation code. Example: TestNG API is a third API having its own commands which help us in implementing Unit Testing in Selenium Automation Frameworks. 
  • Third party APIs for implementing the logging in Selenium Automation code. Logging in Selenium Automation helps us in printing the actions that have taken place while the Selenium Automation code is getting executed. Once the execution is completed, by reading logs in log files, we can understand how the program got executed and also can know the error details if any. Example: Log4j API is a third party API which helps us in collecting the logs into a log file while the Selenium Automation code is executing.
  • Third party APIs for reading the test data from the Excel files. Reading text data from Excel files is generally used in Selenium Automation for two purposes. One of the purpose is in Keyword driven framework where the Keywords related to the current executing test case are read from the excel files and based on that the corresponding Selenium code gets executed. And the other purpose is to execute the same test cases multiple times with different sets of test data that is available in the Excel files. The second purpose is generally used in the Data Driven Framework. Example: POI API is a third party API having its own commands / methods which helps us in reading the test data from the Excel files. 
  • Third party APIs for reporting the results of the executed Selenium Automation tests. Example: Extent Reports API is a third party API which has its own commands / methods which help us in extracting the results the executed test cases and displaying properly in the form of logs, screenshots and results in a beautiful format in a good looking HTML pages. 

Selenium WebDriver API is part of the Selenium project and has commands / methods for performing various operations of the Web Elements like clicking a button, selecting an option from the drop down list, selecting a check box option, entering text into the text field and so on. So in order to automate an application, we don’t have to write the code from scratch, instead we can write the high level code which uses the commands / methods of the Selenium WebDriver API. Developers of Selenium WebDriver API has already done the hard work and has provided us with a set of popular commands / methods which help us in automating the applications. Hence we simply use these commands / methods for automating the application. 

Similar to Selenium WebDriver API, there are other developers who have provided a different API’s for various other things. i.e. There are API’s which help us in extracting logs while the program is executing. And there are API which help us in implementing the Unit Testing in our Program code. And also there are APIs which help us in reading text from the Excel files. And also there are APIs which help us in generating the good looking reports with the advanced results extraction from the executed tests. But these API’s were developed by a different set of developers and hence these API’s are not part of Selenium WebDriver API. While working with Selenium Automation, we have to use the other Third Party APIs for performing various other operations which Selenium WebDriver API wont provide. 


APIs for implementing the Unit Test Frameworks


As we already know from above content that there are various Unit Testing Frameworks which are supported by Selenium based on the programming language which we choose for developing the Selenium Automation code.

The below are the different Unit Testing frameworks that are supported by Selenium and categorized according to their compatible Programming languages.

  • Java : TestNG and JUnit Unit Testing Frameworks
  • C# : NUnit Unit Testing Frameworks
  • PHP: (Behat+Mink) Unit Testing Frameworks
  • Python : unittest , pyunit , py.test and robot framework Unit Testing Frameworks
  • Ruby : RSpec and Test::Unit Unit Testing Frameworks
  • JavaScript : WebDriverJS, WebDriverIO and NightwatchJS

The below diagram depicts the above categorization of Unit Testing frameworks according to different programming languages:



For implementing these Unit Testing Frameworks in Selenium Automation, the developers of these Unit Testing Frameworks are providing the APIs containing the commands / methods. Unit Testing Framework API generally provide Annotations, on using which we can implement Unit Testing Frameworks in Selenium and hence can control the execution of the code based on the Annotations used in the code. If we are using Java programming language in Selenium Automation tests, then we have to choose either JUnit or TestNG as our Unit Testing Frameworks. But in Selenium Project, TestNG is preferred over JUnit, as  TestNG Unit  Testing Framework is more powerful and is very much suitable for Selenium. TestNG is a unit testing framework, which plays a major role in developing Test Automation frameworks using Java Programming language. 


APIs for taking Logs


During the execution of the test cases, we generally print some output into the console by placing Print statements where and there necessary inside the programming code. This process of printing the output into the console while the programming code is getting executed is known as logging. Though there are many advantages of implementing logging in the programming code, but the main purpose of logging is Debugging. Debugging is the process of finding the root cause behind the errors which generally occur when the program is executing.

But unfortunately , the print statements are heavy and we cannot overuse them in the program for logging as they degrade the performance of the complex programs. And another disadvantage of overusing the print statements in the complex programming code is we cannot turn them on and off when and then needed. 

So before finding a better solution for logging, lets first understand various advantages we get implementing logging in Selenium Automation. The below are the major advantages of implementing logging in Selenium Automation: 

  • After the execution of the Automation scripts, by going through the logs we can completely and clearly understand how the automation scripts got executed. 
  • By investigating the logs, we can identify the reasons for the program execution issues and failures. Hence we can perform debugging using the displayed logs.

Selenium WebDriver API wont provide the mechanism for logging. Instead we have to use a third party API for implementing the logging mechanism in Selenium Automation. In Selenium Java Automation, we need to use the third party API known as Log4j. Using Log4j in Selenium Java Automation, we can replace the print statements which are heavier. And also the Log4j is flexible to use as we can turn on and off when needed.

The below are the different steps we need to perform for implementing the Log4j logs in Selenium Java Automation:

  • Downloading log4j Jar file
  • Configuring the downloaded Jar file into the Selenium Java Automation Project
  • Creating log4j properties file using Log4j’s Loggers, Appenders and Layouts. 
  • Using the Log4j API’s commands / methods in the Selenium Java Automation code.

After performing the above steps and executing the Selenium Java Automation code, we can see the logs getting printed into the log files while the Automation code is getting executed. 

Logging plays a major role in Test Automation Frameworks. Irrespective of the Test Automation Frameworks like data driven / keyword driven / hybrid / others, we can implement logging mechanism using Log4j in all the Selenium Java Test Automation Frameworks for a creating better frameworks.

"Selenium WebDriver’s API is not capable enough in implementing the logging mechanism in Selenium Automation. Hence we have to use a 3rd Party API known as Log4j."

APIs for reading test data from Excel Files


Based on the automation requirements in our project, we have to implement reading of test data from Excel files during Selenium Automation.

The below are the two requirements in Selenium Automation, where we need to read the data from the Excel files:

  • Executing the test cases by reading the keywords from the Excel files. 
  • Executing the same test case multiple times by reading different sets of test data from the Excel files

As Selenium WebDriver wont provide the mechanism for reading the data from the Excel files, hence in order to accomplish the above requirements in Selenium Automation, we need to use the below Third Party API’s with Selenium:

  • POI API (The popular one)
  • JXL API

The below factor make POI API the popular and most used one:

  • There are two kinds of Excel files. i.e. XLS file which is an older version of Excel file and XLSX file which is the latest version of Excel file. POI API supports both XLS and XLSX files, but JXL API supports XLS files only.

Steps for using POI API in Selenium Automation:

  • Download the below 5 Jar files (i.e. poi-3.15, poi-ooxml-3.15, poi-ooxml-schemas-3.15, xmlbeans-2.6.0 and log4j-1.2.17) and configure them in the required Selenium Automation Project.
  • Using the commands provided by POI API for reading the data from the Excel files. The below are the different Interfaces and Classes in POI API which provide the required commands for reading the data from the XLS and XLSX Excel files.



APIs for generating Reports


Reports play a major role in generating the verification results (i.e. Pass / Fail) of the executed Test Automation scenarios. The purpose of the verification results displayed in the generated reports is to identify any potential bugs for the failed scenarios.

Irrespective of any Test Automation Frameworks (i.e. Keyword Driven, Data Driven, Hybrid Frameworks etc.), reports play a major role in the creation of the Frameworks. 

By default Selenium don't have any reporting feature. We need to take the help of the 3rd Party APIs for generating Test Execution Results reports in Selenium. But because of this flexibility of integrating different Third Party API's for generating reports in Selenium, there are many different types of reports we can generate in Selenium.

Different types of Reports:

By integrating different 3rd Party APIs into the Selenium code, the below are the different types of reports which can be generated after the execution of Selenium Automation code:




7. Test Automation Frameworks



Introducing Automation Frameworks


Framework is a collection of protocols, rules, guidelines and standards that can be incorporated into the Automation code to resolve the problems which occur while automating complex projects.

Simple projects won't require any framework, but when the projects grow in complexity, the problems in handling or organizing the automation code will grow and it will be a mess for the automation resources to manage all these problems without creating an Automation Framework.

Example: Lets say our application is a complex one and has around 1000 test scenarios. Lets say User need to login into the application, in-order to test all these 1000 test scenarios. In this case, writing the same automation code of login functionality for each and every test scenario will increase the code and at the same time, if anything changes in the login functionality flow, the automation resource needs to modify all the 1000 scenarios. To avoid this problem, we implement re-usability concept as part of Automation Framework. On implementing Re-usability in Automation Frameworks, the repetitive code like 'logging into the application" etc. will be written only once in a single method of a single class. All the tests which require 'logging into the application' will access this single method for login. Hence the code repetition problem will get resolved and at the same time if anything changes in the login functionality, the automation resource will just change the code inside the single login method instead of changing the code for all the tests.

Hence from the above example, we follow few protocols/rules/guidelines/standards like 'Code Re-usability' etc. to create Automation Framework to solve the problems which occur while automating complex projects. Apart from the above example, Automation Frameworks solve many other problems, all these problems and their addressing framework concepts will be explained as part of this chapter.

Understanding a framework using a real life example:

To get more clarity on Automation Frameworks, we need to first understand the concept of generic 'Framework' term using a real life example.

Lets say you are attending a Training Session, and you are asked to follow few of the below guidelines:

  • Participants should occupy their seats 5 minutes before the start of session
  • Bring a notebook and pen to the session
  • Mobile phones should be in silent mode
  • And many more

Though we can conduct a Training sessions, without following any of the above mentioned guidelines. But if we follow the above mentioned guidelines, the below benefits will produce:

  • Training session will start of time 
  • Participants won’t get distracted during the session
  • And many more

Based on the above real life example, a Framework can be defined as a set of protocols/rules/guidelines/standards, on following which produce beneficial results.


Benefits of using Test Automation Frameworks


From the last section, we have understood how a framework provides beneficial results on applying it to a real life example. Now lets understand the benefits / advantages we get on applying a Framework to a Test Automation project.

Before understanding the benefits of a Test Automation Framework, lets understand what exactly is a Test Automation Framework. A Test Automation Framework in simple terms is a set of rules/guidelines/standards/protocols used for creating and designing test automation scripts. On applying these rules/guidelines/standards/protocols while creating and designing test automation scripts, we will get benefited and can efficiently perform the below:

  • Test Development
  • Test Execution
  • Test Reporting

The below are the different benefits we get on applying a Framework to a Test Automation project:

  • Re-usability of Automation code and other things
  • Lower Maintenance of Automation code and other things
  • Duplication of test cases chosen for automation can be avoided
  • Reporting of Test Automation results gets easier

Re-usability of Automation code and other things

If the same code is required by multiple test cases, it’s not a good idea to write the same code for multiple test cases. Instead it is a good idea to write the code once at a particular location and all the other test cases can access it. Lets say the automation code for logging into the application is generally required by almost all the test cases and it is not required to write the automation code for login in each and every required test case. Hence to reuse the login code, we need to create the automation code for login in a reusable method and all the test cases which need to login needs to access the code from the same reusable method. Using Test Automation Framework, we can implement the concept of reusable methods.

Similarly the same locator may also be required by multiple test cases. Similarly it’s a good idea to keep the reusable locators at a central repository and all the test cases requiring the locators can access it from the central repository. Using Test Automation Framework, we can implement a central repository for the reusable locators.

By reusing the automation code and reusing the other things like locators, we can modify only the reusable methods or the locators at the centralized repository, instead of modifying all the test cases having the same code or same locators.

Lower Maintenance of Automation code and other things

Usage of Frameworks in Test Automation leads to the lower maintenance of Automation code and lower maintenance of other things. To understand how, go through the below explanation:

If there is any change to any particular test case, Test Automation framework allows us to update the automation code related to that particular test case without disturbing the other code in the Test Automation Framework. i.e. The user can update the code related to that particular test cases without disturbing the code related to the driver scripts , startup scripts and other test cases etc. in the Test Automation Framework.

Using automation framework, even the locators for identifying the elements can be maintained at a centralized location and all the test cases which needs to use these locators will access the centralized repository. Hence if there are any changes to a locator which is used by multiple test cases, we can simply update the locator only once at the centralized repository, instead of updating all the test cases which are using the changed locator.

And also using automation framework, if the same code is required by multiple test cases (example:  Login code can be required by multiple test cases), we can write this code under a single location know as reusable method and all the test cases which required the code will access this single reusable method. If any changes happen to this reusable code, we just need to update the code inside the reusable method instead of updating all the test cases.

Hence from the above explanation, it is very clear that by implementing a Test Automation Framework in Test Automation projects, we can lower the need for the maintenance of automation code.

Duplication of test cases chosen for automation can be avoided

Using Test Automation frameworks, we can properly organize the test cases that are chosen for automation. As a result of organizing the test cases chosen for automating with the help of a Test Automation Framework, the duplication of test cases will be avoided. Hence Test Automation Frameworks helps us in organizing the automation code, as a result the User won’t get confused and avoids the duplication of test cases.

Reporting of Test Automation results gets easier

Test Results Reports can be generated for each and every test case individually. But Test Automation Framework, helps us to generate a single Test Result Report for all the test cases. i.e. The test results of the executed test cases will be presented in a single report. As explained in the earlier sessions, we have to configure any third party API which help us in generating reports in our Automation Project. Configuring a third party API in our Automation Project is a part of Framework building. Hence Test Automation Framework capable enough to report the test results for all the executed test cases makes the reporting easier. Based on the configured Third party API for report generation, we can also generated logs and screenshots for the failure steps in the same test results reports.

Other Advantages of Test Automation Frameworks

Apart from the above advantages, Test Automation Frameworks also provides the below self explanatory advantages:

  • Test Automation scrips development gets easier 
  • Standardization of Test Automation scripts development
  • Test Automation scripts execution gets easier
  • Test Data can be separated from Test Cases
  • Role based access can be provided for accessing the Automation code
  • Low level commands can be abstracted and wont be visible directly while automating the scripts
  • Many tools which are helpful for Test Automation can be dragged into a single umbrella 
  • Errors and Exceptions can be handled to continue the execution of Automation Scripts without halting
  • Running the Automated tests in parallel is possible
  • Running the Automated tests on different multiple machines at the same time is possible
  • Global Properties like URL of the Application, User-name and Password to login etc. can be set and Automated tests can read them.
  • Latest versions of the tools required by automation can be automatically downloaded
  • Can choose to run a specific set of tests like Functional Tests, Regression Tests, Smoke Tests etc. 
  • Can set the Automated tests to run Continuously when the Application build is ready or based on the regular time interviews
  • And Many more.

Different Types of Test Automation Frameworks


From the last few sections of this book, we got a basic idea of automation framework and its benefits. In this section, we will go through the list of different types of Test Automation Frameworks that are available in the market. The below are the list of different Test Automation Frameworks:

  • Linear Framework
  • Structured Framework
  • Modular Based Framework
  • Library Architecture Framework
  • Data Driven Framework
  • Keyword Driven Framework
  • Hybrid Framework
  • Behavior Driven Development Framework

The below diagram depicts the list of different Test Automation Frameworks:



To be continued ...

No comments: