0% found this document useful (0 votes)
10 views

unit 5(Selenium Introduction)

Selenium is a popular open-source tool for automating web applications, consisting of a suite of tools including Selenium IDE, Selenium RC, Selenium WebDriver, and Selenium Grid. It supports multiple programming languages and browsers, making it versatile for various testing needs, although it is limited to web applications and cannot automate desktop or mobile apps. The document also discusses the advantages and disadvantages of Selenium IDE, including its ease of use for beginners and limitations in handling complex applications.

Uploaded by

Anushika Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

unit 5(Selenium Introduction)

Selenium is a popular open-source tool for automating web applications, consisting of a suite of tools including Selenium IDE, Selenium RC, Selenium WebDriver, and Selenium Grid. It supports multiple programming languages and browsers, making it versatile for various testing needs, although it is limited to web applications and cannot automate desktop or mobile apps. The document also discusses the advantages and disadvantages of Selenium IDE, including its ease of use for beginners and limitations in handling complex applications.

Uploaded by

Anushika Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Unit 5

Introduction to selenium

Selenium is one of the most popular open-source automated testing Tools available
today.
 Selenium automates web applications for testing purposes but is certainly not limited to
just that.

Introducing Selenium: Selenium is not a single tool, instead it is a set of different tools
each with a different approach to support test automation.
 Learning all the tools will give you different options for approaching different test
automation problems.
 These tools when used together give many options for locating the UI elements and
comparing expected test results against actual application behavior.
 The following are the four tools that comprise the Selenium's Tools Suite:

1. Selenium IDE
2. Selenium RC (Outdated)
3. Selenium WebDriver
4. Selenium Grid

Few

Good things about the Selenium Automation Tools:

1. Selenium is an open source tool


2. Unlike the proprietary tools its available free of cost.
3. This is the best tool available in the market for web application testing
4. One of the selenium's key features is the support for executing one's tests on multiple
browser platforms like Internet Explorer, Firefox, Google Chrome, Opera, Safari etc.
5. Selenium allows you to control a browser from a language of your choice. You can choose
any one of these Six languages to write automation scripts -> Java, C#, Ruby, Python, Perl and
Php .
6. Selenium is available for Operating Systems such as Windows, Linux and Mac
7. Selenium can be used for functional, compatibility, regression testingand User Acceptance
Testing (Mainly used for Regression Testing).
 Functional Testing refers to the test that verifies a specific action of the software
(Example - Logging into the Gmail is an action)
 Compatibility Testing is performed to verify whether a software works with all other
supported software and hardware.
 Regression Testing is the repetition of the earlier successful tests in order to make sure
that changes to the software has not introduced any new side effects
 User Acceptance Testing is testing an application prior to customer delivery for
functionality and usability using real world scenarios which resemble how the application will be
used by the end users.
Drawbacks of using Selenium Automation Tools:

1. Selenium is a browser based testing tool. It can’t be used for automating the desktop
applications and mobile applications. (i.e. It can be used only for automating the web based
applications)

Different Browsers supported by Selenium:

The following are the list of supported browsers on which Selenium Automation scripts can be
executed:

 Chrome Browser
 Firefox Browser
 Internet Explorer Browser
 Safari Browser
 Opera Browser

Different Operating Systems / Environments supported by Selenium:

The following are the list of supported Operating System Environments on which Selenium
Automation scripts can be executed:

 Microsoft Windows OS
 Apple OS X
 Linux OS

Different Programming Languages supported by Selenium:

The following are the list of programming languages using which the Selenium Automation
scripts can be developed:

 Java (Famous and mostly used)


 Python (Second famous language for Selenium)
 C# (Third famous language for Selenium)
 Ruby (Fourth famous language for Selenium)
 JavaScript
 Haskell
 Objective C
 Perl
 PHP
 R

Different versions of Selenium:

Selenium 1 (Oldest version)


Selenium IDE + Selenium RC + Selenium Grid
Selenium 2 (Older version)
Selenium IDE + Selenium RC + Selenium WebDriver + Selenium Grid
Selenium 3
The latest version of Selenium
Selenium IDE + Selenium WebDriver Latest + Selenium Grid
Selenium 4
Alpha version is not available in the mvnrepository.com
Its not official announced yet
Lets wait for a stable version of Selenium 4 with official announce by Selenium Guys.
What is Selenium IDE ?
The following are the four Selenium Automation tools -

1) Selenium IDE
2) Selenium RC
3) Selenium WebDriver
4) Selenium Grid

Selenium IDE is a firefox extension which is used to record the tests like recording a
video and playback the recorded tests like playing a video.

Using Selenium IDE we can create Selenium Automation Code without writing the code
instead the code gets generated automatically when we record the tests using Selenium
IDE. Where as with Selenium RC and Selenium WebDriver tools we need to write the
code ourselves.

We use Selenium RC and Selenium WebDriver to write automation scripts for complex
projects where as Selenium IDE can only be used to automate simple projects.

Can we ignore Selenium IDE as we can automate all our tests using Selenium 1
and Selenium 2 ?

No, you cannot ignore Selenium IDE. It is the only selenium tool which has record and
playback feature.Selenium 1 and Selenium 2 don't have this feature. We can use
Selenium IDE to automate simple project.

Why to record and playback ?

There are two reasons to record and playback.

Reason#1 - In order to automate simple scripts, the tester may not require Selenium 1
and Selenium 2 tools, hence tester will just record and playback using Selenium IDE.

Reason#2 - While automating tests using Selenium 1 or Selenium 2, the tester may
face problems while writing the automation scripts. In this case the tester will record and
playback the test using the Selenium IDE tool and view the automation code generated
by the tool. He/She will compare his Selenium 1 or Selenium 2 self written code with
Selenium IDE's auto generated code, and make necessary changes to his Selenium 1
or Selenium 2 code in order to make it work.

Advantages of Selenium IDE:


 Easy for beginners i.e. Requires less or no software development
 Takes very less time to automate
 An excellent way of learning Selenium Script Syntax in your desired
programming language.

Disadvantages of Selenium IDE:


 Not suitable for complex applications –
Reason - If something say Application URL gets changed, we need to record all the
tests from the beginning. Where as in Selenium RC and Selenium WebDriver, we can
change it from a single location such that the URL gets updated in all the test
automation scripts. So we cannot maintain the scripts generated using Selenium IDE.
 Repetition of the code
