Selenium - A Brief Overview
Selenium - A Brief Overview
Contact Author
Happy Learning :)
3
Table of Contents
Chapter 1 : Introduction 4
Overview
What is Selenium?
Things that Selenium can automate
Things that Selenium cannot automate
Selenium is an open source and free tool
Different components in Selenium
Browsers supported by Selenium
Operating Systems supported by Selenium
Programming languages supported by Selenium
Different versions of Selenium
History of Selenium
Chapter 2 : Prerequisites 10
Unit Testing
Unit Testing Frameworks
Purpose of using Unit Testing Frameworks in Selenium
Unit Testing Frameworks supported by Selenium
TestNG Unit Testing Framework
Advantages of TestNG Unit Testing Framework
Installing TestNG Unit Testing Framework
TestNG Annotations
Different TestNG Annotations
4
Introduction
Overview
The whole and sole purpose of this eBook is to give you a high level and big picture on Selenium Test
Automation Tool. I believe this eBook 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
book 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.
What is Selenium?
In simple words, Selenium is a Test Automation Tool developed by the company named “ThoughtWorks”
from the year 2004.
Out of all the languages supported, Selenium has more support for Java language and most of the Projects use
Java, C#, Ruby and 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# languages.
• 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 break. This became a drawback for Selenium core as it uses
JavaScript programs to interact with the Application 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 Patrick 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
released 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 Selenium core’s Java Script program is part of the Application code using the Proxy server as a
communicator, but Selenium WebDriver used the concept of native drivers to 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 wont treat Selenium’s JavaScript programs as an external.
The native drivers available for supported browsers are FirefoxDriver, ChromeDriver, InternetExplorerDriver,
SafariDriver and OperaDriver. The development of this drivers is done by Selenium guys, even though the
drivers are native to Browsers.
This eBook is provided by the author of blogs www.QAFox.com
8
Selenium 2
Later Selenium 1 (i.e. Selenium IDE + Selenium RC + Selenium Grid) 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 and Selenium WebDriver. As these
two teams identified that Selenium can become more powerful on merging Selenium RC and Selenium
WebDriver, and finally released the merged version of Selenium as Selenium 2 into the market.
Selenium 3
Selenium 3 is the latest version on 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 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 changes 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 the browser vendors
have taken the ownership to support the drivers which are required to run the automation scripts on the
browser instead of selenium guys supporting their development. i.e. gecko driver is supported by Mozilla,
safari driver is supported by Apple and edge driver is supported by Microsoft.
Contact Author
Happy Learning :)
10
Prerequisites
If you are new to testing, go through the below blog post to get started.
If you want to learn all of the above Automation basics, go through the below post:
OOPS concepts
Primitive Data Types
Variables and Values
Operators
Decision making statements
Iterative Statements
Methods
Constructors
Arrays
String
Access modifiers
Wrapper Classes
Exception Handling
Files
Collections Framework.
And others miscellaneous Core Java concepts.
Having the knowledge on Core Java is enough for Selenium. Its not
required to learn the Advanced Java concepts.
- Arun Motoori
https://www.qafox.com/
HTML
CSS
Xpath
DOM
JavaScript
XML
If you want to learn the HTML concepts, go through the below post:
HTML Basics
Contact Author
Happy Learning :)
13
Locators Priority
As all the different types of locators 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 Selectors 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.
The below is the reason behind recommending CSS Selectors over Xpath Expressions:
But practically speaking, the performance difference between CSS Selectors over Xpath Expressions in
identifying 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 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 Expressions needs to be recommend over CSS Selectors. But the
truth is, in some cases Xpath Expressions are recommend over CSS Selectors and in some other cases,
CSS Selectors are recommend 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 recommend 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 you requirement is to run your Automation Scripts on Firefox and Chrome browsers only, then
Xpath Expressions are the best choice over CSS Selectors, as they are more powerful than CSS Selectors.
Hence, incase 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.
Firefox Add-ons: Firebug, Firepath and WebDriver Element Locator are the Add-ons which does this job.
Contact Author
Happy Learning :)
16
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 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:
Very Easy for Automating the Tests Can be used by the non programmers as programming
knowledge is not required for using ‘Record and Playback’ tools.
Takes less time for Automating the Tests It wont take much time to automate the tests using
‘Record and 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 and 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 tak-
en to automate the tests will be less when compared to other ways.
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 the changes. i.e. We need to use ‘Record and 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 and Playback’ tool is easy, the maintenance of the recorded scrips will be high
Reusability 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 with the 100 tests. Hence
the reusability will be less in case of automating the tests using ‘Record and Playback’ tool.
Not suitable for complex Applications Due to the maintenance and reusability 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 reusability 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 / Plugin and is free to install on your
Firefox browser.
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.
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 instruc-
tions, 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 Automa-
tion scripts and repeats the same cycle. The below diagram depicts the same.
Browser
Selenium RC Automation Code Selenium RC Server
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.
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:
Selenium
WebDriver Code
Browser
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.
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:
NODE1
NODE3
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.
Contact Author
Happy Learning :)
22
JUnit
TestNG
NUnit and many more.
The whole and sole purpose of these Unit Testing Frameworks is to make the process of developing and exe-
cuting the Unit Tests in an easier way. Hence by using Unit Testing Frameworks, we can easily develop and
execute the Unit Tests.
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.
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:
The below diagram, depicts the purpose of Unit Testing Frameworks in Selenium:
The below are the different Unit Testing frameworks that are supported by Selenium and categorized
according to their compatible Programming languages.
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 is a unit testing framework, which plays a major role in developing Test Automation frameworks
using Java Programming language.
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(){
Contact Author
Happy Learning :)