Day2 Selenium Webdriver
Day2 Selenium Webdriver
4/28/17
SELENIUM WEB DRIVER
Selenium RC is a tool which is written in java that allows a user to construct test scripts for a
web based application in which ever programming language he/she chooses. Selenium RC came
as result to overcome various disadvantages incurred by Selenium IDE or Core.
Loopholes and restrictions which were imposed while using Selenium Core made it difficult for
the user to leverage the benefits of the tool to its totality. Thus it made the testing process a
cumbersome and a far reaching task.
The problem of same origin policy disallows to access the DOM of a document from an origin
that is different from the origin we are trying to access the document.
Origin is a sequential combination of scheme, host and port of the URL. For example, for a URL
http://www.seleniumhq.org/projects/, the origin is a combination of http, seleniumhq.org, 80
correspondingly.
Thus the Selenium Core (JavaScript Program) cannot access the elements from an origin that is
different from where it was launched.
Thus, to test the application using Selenium Core, one has to install the entire application on the
Selenium Core as well as web server to overcome the problem of same origin policy.
Page 1 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
So, In order to govern the same origin policy without the need of making a separate copy of
Application under test on the Selenium Core, Selenium Remote Control was introduced. While
Jason Huggins was demoing Selenium, another fellow colleague at ThoughtWorks named Paul
Hammant suggested a work around of same origin policy and a tool that can be wired up with a
programming language of our choice. Thus Selenium RC came into existence.
Unlike selenium IDE, selenium RC supports a wide range of browsers and platforms.
Workflow Description
Page 2 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
This tutorial describes Eclipse setup to use with Selenium. First download all the necessary
packages:
After Eclipse, extract Selenium RC and Selenium Client Drivers to folder C:\SELENIUM
Page 3 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Navigate url:https://www.eclipse.org/downloads/download.php?
file=/oomph/epp/neon/R/eclipse-inst-win64.exe
The next step is launching Eclipse(execute eclipse.exe from eclipse folder). After few seconds
there will be displayed a window. Here You can create a Workspace. Click on Browse and select
Page 4 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Your work location, make new folder (i.e. C:\ Selenium_JUnit) and click OK.
Now You will be navigated to the Eclipse home screen. Click on Workbench
The next step is Project creation. Click on File -> New -> Project. In wizard click on Java ->
Java Project. Click on Next.
On next screen fill the Project Name field, select “Use default JRE” and click on Finish.
Page 6 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
After click on Finish, the Project will be created. On next window click on Yes and Your project
will be displayed at the left side.
By expanding the project the there will be displayed default “src” folder and under this folder
another folder “JRE System Library”. Here are listed the JRE system library files associated to
the project.
Next step is configuring the Selenium Client driver to this Project. Right Click on created project
at the left side. On new window select Build Path -> Configure Build Path. Click on Libraries tab
and after click on “Add External JARs” button.
Select “Selenium Client Drivers” from “C:\SELENIUM” folder (Selenium Server JAR file
should not be added) and Click on OK button. Now the build path is configured and the project
is ready to be used.
Right Click on “src” in package explorer and select New -> Other…- > Java- > Package and
click on Next.
Page 7 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
On next window You will create a new Java package. Enter Your package name into Name field
and click on Finish.
The package is created and listed in explorer at the left side under the folder src.
Next step is adding a java class. Right Click on “Package” in package explorer and select New ->
Other… – > Java -> Class.
Page 8 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Click on the Next button and on next window fill the Name field and click on Finish. The .java
file is created.
Page 9 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
After creation of first class or test run selenium server from cmd prompt,
Later write sample program to launch browser and site like below and execute
Page 10 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Coming on to the advantages and disadvantages of selenium RC, refer the following figure.
Selenium Grid
Page 11 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
With selenium RC, life of a tester has always been positive and favorable until the emerging
trends raised a demand to execute same or different test scripts on multiple platforms and
browsers concurrently so as to achieve distributed test execution, testing under different
environments and saving execution time remarkably. Thus, catering these requirements selenium
grid was brought into the picture.
Selenium Grid was introduced by Pat Lightbody in order to address the need for executing the
test suites on multiple platforms simultaneously.
It is easy to install TestNG, as it comes as a plugin for Eclipse IDE. Prerequisite for installing
TestNG is your Internet connection should be up & running during installation of this plugin and
Eclipse IDE should be installed in your computer. Please see Download and Install Eclipse to
setup Eclipse to you system.
Steps to follow:
1) Launch the Eclipse IDE and from Help menu, click “Install New Software”.
Page 12 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
3) Type name as you wish, lets take “TestNG” and type “http://beust.com/eclipse/” as location.
Click OK.
4) You come back to the previous window but this time you must see TestNG option in the
available software list. Just Click TestNG and press “Next” button.
Page 13 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
6) You may or may not encounter a Security warning, if in case you do just click OK.
7) Click Next again on the succeeding dialog box until it prompts you to Restart the Eclipse.
10) After restart, verify if TestNG was indeed successfully installed. Right click on you project
Steps:
3). Open index.html, it’ll open in eclipse win down and click on xml file
4). Xml file will be open at right side copy the xml content
Page 14 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
</classes>
</test> <!-- Default test -->
5).Create new file under the project and paste the above code and save it as testngsuite.xml and
modify that xml according to over need like below by adding other testing classes
6).Save the file and right click on the testingsute.xml file and run as testng class and observe all
the testing classes add to the testing file will execute one by one after completion of execution
refresh the project and click on emailable-report.html suite results will be displayed
Selenium WebDriver
Selenium WebDriver was created by yet another engineer at ThoughtWorks named as Simon
Stewart in the year 2006. WebDriver is also a web-based testing tool with a subtle difference
with Selenium RC. Since, the tool was built on the fundamental where an isolated client was
created for each of the web browser; no JavaScript Heavy lifting was required. This led to a
compatibility analysis between Selenium RC and WebDriver. As a result a more powerful
automated testing tool was developed called Selenium 2.
Page 15 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
WebDriver is clean and a purely object oriented framework. It utilizes the browser’s native
compatibility to automation without using any peripheral entity. With the increasing demand it
has gained a large popularity and user base.
Refer the following figure for the advantages and disadvantages of WebDriver.
Selenium 3
With the advent and addition of each new tool in the selenium suite, environments and
technologies became more compatible. Here is an exhaustive list of environments and
technologies supported by selenium tool set.
Page 16 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Supported Browsers
Page 17 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
1).Navigate to http://docs.seleniumhq.org/download/
After downloading selenium web driver zip file extract it on workspace location
Page 18 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
4.) Create new package under the java project and create new TestNG class and write the code
like below to launch Firefox browser and one sample application through web driver
Page 19 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
5).Now right click on the class name and run it, it’ll open Firefox browser and will load
AUT(url provided)
What is Locator?
Locator can be termed as an address that identifies a web element uniquely within the webpage.
Locators are the HTML properties of a web element which tells the Selenium about the web
element it need to perform action on.
There is a diverse range of web elements. The most common amongst them are:
Text box
Button
Drop Down
Hyperlink
Check Box
Radio Button
Page 20 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
What is Locator?
Locator can be termed as an address that identifies a web element uniquely within the webpage.
Locators are the HTML properties of a web element which tells the Selenium about the web
element it need to perform action on.
There is a diverse range of web elements. The most common amongst them are:
Text box
Button
Drop Down
Hyperlink
Check Box
Radio Button
Page 21 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Steps:
Open the fire fox browser, search for fire bug, and navigate to fire fox add-ons page and click on
Add to Firefox button
Steps: Open the fire fox browser, search for fire path, and navigate to fire fox add-ons page and
click on Add to Firefox button
Page 22 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Steps: Open the fire fox browser, search for XPath Checker, and navigate to fire fox add-ons
page and click on Add to Firefox button
After completion of installation restart fire fox browser and open AUT and right click on the
browser and observe Fire Bug(Inspect Element With Firebug),Fire Path(Inspect In FirePath),
XPath checker (View XPath)
Page 23 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Types of Locators
Identifying these elements has always been a very tricky subject and thus it requires an accurate
and effective approach. Thereby, we can assert that more effective the locator, more stable will
be the automation script. Essentially every Selenium command requires locators to find the web
elements. Thus, to identify these web elements accurately and precisely we have different types
of locators.
Before we start with the locators, let me take a moment to introduce the application under test.
We would be using “https://accounts.google.com/” for locating different types of web elements
using different locator types.
Using ID as a Locator
The best and the most popular method to identify web element is to use ID. The ID of an each
element is alleged to be unique.
In this sample, we would access “Email” text box present in the login form at gmail.com.
Page 24 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Step 4: Hover on the web element (Email textbox in our case) on which we desire to perform
some action. In the firebug section, one can see the corresponding html tags being highlighted.
Step 5: Be cognizant about the ID attribute and take a note of it. Now we need to verify if the ID
indentified is able to find the element uniquely and flawlessly.
Alternative approach:
Instead of following step 2 to 4, we can directly locate / inspect the web element by right
clicking on the web element (Email Textbox) whose locator value we need to inspect and
clicking on the option “Inspect Element with Firebug”. Thus, this click event triggers the
expansion of firebug section and the corresponding html tag would be highlighted.
Step 3: Type “id=Email” i.e. the locator value in the target box.
Step 4: Click on the Find Button. If the provided locator value is legitimate then the Email
textbox will be highlighted with yellow color with a florescent green border around the field. If
the locator value provided is incorrect, an error message would be printed in the log pane at the
bottom of Selenium IDE.
Page 26 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Step 5: In order to verify further, user can also execute “type” command against the given target
by providing some value in the “Value” field. If the execution of the command enters the
specified value in the Email text box that means the identified locator type is correct and
accessible.
There is only a subtle difference between using ID as a locator and using classname as a locator.
In this sample, we would access “Need Help?” hyperlink present at the bottom of the login form
at gmail.com.
Step 1: Locate / inspect the web element (“Need help?” link in our case) by right clicking on the
web element whose locator value we need to inspect and clicking on the option “Inspect Element
with Firebug”.
Page 27 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Step 2: Be cognizant about the classname attribute and take a note of it. Now we need to verify if
the classname indentified is able to find the element uniquely and accurately.
------------
Step 1: Type “class= need-help-reverse” in the target box in the Selenium IDE.
Step 2: Click on the Find Button. Notice that the hyperlink will be highlighted with yellow color
with a florescent green border around the field.
Locating a web element using name is very much analogous to previous two locator types. The
only difference lies in the syntax.
In this sample, we would access “Password” text box present in the login form at gmail.com.
Page 28 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Step 1: Type “name= Passwd” in the target box and click on the Find Button. Notice that the
“Password” textbox would be highlighted.
All the hyperlinks on a web page can be indentified using Link Text. The links on a web page
can be determined with the help of anchor tag (<a>). The anchor tag is used to create the
hyperlinks on a web page and the text between opening and closing of anchor tags constitutes the
link text (<a>Some Text</a>).
In this sample, we would access “Create an account” link present at the bottom of the login form
at gmail.com.
Step 1: Locate / inspect the web element (“Create an account” link in our case) by right clicking
on the web element whose locator value we need to inspect and clicking on the option “Inspect
Element with Firebug”.
Step 2: Be cognizant about the text present within the <a> </a> tags and take a note of it. Hence
this text will be used to identify the link on a web page uniquely.
Page 29 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Step 1: Type “link=Create an account” i.e. the locator value in the target box in Selenium IDE.
Step 2: Click on the Find Button. Notice that the link would be highlighted with yellow color
with a florescent green border around the field.
Xpath is used to locate a web element based on its XML path. XML stands for Extensible
Markup Language and is used to store, organize and transport arbitrary data. It stores data in a
key-value pair which is very much similar to HTML tags. Both being mark up languages and
since they fall under the same umbrella, xpath can be used to locate HTML elements.
The fundamental behind locating elements using Xpath is the traversing between various
elements across the entire page and thus enabling a user to find an element with the reference of
another element.
Relative Xpath
Relative Xpath begins from the current location and is prefixed with a “//”.
Absolute Xpath
Absolute Xpath begins with a root path and is prefixed with a “/”.
Page 30 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Key Points:
The success rate of finding an element using Xpath is too high. Along with the previous
statement, Xpath can find relatively all the elements within a web page. Thus, Xpaths can
be used to locate elements having no id, class or name.
Creating a valid Xpath is a tricky and complex process. There are plug-ins available to
generate Xpath but most of the times, the generated Xpaths fails to identify the web
element correctly.
While creating xpath, user should be aware of the various nomenclatures and protocols.
Xpath Checker
Creating Xpath becomes a little simpler by using Xpath Checker. Xpath Checker is a firefox add-
on to automatically generate Xpath for a web element. The add-on can be downloaded and
installed like any other plug-in. The plug-in can be downloaded from
“https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/”.
As soon as the plug-in is installed, it can be seen in the context menu by right clicking any
element whose xpath we want to generate.
Click on the “View Xpath” to see the Xpath expression of the element. An editor window would
appear with the generated Xpath expression. Now user has the liberty to edit and modify the
generated Xpath expression. The corresponding results would be updated cumulatively.
Page 31 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Note that the Xpath Checker is available for other browsers as well.
But re-iterating the fact, that most of the times, the generated Xpaths fails to identify the web
element rightly. Thus, it is recommended to create our own Xpath following the pre defined rules
and protocols.
In this sample, we would access “Google” image present at the top of the login form at
gmail.com.
Step 1: Type “//img[@class=’logo’]” i.e. the locator value in the target box within the Selenium
IDE.
Page 32 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Step 2: Click on the Find Button. Notice that the image would be highlighted with yellow color
with a florescent green border around the field.
AUT: http://newtours.demoaut.com/
Page 33 of 34
SUDHEER REDDY ANKIREDDYGARI
4/28/17
SELENIUM WEB DRIVER
Page 34 of 34