Reason - Lets say there are two tests. Test1 -> Login to Gmail, View Unread Emails
and Logout. Test2 -> Login to Gmail, Send Email and Logout. When we use Selenium
IDE to record these tests it will generate automation code for separate Login and Logout
actions. So code is repeated. Where as in Selenium RC and Selenium WebDriver we
can write automation code for Login & Logout actions and move them to
Methods/Functions. So Test1 will call Login and Logout functions and also Test2 will
call the same Login and Logout functions. Hence code is not repeated.
 Code is not readable as it contains Hard coded values
Reason - Selenium IDE records what ever the actions the User performs while
recording a test, hence the code contains Hard coded values. Lets say the User has
recorded the Test -> Login to Gmail with User Name - arunmotoori and Password -
selenium143 using Selenium IDE. When you read the code that is automatically
generated by the Selenium IDE, it will contain the Hard coded values arunmotoori and
selenium143. By reading the code we dont know what arunmotoori and selenium143
are. Were as in Selenium RC and Selenium WebDriver we can Parameterise these
Hard coded values by replacing them with a valid variable names like Username and
Password. Hence the code becomes readable.
 Cannot create constants –
There are few constants like Application URL, Usernames etc. Since the recorded code
contains Hard Coded values, whenever some functionality gets changed say
Application URL, we need to read all the recorded tests and make changes to all the
tests containing Hard coded Application URL. But where as in Selenium RC and
Selenium WebDriver we can parameterise the hard coded values with readable variable
names say Applicaition_URL in this case. These variables will be saved as constants in
a different place. If the application URL changes in this case, we just update the saved
constant with new URL only once, hence no need to reading the code of all the tests.
 Data Driven tests are not possible
Suppose if you want to run the same Test say -> Login to Gmail with say 100 different
Users. Using Selenium IDE you have to record 100 tests (each test for each User).
Instead if you use Selenium RC or Selenium WebDriver you can create single test by
parenthesizing the User details in the test. Hence you can pass 100 Usernames to the
same variable and run the same test 100 times.
 Hence Framework implementation is not possible
Automation Framework is a set of guidelines like 'Code Re-usability, 'Parametrization'
etc. Hence Framework implementation is not possible with Selenium IDE. It is possible
with Selenium RC and Selenium WebDriver.

Record and Playback using Selenium IDE


Prerequisite - Install Selenium IDE

Recording Process:

1. Launch Firefox and open Selenium IDE from Tools Menu as shown below:
2. Ensure that the Selenium IDE is launched as shown below:

3. After the Selenium IDE is launched, ensure that the Recording Button is turned on by default.
(i.e. Selenium IDE is in recording mode by default on launching) as shown below:
4. While the recording button is enabled, open any website say www.google.com in the Firefox
browser as shown below:

5. In the 'Google' page, click on 'I am feeling lucky' button as shown below:
6. Turnoff the 'Recording' button on the Selenium IDE to stop recording as shown below:

7. Ensure that the recorded commands are displayed under the table tab as shown below:

 Playback Process:

 8. Open any other site say www.yahoo.com in the Firefox browser and Click on the 'Play
current test case' button on the Selenium IDE as shown below:

Click this image to view Full Screen



 9. On clicking the 'Play current test case' button, observe that the recorded commands
are executed automatically in the Firefox Browser

Validating elements using Selenium IDE

There are two mechanism for validating elements that are on the application under test
using Selenium IDE. In order to understand what actually the validation means, please
go through the below examples first:

Examples of Validations:

 Example1: To check whether the UI element exists or not on the Application. If


the automation tool finds that the element is found on the application it results PASS
else it will result FAIL as output
 Example2: To check whether the hidden UI element is really hidden. If the
automation tool finds that this element is not hidden it results FAIL else it will result
PASS.
Two mechanism for validating elements in Selenium IDE:
1. Assert - This allows the test to check whether the element is on the page. If it is
not available then the test will stop on the step that failed and wont execute the
remaining steps.
2. Verify - This allows the test to check whether the element is on the page. If it is
not available then the test will fail the step and carry on execution of the remaining
steps.

Validate elements using Assert mechanism


As we've learnt in the previous post that assert will check whether the element is on the page, if
it is not available, the test will fail the step and wont continue executing the remaining steps.

The following steps will show how assert mechanism validates elements:

Example: Check whether the Google Logo is available on the www.Google.com page. (i.e. The
test will pass if the Google Logo exists on the Google page else it fails).

1. Launch Selenium IDE from Firefox Browser -> Tools Menu


2. Ensure that the Selenium IDE is launched and also ensure that 'Record' option is enabled by
default.
3. Type www.google.com in the Firfox Address bar and press 'Enter' button on the keyboard
4. Ensure that the Google page is displayed in the Firefox Browser
5. In Google Page, right click on the UI element to be validated i.e Google Logo in this example
as shown below:

6. In the right click menu options, select 'Show All Available commands' option as shown below:

7. Ensure that the sub-menu option for the 'Show All Available commands' option is displayed
and select 'AssertElementPresent' option as shown below:
8. In Selenium IDE -> Table tab, ensure that assertElementPresent element is added as shown
below:

9. Click on the 'Record' button to turn off the recording process as shown below:
10. Playback or Run the above recorded validation, by click on the 'Play current test case'
option on the Selenium IDE as shown below:

11. After the test Run, ensure that the 'assertElementPresent' command under the Selenium
IDE -> Table tab is displayed in Green color (i.e.Green means the step got passed and the UI
element is present i.e. Google Logo in this example is present on the Google Page) as shown
below:
12. If the element is not present (i.e. Google Logo in this example), then the step will fail and
displayed in red color as shown below:

13. If in case the test fails as shown in the step 12, all the steps to be
executed after the failed step wont be executed
Validate elements using verify mechanism
The following steps will show how verify mechanism validates elements:

Example: Check whether the Google Logo is available on the www.Google.com page. (i.e. The
test will passes if the Google Logo exists on the Google page else it fails).

1. Launch Selenium IDE from Firefox Browser -> Tools Menu


2. Ensure that the Selenium IDE is launched and also ensure that 'Record' option is enabled by
default.
3. Type www.google.com in the Firfox Address bar and press 'Enter' button on the keyboard
4. Ensure that the Google page is displayed in the Firefox Browser
5. In Google Page, right click on the UI element to be validated i.e Google Logo in this example
as shown below:

6. In the right click menu options, select 'Show All Available commands' option as shown below:

7. Ensure that the sub-menu options for the 'Show All Available commands' option is displayed
and select 'VerifyElementPresent' option

8. In Selenium IDE -> Table tab, ensure that verifyElementPresent element is added .
9. Click on the 'Record' button to turn off the recording process .
10. Playback or Run the above recorded validation, by click on the 'Play current test case'
option on the Selenium IDE
11. After the test Run, ensure that the 'verifyElementPresent' command under the Selenium IDE
-> Table tab is displayed in Green color (i.e.Green means the step got passed and the UI
element is present

12. If the element is not present (i.e. Google Logo in this example), then the step will fail and
displayed in red color as shown below:

13. If in case the test fails as shown in the step 12, all the steps to be executed after the failed
step will also get executed

What are Locators?


 Locators are one of the essential components of Selenium infrastructure,
which help Selenium scripts in uniquely identifying the
WebElements(such as text box, button, etc.)

In Selenium Automation, Locators are used to locate the UI (User Interface) elements of
a page like Text Box field, Button etc.
First lets understand what are the UI elements using the following example:

Example#1 - Suppose we've to automate a test, where the User have to log
in to Facebook

Steps to Log In to Facebook application:

1. Browse www.facebook.com in any browser


2. In 'Facebook' Log In page, enter your email id into the Username text box.
3. Enter your Facebook password into the Password text box.
4. Click on 'Log In' button.
In the above example, Username text box, Password text box and Log In text box are the UI
elements to be identified by the automation tool.

Example#2 - Suppose we've to automate a test, where the User have to search for 'selenium-
by-arun' in Google.

Steps to search for 'selenium-by-arun' in Google:

1. Browser www.Google.com in any browser


2. In 'Google' page, enter text 'Selenium-by-arun' into the 'Search' text box
3. Click on 'Google Search' Button
In the above example 'Search' text box and 'Google Search' button are the UI elements to be
identified by the automation tool.
So now you have understood how to identify the UI elements required by our automation tools
manually on any page. But how can Selenium Automation Tool identify the UI elements for
automating the tests. So the simple answer is by using the Locators. Lets find out what actually
the locators are:

Identifying an UI element on the application screen is an important feature for any automation
tool. Locators allows the Selenium Automation tool to find UI elements on a page that can be
used in our automation tests.

i.e. The Selenium Tool uses Locators to find and match the elements on your page that it needs
to interact with while running the automation scripts.

Different Types of Locators to identify UI elements


Selenium uses different types of locators to identify the UI elements on the page.
The following is the list of locators based on the priority and recommendations to use:

1. ID
2. Name
3. Class Name
4. Link
5. CSS
6. XPath

Practically, most of the people will try to locate the UI element using a unique ID. If
unique ID value is not available, they will use CSS selector locator followed by XPath
locator.
Why different types of locators are used instead of having only one kind of locator to
identify the UI elements ?
While building the application code, it is seen as a good practice to make sure that
every element you need to interact with has an ID attribute and a Name attribute.
Unfortunately, developers may not have provided ID or Name attributes to some
elements in your application. So identifying those kind of elements may not be possible
if we don't use the other type of locators like XPath or CSS selector locators

Tools in Selenium to find out the locators


Lets find out how the following browsers finds out the locators of the UI element:

Internet Explorer Browser -

1. Do we have to install anything in Internet Explorer? The Answer is No. Starting from
Internet Explorer 7 to the latest version of the browser, IE Developer Tools are inbuilt in the IE
browser.
2. Open www.google.com in any Internet Explorer Browser as shown in the screenshot
3. Press 'F12' key on your keyboard
4. Ensure that 'IE Developer Tools' are displayed in the IE browser window as shown below

5. At this point I would like to say that 'IE Developers Tools' shown above are used to
identify the locators while using the IE Browser
Chrome Browser -

1. Do we need to install anything in Chrome Browser? The Answer is No. 'Google Chrome
Developer Tools' are inbuilt in the Chrome Browser
2. Open www.google.com in the chrome browser
3. Press 'F12' on your keyboard
4. Ensure that 'Google Chrome Developer Tools' are displayed in the Chrome Browser as
shown below

How to locate a web element in DOM?

As we all know, the first thing to start with is to find the HTML element in DOM (Document
Object Model), for which we need to grab the locator. We can follow the following steps to
identify the web element in DOM on a web browser:

1. DOM can be accessed in Google Chrome either by pressing F12 or by right click on the web
page and then by selecting Inspect (as shown in the screenshot below).

2. Once we click on the "Inspect option", it will open the Developer Tools console, as shown
below. By default, it will open the "Elements" tab, which represents the
complete DOM structure of the web page. Now, if we hover the mouse pointer over
the HTML tags in the DOM, it will highlight the corresponding elements it represents on the
webpage.

3. Now, the main point is, how do we find the web element in the DOM. Click on the "Mouse
Icon" arrow (as designated by Marke 1 in below screenshot) and then select the web element on
the web page. It will automatically highlight the corresponding HTML element in the DOM.
Suppose we want to find the HTML elements corresponding to the banner image (as shown
below by marker 3). When we select the mouse point and click on the banner image, it will
automatically highlight the corresponding HTML element, as shown by marker 2, in the below
screenshot:

So, this way, we can easily search the HTML element in the DOM corresponding to a web
element on the webpage.
To access all these locators, Selenium provides the “By” class, which helps in locating elements
within the DOM. It offers several different methods (some of which are in the image below)
like className, cssSelector, id, linkText, name, partialLinkText, taName, and xpath, etc.,
which can identify the web elements based on their corresponding locator strategies.

You can quickly identify all the supported locators by Selenium by browsing all the visible
methods on the "By" class, as shown below:

How to use locators to find web elements with Selenium?

How to locate a web element by using the "id" attribute?

"ID" as a locator is one of the most common ways of identifying elements on a web page.
According to W3C, an ID for a web element always needs to be unique. The ID is one of the
fastest and unique ways of locating web elements and is considered as one of the most reliable
methods for determining an element. But with the advancement in technologies and more of
the dynamic web pages, "IDs" are generated dynamically and generally not the reliable way to
locate a web element, as they change for different users.

Let's understand the usage of the "id" attribute for locating a web element with the help of an
example.

Suppose we have the locate the text box of First Name on the web page
"https://demoqa.com/automation-practice-form". When we inspect it in DOM, we see the
following DOM structure:
As we can see, the "Input" HTML tag has the following properties and attributes:

<input required="" autocomplete="off" placeholder="First Name" type="text" id="firstName"


class=" mr-sm-2 form-control">
As we can see, the HTML tag contains the attribute “id” inside the input tag. The id here used is
the “firstName*” which we can use to locate this element in the web page. Now, to find the
“First Name” text box on the web page, we can use the following syntax:

By.id("firstName");
How to locate a web element by using the "name" attribute?
Similar to the id attribute, Selenium also offers users a way to identify the element using
the name attribute. But contrary to id, a web page can have multiple elements with the same
“name” attribute. So, if we are going to use the name attribute for the identification of a web
element, we should always make sure that the name attribute must contain a unique value. If
multiple elements have the same value of the ‘name’ attribute, then, Selenium will just select the
first values in the page which matches the search criteria. So, we always recommend making
sure that the value of the name attribute should be unique when we select it for locating a web
element.

Let's understand the usage of the "name" attribute for locating a web element with the help of an
example.For example, suppose we need to locate the checkbox in the gender section on the web
page "https://demoqa.com/automation-practice-form", as highlighted below:
As we can see, the "Input" HTML tag representing the Male Checkbox has the following
properties and attributes:

<input name="gender" required="" type="radio" id="gender-radio-1" class="custom-control-


input" value="Male">
We can see the attribute ‘name ‘ with value as ‘gender’. We can use the following syntax to
locate the web element using the "By" class.

By.name("gender");

How to locate a web element by using the "CSSselector"?

CSS or Cascading style sheets are used extensively to style webpages and hence can be an
effective medium to locate various web elements. These days most of the web pages are
dynamically designed. Thus its quite challenging to get a unique id, name, or class to locate
element. In this type of situation, CSS selectors can be a great alternative as these are way faster
compared to another locator strategies.The basic syntax of identifying a web element
using CSS is as follows:

css=(HTML Page)[Attribute=Value]
For example, let’s say we want to find the following input textbox using CSS selector.
As we can see, the input element has the following properties and attributes:

<input autocomplete="off" placeholder="Full Name" type="text" id="userName" class=" mr-


sm-2 form-control">
Now, to find the element using the CSS selector, we have to use the following syntax:

By.cssSelector("input[id= ‘userName’]");
Similarly, we can use other attributes along with the tags to define different CSS locators.

How to locate a web element by using the "xpath" attribute?

XPath uses the XML expression to locate an element on the webpage. Similar to CSS selectors,
Xpath is quite useful in locating dynamic elements on a webpage. Xpath can access any element
present in the webpage even when they have dynamic properties.

The basic syntax of identifying a web element using the XPath locator strategy is as follows:

//tag_name[@attribute_value]
Where the tag_name is the name of the tag in the DOM structure, and the attribute is an
attribute of the target element that can identify the web element uniquely.

The XPath for the above example will be://input[@id='userName']

By.xpath("//input[@id='userName']");
Here we have used the input tag and id attribute to identify the element.

Example showing usage of all the locator's strategies in Selenium:Suppose we want to


automate specific actions on the web page: on the "https://demoqa.com/automation-practice-
form".

package TestPackage;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium {
public static void main(String[] args) {
String exePath = "C:\\Selenium\\chromedriver\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
driver.get("https:\\demoqa.com\\");
/**Locate by ID Attribute
* URL - https://demoqa.com/automation-practice-form
*/
driver.get("https://demoqa.com/automation-practice-form");
driver.findElement(By.id("firstName"));
/**
* Locate by Name attribute
* URL - https://demoqa.com/automation-practice-form
*/
driver.get("https://demoqa.com/automation-practice-form");
driver.findElement(By.name("gender"));
/**
* Locate by cssSelector attribute
* URL - https://demoqa.com/text-box
*/
driver.get("https://demoqa.com/text-box");
driver.findElement(By.cssSelector("input[id= ‘userName’]"));
/**
* Locate by xpath attribute
* URL - https://demoqa.com/text-box
*/
driver.get("https://demoqa.com/text-box");
driver.findElement(By.xpath("//input[@id='userName']"));
}}
Now, the interaction with any web application requires the Selenium driver to identify web
elements on the page. Until and unless an element gets correctly identified, it's impossible to
trigger events like click, enter, etc. on any web element. Now to search the web elements,
Selenium provides two methods:

 findElement: find and return a single web element based on the search criteria of the
locator.
 findElements: find and return all the web elements based on the search criteria of the
locator.

Both of these methods accept the object of the "By" class and then return the corresponding web
element. We have used the findElement of the Selenium WebDriver to search the various web
elements and have passed different types of locators by invoking the various method of the "By"
class.

Selenium Grid

 A concept for simultaneously executing tests on multiple machines


and multiple browsers
 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 machine 1, at the same time another test will get executed
on machine 2.

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.

Selenium RC - The outdated legend of Selenium


 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. 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.
Server is required for executing the automation scripts

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.

Introduction to Selenium WebDriver


Selenium WebDriver is a set of open-source APIs, which provided the capabilities to interact with any of
the modern web-browsers and then, in-turn to automate the user actions with that browser. It is an
essential component of the Selenium family.

Selenium is not an independent tool; rather, it is a collection of tools that make the Selenium suite, which
was created when two projects Selenium RC and WebDriver were merged.

WebDriver was integrated with Selenium RC to overcome a few of the limitations of Selenium RC and
has now become the de-facto for Web automation. You must be aware that after Selenium 2, there are
new versions have been released. By now in Jan 2021 they have reached Selenium 4.

Why use Selenium WebDriver for Web Automation?

Feature* Description
Dynamic Web Selenium WebDriver can automate dynamic websites where the content of pages
Page Automation changes by user actions.
Works Close to Browser vendors ship their WebDriver implementation. Hence they are tightly
Browser coupled to the browser giving a better testing experience.
Selenium WebDriver allows you to automate the test cases for all the web
Technology
applications, irrespective of the technology used for the development of the
Agnostic
application under test.
Selenium WebDriver allows QA to mimic user actions on the websites. Selenium
Mimics Real User WebDriver can mimic typical user actions like form filling, clicking, double-clicking,
key-press, as well as advanced user actions like drag and drop, click and hold, etc.
Selenium WebDriver has the most significant advantage when doing cross-browser
Supports Cross testing - where a QA can test for the same website, using the same piece of code on
Browser Testing different browsers. It enables the verification and validation of test cases on multiple
sets of browsers at the same time.
Supports parallel If there are more scripts to be executed on multiple browsers, then doing them one by
Feature* Description
one is time-consuming. So Selenium WebDriver allows parallel execution, using
Execution frameworks like TestNG, so that execution of test cases is faster. This allows large
scale execution of test cases in a short time.
Selenium WebDriver allows a QA to view the live execution of the automated test run
View Execution
on the computer system as well as on any other CI/CD pipeline machine by supporting
Results
functionalities like the screenshot, video-recording of test cases, etc.
Supports modern Selenium WebDriver integrates very well with modern Software Development
development principles like Behaviour Driven Development via integration with the Cucumber
techniques library.

Why Selenium WebDriver is popular?

Apart from the above-mentioned capabilities, WebDriver, being part of the Selenium family, also
encompassed some of the unique characteristics, which adds to its popularity as a web automation tool. A
few of those characteristics are:

 Multi-Browser Compatibility - One of the prime reasons for the popularity of Selenium and
WebDriver is its cross-browser support using the same piece of code. It gives the ability to run a
specific piece of code that mimics a real-world user using a browser's native support to hit direct
API calls without the need for any middleware software or device. The below shows a sample list
of browsers supported:
 Multi-Language Support - Not all testers are well versed with a particular language. Since
Selenium provides support for many languages so a tester can use any language out of the
supported languages and then use WebDriver for automation. This gives the freedom to write
code in the language people are comfortable with.
 Faster Execution - Unlike Selenium RC, WebDriver doesn't depend on a middleware server to
communicate with the browser. WebDriver directs communications with browsers using a
defined protocol (JSON Wire), which enables it to communicate faster than most Selenium tools.
Also since JSON Wire itself uses JSON, which is very lightweight, so the amount of data transfer
per call is very minimum. The below figure shows clearly how the WebDriver communicates
with the Browser:
 Locating Web Elements - In order to perform actions like Click, Type, Drag, and Drop we first
need to identify on which web element (like button, checkbox, drop-down, textarea) we need to
perform an action. To facilitate this, WebDriver has provided methods to identify web elements
using various HTML attributes - like id, name, class, CSS, tag name, XPath, linktext etc.
 Handling dynamic web elements - There are times when there are web elements on-page, which
change with every reload of the page. Since the HTML attributes change, it becomes a challenge
to identify these elements. Selenium provides multiple methods to handle these situations -
o Absolute Xpath - this contains the complete XML path of the element in question.
o Contains( ) - using these functional elements can be searched with partial or full text and
can be used to handle dynamic elements.
o Starts-With( ) - this function is based on finding elements using starting text of the
attribute under question.
 Handling Waiting for Elements - Not all the pages have the same structure. Some are
lightweight, while some have a considerable amount of data handling or AJAX calls. Many times
the web elements take some time to load. To account for this WebDriver has provided multiple
waiting mechanisms that can be used to pause the script execution for a required amount of time
based on certain conditions and then continue once the condition is full-filled. The following
figure shows a sample list that shows the capabilities of WebDriver which helps in handling the
dynamic behavior of web pages.

What are the drawbacks of Selenium WebDriver?

 Requires Programming Knowledge and Expertise .


 No Support for Desktop Applications. - Selenium ecosystem, including WebDriver, was built
for the automation of web-applications. As such if you are looking to automate windows based
applications, you will not be able to do so.
 No Customer Support - Selenium ecosystem, including WebDriver is completely open-source,
which means it is driven by individuals and not by any company. Because of this, there is not a
dedicated support team to look into your issues. If a person is stuck somewhere, there are many
communities, forums which a person can rely on, but that's about it.
 No Built In Object Repository - Paid tools like UFT/QTP provide a centralized location to store
objects/elements, called the Object Repository. This ability is not provided by default in Selenium
WebDriver. This can be overcome using approaches like the Page Object Model, but it requires
considerable coding skills and expertise.
 Lack of built-in reporting - Selenium WebDriver can help you run your automation tests but to
provide a reporting capability, you would need to integrate it with a testing framework like Junit,
TestNG, PyTest, Allure, etc.
 Managing Browser-Selenium Dependencies - Since Selenium has to rely on compatibility
between the browser drivers and the actual browser itself, at many times due to incompatibility or
bugs in either the browser driver or browser, functionality breaks, and users have to rely on
community support to get it fixed.

Understanding of Selenium WebDriver Architecture

Being a part of the overall component system, we deduce that the Selenium WebDriver is not a
standalone testing tool. It comprises various components that are required to run tests. These are the
architectural components of Selenium.
This image tells us about the core selenium webdriver architecture and the major selenium
components which comprise WebDriver.

 Selenium WebDriver Client Libraries / Language Bindings – Software Testers want to select
languages that they are comfortable with. Since WebDriver Architecture supports different
languages, so there are bindings available for a range of languages like Java, C#, Python, Ruby,
PHP, etc
 JSON WIRE PROTOCOL - As per the Selenium Architecture above, the JSON Wire Protocol
facilitates all the communication that is happening in Selenium between the browser and the
code. This is the heart of Selenium. JSON Wire Protocol provides a medium for data transfer
using a RESTful (Representational State Transfer) API which provides a transport mechanism
and defines a RESTful web service using JSON over HTTP.
 Browser Drivers – Since there are various browsers that are supported by Selenium, each
browser has its own implementation of the W3C standard that Selenium provides. As such
browser-specific binaries are available that are specific to the browser and hides the
implementation logic from the end-user. JSONWire protocol establishes a connection between
the browser binaries and the client libraries.
 Browsers – Selenium will be only able to run tests on the browsers if they are locally installed,
either on the local machine or on the server machines.

How Selenium WebDriver works?

When a user writes a WebDriver code in Selenium and executes it, the following actions happen in the
background –

 An HTTP request generates, and it goes to the respective browser driver (Chrome, IE, Firefox).
There is an individual request for each Selenium command.
 The browser driver receives the request through an HTTP server.
 The HTTP server decides which actions/instructions need to execute on the browser.
 The browser executes the instructions/steps as decided above.
 The HTTP server then receives the execution status and then sends back the status to an
automation script, which then shows the result ( as passed or an exception or error).
Basic Steps in a Selenium WebDriver Script
 Create a WebDriver instance.
 Navigate to a webpage.
 Locate a web element on the webpage via locators in selenium.
 Perform one or more user actions on the element.
 Preload the expected output/browser response to the action.
 Run test.
 Record results and compare results from them to the expected output.

How Selenium WebDriver Works


On a high-level, Selenium WebDriver works in three steps:

 Test commands are converted into an HTTP request by the JSON wire protocol.
 Before executing any test cases, every browser has its own driver, which initializes the server.
 The browser then starts receiving the request through its driver.
Let’s take an example with the code snippet below:
WebDriver driver = new ChromeDriver ();

driver. get (https://www.browserstack.com)

As soon as you complete writing your code, execute the program. The above code will result in the
launching of the Chrome browser which will navigate to the BrowserStack website.
Now let us understand what goes behind the scene when you click on Run until the launching of the
Chrome Browser.
Once the program is executed, every line of code/script will get transformed into a URL. The JSON Wire
protocol over HTTP makes this possible. Then this URL is passed to the browser drivers (in our example,
the ChromeDriver). At this point, our client library (Python in our example) translates the code into JSON
format and interacts with the ChromeDriver.
The URL after JSON conversion looks as follows:
https://localhost:8080/{"url":https://www.browserstack.com"}

To receive the HTTP requests, every Browser Driver uses an HTTP server. Once the browser driver
receives the URL, it processes the request by passing it to the real browser over HTTP. And then, all your
commands in the Selenium scripts will be executed.
Run Selenium Tests for Free
Types of Requests:There are two types of requests you might be familiar with – GET and POST.
If it’s a GET request then it results in a response that will be generated at the browser end and it will be
sent over HTTP to the browser driver and eventually, the browser driver with the help of JSON wire
protocol sends it to the UI (Eclipse IDE).
Writing First Selenium Test Case:Before start writing the first Selenium, we need to fulfill a few
pre-requisites as mentioned below:

 Install and Set Up Java


 Downloading WebDriver Java Client
 Configuring Selenium WebDriver in Eclipse

Once done with the above steps, you are now all set to write your First Selenium Test Case. Suppose we
take the next user journey for automation using Selenium WebDriver:

 Firstly, open the browser.


 Secondly, navigate to the ToolsQA Demo Website.
 Thirdly, maximize the browser window.
 After that, retrieve the title of the page.
 Fifthly, log in to the Website by specifying credentials.
 Sixthly, validate the LogOut button is visible.
 Lastly, we logout from the Website.

Note: We will use the login username - 'testuser' and password- 'Password@123' for our test script.
Additionally, you can create a user for yourself if need be.

Let us now begin with the step by step journey of writing our first Selenium test case.

How to open a browser using Selenium WebDriver?

The first step for automating any test case using Selenium WebDriver is "Open a Browser".
The Selenium WebDriver provides multiple drivers for different browsers like ChromeDriver for Chrome
browser, InternetExplorerDriver for IE, GeckoDriver for Firefox browser. Consequently, depending on
your requirement, you can choose the desired driver and instantiate the WebDriver for the specified
browser.

As we know that WebDriver is an interface which can instantiate using the driver class of any of the
browser. A few of the samples for instantiating the WebDriver using Firefox and Chrome driver is as
shown below:

Instantiate WebDriver using Firefox Driver:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
System.setProperty("webdriver.gecko.driver", "<Gecko Driver executable location on your system>");
WebDriver driver = new FirefoxDriver();

Instantiate WebDriver using Chrome Driver:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver", "Chrome Driver executable location on your system");
WebDriver driver = new ChromeDriver();
Subsequently, let's understand the details of the above code snippets:
Import WebDriver packages:
As we can see, both the above code snippets start with two import statements. Java import
statements allow access to classes/methods that we declare in other packages. We can use
the import keyword to import the built-in and user-defined packages in our java source file. Additionally,
in doing so, our class can refer to a class that is in another package by directly using its name.

You can see that the first import statement is common for all the three examples, i.e.,

 import org.openqa.selenium.WebDriver; -- This import statement references the WebDriver


interface that instantiates the browser.

The second import statement, i.e.

 import org.openqa.selenium.firefox.FirefoxDriver; , import


org.openqa.selenium.chrome.ChromeDriver; on the other hand, references the Driver class,
which instantiates browser drivers using the WebDriver interface.

Set path of browser driver:


Selenium 3 onwards, Selenium WebDriver expects an external executable, which will act as a medium of
communication between the Selenium test and the corresponding browser. Moreover, the path of this
executable needs to be explicitly set. There are various means of setting up the paths of the driver's
corresponding to each browser, and one of them is using the "System.setProperty " method, using which
we can set up various driver specific properties, such as "webdriver.chrome.driver
" for chrome and "webdriver.gecko.driver " for firefox.

Object Instantiation of WebDriver


The fourth and most important statement is creating an Object of driver class by referencing
the WebDriver interface. As already said above, this object/instance will call various WebDriver methods
in our test script. Let us consider the chrome example here,

WebDriver driver = new ChromeDriver();

The above line of code would instantiate a Chrome browser in safe mode, with no plugins and
extensions.Once we have created the object of WebDriver corresponding to a specific browser, the next
step is to open our test application in the browser. Subsequently, let's see how we can achieve the same
using Selenium WebDriver:

How to navigate to a web page using Selenium WebDriver?

Once the WebDriver instantiates, we will navigate to the desired web page. There are two methods
in Selenium WebDriver using which you can navigate to a particular web page.

1. driver.get("URL") - Navigates to the URL passed as an argument and waits till the page loads
2. driver.navigate().to("URL") - Navigates to the URL passed as an argument and didn't wait for the
page load. Additionally, it maintains browser history to navigate forward and backward.
You can use either of these two methods, but generally, the get() method is preferred since it would halt
any further action until the page fully loads. We will be using the get() method in our examples.
Moreover, the code for it would look like below-

driver.get("https://demoqa.com/login");
The driver variable would invoke the get() method to navigate to the website URL, which passes as a
String argument.

Generally, when Selenium WebDriver opens the browser, it doesn't open the browser in a full window,
which is how users use any browser. So, even in test automation, we should open the browser in full size.
Let's see how we can achieve the same using Selenium WebDriver:

Maximize browser window using Selenium Webdriver?


When you launch the browser using Selenium WebDriver, it might not launch in full size. We can
anytime maximize the browser window using a simple line of code-

driver.manage().window().maximize();
Where the manage() method returns an instance of the Options interface, now this Options interface has
a window method that returns Window type. We then use the maximize method of the Window
interface to maximize the browser window.

Retrieve the title of the page:


Just like the get() method, the getTitle() is another interesting method offered by WebDriver. It fetches
the title of the currently opened web page and returns the same as a String. In the code below, the driver
variable calls the getTitle() method and stores the values in a string variable 'title'. We are then printing
this string variable on the console window using the Java print statement:

String title = driver.getTitle();


System.out.println("The page title is : " +title);
So, now that we have understood what Selenium WebDriver provides, different browser-specific
methods, and how we can navigate to a specific page, maximize the browser and get the title of the
currently opened webpage in the browser. Now, let's see how Selenium WebDriver can locate a particular
web element on which we need to perform a specific action:

How to locate a web element on a web page using Selenium WebDriver?

The next step towards writing the automation scripts is locating the web elements we want to interact
with. Selenium WebDriver provides various locator strategies using which it can locate various web
elements on a page. The "By" provides all these locators. Class of Selenium WebDriver. For our example
scenario, we have used the XPath locator strategy for locating the userName, password, and login button
elements, as shown below:

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

WebElement uName = driver.findElement(By.xpath("//*[@id='userName']"));


WebElement pswd = driver.findElement(By.xpath("//*[@id='password']"));
WebElement loginBtn = driver.findElement(By.xpath("//*[@id='login']"))
As you can see that the different Web Elements store in variables that would perform actions on these
elements. With this, you will also notice two more import statements added to your code:

 import org.openqa.selenium.By; - It references the By class through which we call the locator
type.
 import org.openqa.selenium.WebElement; - It references the WebElement class that helps
instantiate a new web element.

Now that we have located the web elements, let's see how we can perform various actions, such as type,
click, etc., on these web elements:

How to perform actions on web elements using Selenium WebDriver?

After we have located the web elements and stored them as WebElement instances, we will be performing
actions on them like entering text for the user name and password and clicking on the login
button. Selenium WebDriver provides multiple methods to perform actions on various web elements. A
few of those methods exposed by the WebElement interface are:

 sendKeys() - used to enter text in the web element


 submit() - used to submit a form
 click() - used to perform click on the web element
 clear() - used to clear entered text

For example, we can use the methods as mentioned above, to enter the user name and password, as shown
below:

uName.sendKeys("testuser");
pswd.sendKeys("Password@123");
loginBtn.click();

How to perform validations on web elements using Selenium WebDriver?

Once we perform the corresponding actions on the web elements, the next step should be to perform
validations on certain web elements, which will ensure that the action which we performed execute
successfully and result in the needed state of the element.

For Example, for the test user journey, once we will be able to log in to the ToolsQA Demo Site
successfully, the logout button should be visible. Let's see how we can validate the visibility of the logout
button and click on the logout button if it is visible:

try {

WebElement logoutBtn = driver.findElement(By.xpath("//div[@class='text-right col-md-5


col-sm-12']//button[@id='submit']"));

if(logoutBtn.isDisplayed()){
logoutBtn.click();
System.out.println("LogOut Successful!");
}
}
catch (Exception e) {
System.out.println("Incorrect login....");
}
Where,

 We first locate the logout button using the XPath


 After this, we check if it is displayed using the "isDisplayed()" method.
 If yes, we go on clicking it and printing the success message. If not, we print the error message.
Notice that we are surrounding our code within the try-catch block. Since there might be an
exception in case of incorrect login credentials, our test script would fail. Using the try-
catch block, we can handle such situations and gracefully exit execution.

Note: You can refer to the article on Exception Handling for understanding more about this.

Now that the actions and validations needed for the test script are complete, the final step will be to close
the browser. Let's see how we can close a browser using Selenium WebDriver:

How to close the browser using Selenium WebDriver?

The last step that marks the closing of your test script is the closing of the browser. You can close the
browser using any of the following methods provided by Selenium WebDriver:

 close() - It closes the current browser window.


 quit() - It calls the dispose() method of the WebDriver that closes all the browser windows
opened by the WebDriver and terminates the WebDriver session. It's always recommendable to
use the quit() method as it releases the driver.

The below code is used for the driver to call the quit method.:driver.quit();

Now, we can club all the steps as mentioned above to create the complete test script. The consolidated
code would look like below:

package firstPackage;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyFirstTestClass {

public static void main(String[] args){


//Setting the driver path
System.setProperty("webdriver.chrome.driver", "E:\\Softwares\\chromedriver.exe");

//Creating WebDriver instance


WebDriver driver = new ChromeDriver();

//Navigate to web page


driver.get("https://demoqa.com/login");

//Maximizing window
driver.manage().window().maximize();

//Retrieving web page title


String title = driver.getTitle();
System.out.println("The page title is : " +title);
//Locating web element
WebElement uName = driver.findElement(By.xpath("//*[@id='userName']"));
WebElement pswd = driver.findElement(By.xpath("//*[@id='password']"));
WebElement loginBtn = driver.findElement(By.xpath("//*[@id='login']"));
//Peforming actions on web elements
uName.sendKeys("testuser");
pswd.sendKeys("Password@123");
loginBtn.click();
//Putting implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try { //Locating web element
WebElement logoutBtn =
driver.findElement(By.xpath("//div[@class='text-right col-md-5 col-sm-12']//button[@id='submit']"));
//Validating presence of element
if(logoutBtn.isDisplayed()){
//Performing action on web element
logoutBtn.click();
System.out.println("LogOut Successful!");
}
}
catch (Exception e) {
System.out.println("Incorrect login....");
}
//Closing browser session
driver.quit();
}
}
Now that we have our Selenium WebDriver test case ready, let's see how we can execute this test script:
Difference between Selenium WebDriver and Selenium RC:

In Selenium RC we have to use Selenium RC Server as mediator between the Automation Code and the
Browser. Generally Browsers wont allow any external code to get executed on any Website they are
holding. Since our Selenium Automation code is treated as external code by the Browsers, we required a
kind of mediator who can communicate with the browsers, so that the browser can allow the execution of
Selenium Automation code on the website they are holding. Hence Selenium RC Server acts as a proxy to
allow the Selenium Automation Code to get executed on the Application Under Test in Browsers. So in
Selenium RC we have to make Selenium command calls to the browsers via Selenium RC Server.

But in Selenium WebDriver, we don't require any Server which acts as a mediator. We can make direct
Selenium command calls to the browsers.This is possible because of the Browser drivers used in the
Selenium Automation code. There is a separate driver for each browser. Suppose if we want to run our
Selenium Automation code on Internet Explorer browser, we have to the use the Internet Explorer

WebDriver. Here WebDriver of browser will communicate with the browser. We use WebDriver API
(Application Programming Interface) code in the our Selenium Automation Code and hence our Final
Automation Tests containing the WebDriver API code can directly communicate with the browsers.

If your browser and automation tests will all run on the same machine, then your tests only use the
WebDriver API, you do not need to run the Selenium-Server ( i.e. WebDriver will run the browser
directly).

Selenium RC versus Selenium Web Driver

The following are the differences between the Selenium RC and Selenium Web Driver:

Difference 1:

Selenium RC
Selenium Server is required to start.
Explanation:
Selenium RC don't call the browsers directly instead it uses Selenium Server to communicate
with the Test Browsers on the same machine or different machine. So we've to start the Selenium
Server before running the Selenium RC tests.
How Selenium RC uses Selenium Server ?
Selenium RC works the same way for each supported browser by injecting the JavaScript
functions into the browser using Selenium Server when the browser is loaded and also drives the
application under test in the browser using Selenium Server (Selenium Server uses Java Script to
driver the application under test in the browser).
Drawbacks:
Selenium RC server doesn't use the Browser's built in support for test automation. Hence it has to
use Selenium Server as a mediator.
Selenium Web Driver
Selenium Server is not required to start.
Explanation:
Selenium Web Driver makes direct calls to the Test Browsers on the same machine without using
Selenium Server as a mediator.
Exception Case:
Selenium Server will be used as a mediator by Selenium Web Driver when the Selenium
Automation tests need be run on a different machine (i.e. While implementing Selenium Grid
concept which distributes the Automation tests over multiple machines. This concept may require
the Selenium WebDriver to communicate with the test browsers not on the current machine.
Hence it uses Selenium Server to communicate with all the test browsers which are not on the
current machine.)
How can the Selenium Web Driver make direct calls to the test browsers ?
Selenium Web Driver uses Browser's native support/built in support for test automation. i.e.
While writing the Selenium Web Driver automation tests we also use driver code of the browser
to be tested. Suppose if we have to run our tests on Firefox Browser, then we will use Firefox
Driver code in our Selenium WebDriver automation tests. (Browser Drivers will be explained in
the upcoming posts). Hence usage of Selenium Server is not required when the Automation tests
and the test browsers are on the same machine.

Difference 2: Selenium RC code looks different from the Selenium WebDriver code (Selenium
RC uses different commands to perform the operations when compared to Selenium Web
Driver )

Difference 3: Difference 4:

Selenium RC Selenium RC
Simple and compact API when compared to Less object oriented API.
Selenium Web Driver API. Selenium Web Driver
Selenium Web Driver Complete Object Oriented API.
Complex and a bit large API when compared to
Selenium RC API.
Difference 5: Difference 6:

Selenium RC
Selenium RC
Cannot automate to move the mouse cursor using
Cannot test iPhone/Android applications
Selenium RC automation
Selenium Web Driver
Selenium Web Driver
Can test iPhone/Android applications.
Can automate to move the mouse cursor using
Selenium WebDriver automation.

Benefits of Selenium WebDriver


Here are the major advantages of Selenium WebDriver:

 It supports multiple operating systems like Windows, Mac, Linux, etc.


 It supports multiple programming languages like Java, Python, JavaScript, PHP, C#, etc.
 It is compatible with all modern browsers like Chrome, Firefox, Opera, Safari, and Microsoft Edge. It
is also compatible with the near-retiring Internet Explorer (IE) browser.
 It provides faster completion of test scripts as compared to other tools.

Different Unit Testing Frameworks supported by Selenium


Selenium supports a huge list of Unit Testing Frameworks, based on the programming languages. i.e.
Different programming languages will be compatible with different Unit Testing Frameworks.

Example: TestNG unit testing framework is compatible with Java programming language and NUnit unit
testing framework is compatible with C# programming language.
Purpose of 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. This process of breaking the code into smaller units and
testing 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 dont have to waste their time in manually testing the units tests on
earlier written code, each time they develop new code

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 that are supported by Selenium:

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 categorisation of Unit Testing frameworks according to different
programming languages:
TestNG Tutorial: Introduction To
TestNG Framework
Introduction To Automation
Framework
A framework is a set of rules or guidelines like Coding standards, Test data handling,
etc. that the testing world follows in a systematic manner during Automation scripting to
achieve the best output or results. It comprises of best practices and tools that help the
QA professionals to test and achieve more efficient results.

Testing frameworks are an essential part of any successful automation testing process
as they help in reducing the Maintenance costs, Testing efforts, Code reusability and
many other things to achieve. Here we can achieve the best of Return on Investment
(ROI) by showcasing it to the client along with the benefits that we are achieving.

Benefits of Automation Framework


The major benefits include:
1. Increase the Testing team’s speed and efficiency.
2. Improve the Testing team’s Accuracy.
3. Reducing Test maintenance costs.
4. Maximum test coverage.
5. Code reusability.
Types Of Automation Framework
It is very important to choose the type of framework while chalking out the test plan.
Enlisted below are the most common types of test automation frameworks.

1. Linear Automation Framework: Record and playback framework, no coding is


required.
2. Modular Based Testing Framework: Application is divided into individual modules and
then tested separately. These modules are then combined to build larger tests.
3. Data-Driven Testing Framework: Here, test data is stored externally in the form of
Excel spreadsheets, Text files, CSV files, etc. Test scripts are connected to these files
and the application is tested using these files multiple times with different test data sets.
4. Keyword-Drive Testing Framework: This framework utilizes data tables and self-
descriptive keywords that are also stored in the external files. Keywords represent
various actions like click, login, etc. Keywords are stored in a sequential manner with the
required object and testing is done accordingly.
5. Hybrid Test Automation Framework: This framework is a combination of one or more
frameworks mentioned above.

What Is TestNG?
It is a Test Automation Framework for Java, inspired by JUnit and NUnit with new
introductory functionalities that make it more powerful and easier to use. NG means
Next Generation. It is an open-source and free tool.

You might also like