[go: up one dir, main page]

0% found this document useful (0 votes)
302 views98 pages

Selenium Online

This document provides an overview of Selenium, including its history, key components like Selenium WebDriver, Grid, and IDE. It discusses why testing is needed for agile development and continuous integration. The types of testing that can be automated with Selenium are described. Finally, it covers using Selenium WebDriver to run tests on different browsers and find elements on a page.

Uploaded by

Biz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
302 views98 pages

Selenium Online

This document provides an overview of Selenium, including its history, key components like Selenium WebDriver, Grid, and IDE. It discusses why testing is needed for agile development and continuous integration. The types of testing that can be automated with Selenium are described. Finally, it covers using Selenium WebDriver to run tests on different browsers and find elements on a page.

Uploaded by

Biz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 98

Contents

What is Selenium?.................................................................................................................................5
Selenium WebDriver..........................................................................................................................5
API.................................................................................................................................................5
Library............................................................................................................................................5
Driver.............................................................................................................................................5
Framework....................................................................................................................................5
Selenium Grid....................................................................................................................................6
The ‘Hub’ (server):.........................................................................................................................6
‘Node’ (remote device):.................................................................................................................6
Selenium IDE......................................................................................................................................6
Selenium: A History...............................................................................................................................7
2004: Making history in two parts (from Selenium A to B)............................................................7
2005: Selenium RC (Remote Control)............................................................................................7
2006: The Selenium IDE.................................................................................................................7
2007: The Selenium WebDriver (Selenium 2.0).............................................................................8
2008: Multiply by ‘n’: The Selenium Grid.......................................................................................8
2016: Selenium RC gets deprecated and WebDriver becomes standard implementation–aka
Selenium 3.0..................................................................................................................................8
2019: WebDriver becomes a W3C standard protocol...................................................................8
Why do I need Selenium Automation Testing?.....................................................................................8
How Selenium Testing Boosts Agile Development................................................................................9
What is Agile?................................................................................................................................9
Where does testing automation come in:.....................................................................................9
How Selenium Testing is Integral to Continuous Integration/Delivery (CI/CD)....................................10
What is CI/CD?.............................................................................................................................10
Modern web development needs Selenium testing because:.....................................................10
What Types of Testing can be Automated with Selenium?.................................................................11
Compatibility Testing:..................................................................................................................11
Performance Testing:...................................................................................................................11
Integration Testing:.....................................................................................................................11
System Testing:............................................................................................................................11
End-to-end Testing:.....................................................................................................................11
Regression Testing:......................................................................................................................11
Who Uses Selenium?.......................................................................................................................12
Which Browsers can I run Selenium Tests on?................................................................................12
Automation Testing Life Cycle.............................................................................................................13
Why Automated Testing..................................................................................................................13
Test Automation for Web Applications...........................................................................................13
A Comparison between Selenium and QTP(now UFT).........................................................................14
How to Choose the Right Selenium Tool for Your Need......................................................................17
Selenium Features...............................................................................................................................17
Limitations..........................................................................................................................................18
Selenium’s Tool Suite..........................................................................................................................19
1.Selenium Integrated Development Environment (IDE).....................................................19
2. Selenium Remote Control...................................................................................................19
3. Selenium WebDriver............................................................................................................20
4.Selenium Grid.........................................................................................................................21
Selenium WebDriver.........................................................................................................................21
Selenium WebDriver- Architecture.............................................................................................22
Selenium Language Bindings / Selenium Client Libraries...................................................23
JSON Wire Protocol..................................................................................................................23
Browser Drivers.........................................................................................................................23
Browsers....................................................................................................................................23
Selenium WebDriver- Features...................................................................................................24
Explanation of the Code......................................................................................................................25
Import Packages/Statements......................................................................................................25
Instantiating objects and variables..............................................................................................25
Launch Website...........................................................................................................................25
Click on an element.....................................................................................................................26
Selenium WebDriver- Commands.......................................................................................................26
Selenium WebDriver - Browser Commands........................................................................................28
Selenium WebDriver - Navigation Commands.....................................................................................31
Selenium WebDriver - WebElement Commands.................................................................................33
What is Web Element?....................................................................................................................33
Selenium WebDriver- Running test on Chrome Browser.....................................................................40
Selenium WebDriver- Running test on Firefox Browser- Gecko (Marionette) Driver..........................40
What is Gecko Driver?.....................................................................................................................40
1. Using Desired Capabilities........................................................................................................41
2. Using marionette property:.....................................................................................................41
3. Using Firefox Options:..............................................................................................................42
Selenium WebDriver- Running test on IE Browser..............................................................................42
Selenium WebDriver- Running test on Safari Browser........................................................................42
Selenium WebDriver - Find Element and FindElements......................................................................43
Why do you need Find Element/s command?.................................................................................43
FindElement command syntax:...................................................................................................43
FindElements command syntax:..................................................................................................43
Find element Vs Find elements.......................................................................................................44
Selenium WebDriver- Locating Strategies...........................................................................................44
Locating Strategies- (By ID)..........................................................................................................45
Locating Strategies- (By Name)....................................................................................................45
Locating Strategies- (By Class Name)...........................................................................................46
Locating Strategies- (By Tag Name).............................................................................................46
Locating Strategies- (By CSS)........................................................................................................46
Locating Strategies- (By Partial Link Text)....................................................................................50
Locating Strategies- (By XPath)....................................................................................................52
How to Click on Image in Selenium Webdriver................................................................................57
Accessing Image Links..................................................................................................................57
Selenium WebDriver- Handling drop-downs.......................................................................................58
Select in Selenium WebDriver.........................................................................................................58
How to select an option from drop-down menu?.......................................................................59
Handling Radio buttons.......................................................................................................................59
Handling Checkbox..............................................................................................................................60
Assertions............................................................................................................................................62
Hard Assertion.................................................................................................................................62
AssertFalse()................................................................................................................................63
AssertTrue().................................................................................................................................64
AssertEquals()..............................................................................................................................65
AssertNotEquals()........................................................................................................................67
AssertNull()..................................................................................................................................69
AssertNotNull()............................................................................................................................70
SoftAssertion...................................................................................................................................72
Selenium Webdriver - Mouse Click & Keyboard Event: Action Class...................................................72
Handling Keyboard & Mouse Events...............................................................................................72
Building a Series of Multiple Actions...........................................................................................74
Selenium Webdriver - Upload & Download a File...............................................................................75
Uploading Files................................................................................................................................75
Downloading Files............................................................................................................................76
What is Wget?.............................................................................................................................77
Setting up Wget...........................................................................................................................77
Using WebDriver and Wget.........................................................................................................79
Selenium WebDriver- Handling Alerts.................................................................................................81
What is Alert?..................................................................................................................................81
void dismiss()...............................................................................................................................83
void accept()................................................................................................................................83
String getText()............................................................................................................................83
void sendKeys(String stringToSend).............................................................................................83
Selenium WebDriver- Scrolling a web page.........................................................................................84
What is Selenium Grid?.......................................................................................................................85
Architecture of Selenium Grid.........................................................................................................85
Hub..............................................................................................................................................85
Node............................................................................................................................................86
Why Selenium Grid?........................................................................................................................86
How to set up Selenium Grid...........................................................................................................86
Selenium WebDriver - Handle Web Table...........................................................................................88
Reading a HTML Web Table.............................................................................................................88
Accessing Tables:.........................................................................................................................89
Accessing Nested Tables..............................................................................................................89
Using Attributes as Predicates.....................................................................................................90
Shortcut: Use Inspect Element for Accessing Tables in Selenium....................................................92
Handling Dynamic Web Tables Using Selenium WebDriver.............................................................94
Example: Fetch number of rows and columns from Dynamic WebTable....................................95
Example: Fetch cell value of a particular row and column of the Dynamic Table........................95
Example: Get Maximum of all the Values in a Column of Dynamic Table....................................97
What is Selenium?
Selenium is an open-source tool that automates web browsers. It provides a single interface that lets
you write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#,
among others.

A browser-driver then executes these scripts on a browser-instance on your device (more on this in a
moment).

Note: To get started with Selenium latest version (WebDriver-based implementation), you'll only
need a single selenium jar file (selenium-standalone-server-'{'version'}'.jar) to run tests both locally
and on remote devices. This JAR file contains W3C-standard WebDriver API and Selenium Grid,
along with Selenium Server (for existing users of deprecated Selenium RC implementation).

Selenium WebDriver
Also known as Selenium 2.0, WebDriver executes test scripts through browser-specific drivers. It
consists of:

API

Application Programming Interface. Ports test scripts you write in Ruby, Java, Python, or C# to
Selenese (Selenium’s own scripting language), through bindings.

Library

Houses the API and language-specific bindings.Although plenty of third-party bindings exist to
support different programming languages, the core client-side bindings supported by the main project
are: Selenium Java (as selenium jar files), Selenium Ruby, Selenium dotnet (or Selenium C#,
available as .dll files), Selenium Python, and Selenium JavaScript (Node).

Driver

Executable module that opens up a browser instance and runs the test script. Browser-specific–for
instance, Google develops and maintains Chromedriver for Selenium to support automation on
Chromium/Chrome.

Framework

Support libraries for integration with natural or programming language test frameworks, like
Selenium with Cucumber or Selenium with TestNG.

How it Works: The WebDriver protocol has a local end (‘client’) which sends the commands (test
script) to a browser-specific driver. The driver executes these commands on its browser-instance. So,
if the test script calls for execution on Chrome and Firefox, the ChromeDriver will execute the test on
Chrome; the GeckoDriver will do the same on Firefox.
Note: Test scripts execute only when the WebDriver’s client and browser/driver are connected. They
don’t have to be on the same device. To enable test execution on multiple remote drivers, you need
RemoteWebDriver and the Grid.

Selenium Grid
The Grid can minimize test runtime–by executing multiple test scripts on any number of remote
devices at once. This is called parallel testing.

Selenium Grid is a smart server that routes test commands to browser instances on remote devices.
The two main components needed for this (other than the test script from client-side/tester) are:

The ‘Hub’ (server):

Accepts access requests from WebDriver client. Routes JSON test commands to remote drivers on
registered ‘nodes’.

‘Node’ (remote device):

Contains a native OS, browsers, and remoteWebDriver.

How it works: WebDriver-client executes the test on a faraway device through remoteWebDriver.
RemoteWebDriver is like your regular WebDriver, except its two components are the Client (your test
script) and Server (a Java servlet that actually executes the test on the remote device).

In your test script, you define ‘desired capabilities’ (device, platform, browser, etc.) of the node where
the test will execute. The Hub receives this script, runs through the registered nodes to find one that
matches the desired capabilities, and assigns the test to it for execution.

Note: Setting up the Grid is pretty easy, but scaling, configuring, and maintaining its integrity can
take up a lot of resources. Make sure to adopt it after careful consideration.

Selenium IDE
Selenium IDE is a Chrome and Firefox plugin that can log ‘natural’ interactions in the browser and
generate its code in programming languages like C#, Java, Python, and Ruby, as well as Selenese
(Selenium’s own scripting language).

Testers can enable ‘recording’ within the IDE and ‘play out’ the test scenario on the browser. The
IDE can then replay those interactions and highlight any errors (during replay) in red.

Keep in mind that while the plugin is quick and helpful, the code generated is generally too messy to
be used in automation test scripts. So use it for rapid prototyping, but for more serious cross browser
testing, we recommend Selenium WebDriver.
Selenium: A History

A timeline of major events in the evolution of Selenium from an in-house side-project to an open-
source industry standard in browser automation:

2004: Making history in two parts (from Selenium A to B)

 Jason Huggins of ThoughtWorks needs to test his web app’s front-end behavior across
different browsers.
 He develops a tool that works by injecting JavaScript underneath the webpage, allowing the
tester to write code that could ‘automate’ front-end user interactions. This became the
JavaScript TestRunner.
 Although the JS-injection approach couldn’t naturally replicate user interactions (via
keystrokes/mouse movements), it was a workaround for the ‘same-host origin policy’, which
prohibits external JavaScript code from accessing elements from a domain it didn’t originally
reside in. Nonetheless, the tool is positively received by in-house developers and
ThoughtWorks’ clients alike.
 The tool is open sourced due to popular demand.
 To eliminate the need for JS-injections, Huggins, along with colleague Paul Hammant,
discuss the possibility of a ‘server’ component. This server would act as an HTTP proxy and
trick the browser-instance into believing that the test script and the web app under test are
from the same source.
 They develop the server component in Java and the original client-side driver (TestRunner)
gets ported to Ruby.
 This is the original Selenium. Known as Driven Selenium or Selenium B in the evolution
timeline.

2005: Selenium RC (Remote Control)

 Elsewhere (at Bea, specifically), Dan Fabulich and Nelson Sproul begin working on the driver
coder. They eventually mold it into a standalone server that bundled MortBay’s Jetty as
HTTP proxy.
 This becomes ‘Selenium RC (Remote Control)’ or Selenium 1.0. Before we cut to 2.0, there
is another significant development in the form of…

2006: The Selenium IDE

 Shinya Kasatani wraps the Selenium driver code in an IDE module in Firefox browser.
 When it works, he finds that he can run a functional ‘live test’ on a website – interacting with
the browser (as a user would); recording/replaying the interactions and debugging as needed.
 Kasatani donates this tool to Selenium project where it becomes known as the Selenium IDE.
2007: The Selenium WebDriver (Selenium 2.0)

 Back at ThoughtWorks, Simon Stewart diligently codes up separate ‘driver’ clients for every
popular browser, so they’d all support automation with native browser capabilities.
 It pays off. The project becomes famous as the WebDriver.

2008: Multiply by ‘n’: The Selenium Grid

 At ThoughtWorks, Philippe Hanrigou creates a server which would allow testers to access
and run tests on browser instances on any number of remote devices.
 This becomes known as the Grid. Cut to…

2016: Selenium RC gets deprecated and WebDriver becomes standard


implementation–aka Selenium 3.0.

2019: WebDriver becomes a W3C standard protocol.

Why do I need Selenium Automation Testing?

Imagine that a manual tester has this scenario: Checking whether the web app’s signup page
(www.example.com/signup) validates input strings and registers a user successfully in latest versions
of Chrome and Firefox, on Windows 7.

Assume that the signup page has these input fields – username, email address, and password. The
tester will get a Windows 7 desktop and follow these steps, consecutively, on latest versions of
Chrome and Firefox:

1. Enter the URL in the address bar (www.example.com/signup)


2. Enter an invalid string in each input field (email, username, and password)
3. Check whether the input strings were validated against corresponding regexes and
any pre-existing values in the database
4. Enter ‘valid’ strings in each input field; click Sign Up
5. Check whether “Welcome, ‘{‘username’}’“ page showed up
6. Check whether the system database created a new userID for ‘{‘username’}’
7. Mark the test ‘passed’ if it did, ‘failed’ if the signup feature broke anywhere during
the test.

That’s a very basic system test. In the real world, testers are more likely to be checking all user
workflows on www.example.com for breakage, on as many OS-browser combinations as needed to
meet the benchmarked compatibility standards.

Depending on the number of manual testers (and thoroughness of test cases), it may take anywhere
between hours to weeks to be sure that the web app is fully functional.
Modern developers and product teams don’t have that kind of time to allot for testing, but they can’t
set aside exhaustive testing in a hurry to release either. This is why they super-charge their testing
with automation, powered by Selenium.

How Selenium Testing Boosts Agile Development

What is Agile?

Agile is a development methodology. It starts with the simplest working version of the product
design–one that can be continuously improved.

Here’s what a typical Agile workflow looks like:

 Stakeholders agree upon the ‘simplest working’ design of the product.


 The design gets divided into smaller modules.
 Each module is assigned to a cross-functional team of developers, designers, and Quality
Assurance personnel.
 Teams work in sprints to create their modules within a time-frame (‘iteration’)–a window of
one to four weeks.
 At the end of each iteration, finished modules are put together. Tests are run and a functional
product (with minimum bugs) is demonstrated to the stakeholders.
 The stakeholders evaluate project priorities, add customer feedback, and adapt as needed.

The whole cycle begins again with the next iteration and a new set of modules. A ‘market-ready’
product or a new feature will always need multiple iterations.

Where does testing automation come in:

 QAs are involved from early stages to run a series of unit and acceptance tests on modules.
 Integration tests on every iteration ensure that separately coded modules don’t break when put
together.
 Each new iteration requires regression tests (so it doesn’t break the previous working
iteration).

It’s essential to keep track of code as well as test cases, so all iterations are well documented. While
we’re on the subject, you should note that this recurrent testing is a theme in any sub-category of
rapid, iterative development based on Agile, like CI/CD.
How Selenium Testing is Integral to Continuous Integration/Delivery
(CI/CD)

What is CI/CD?

Continuous Integration/Delivery prioritizes delivery of new releases of a build, frequently and


quickly. A project that’s launched remains open to continuous iterations (like Agile).

The only difference is this: the project also remains ready to be shipped at all times (instead of waiting
for iterations to run their course).

A CI/CD pipeline looks like this:

 A developer has code she wants to integrate into the project


 An external CI server does an ‘integration’ test–it grabs the source files and attempts to do a
build with the new code.
 If the build completes successfully, the server packages the changes with source files. If not,
the server notifies members of the team.

CI engines (like Jenkins or Bamboo) have dashboards that display current and previous builds, logs of
previous check-ins and their status (successful/failed), what broke (and when), etc. Everyone remains
informed about any change in code, infrastructure, or configuration. This ensures that deployment
failures are caught (and fixed) early.

Note: There’s a difference between a ‘successful build’ and ‘quality build’. Even if a new integration
is successful, it’s not considered ready to ship until it has passed a series of tests by QA engineers.
That’s where automation testing with Selenium comes in handy.

Selenium automates frequent and recurrent functional, performance, and compatibility testing. This
gives developers near-instant feedback for faster debugging, leaving them with more time to code
business logic for newer versions/features.

Modern web development needs Selenium testing because:

1. It automates repeated testing of smaller components of a large(r) code-base


2. It’s integral to agile development and CI/CD
3. It frees resources from manual testing
4. It’s consistently reliable; catches bugs that human testers might miss
5. It can provide extensive test coverage
6. It’s precise; the customizable error reporting is an added plus
7. It’s reusable; you can refactor and reuse an end-to-end test script every time a new feature
gets deployed.
8. It’s scalable; over time, you can develop an extensive library of repeatable test cases for a
product
What Types of Testing can be Automated with Selenium?

Types of testing that are commonly automated with Selenium are:

Compatibility Testing:

Done by QA professionals/Testers to ensure that the web app meets performance benchmarks on
different browser-OS combinations. For example, testing on different devices (mobile and desktop) to
ensure that the front-end fits to scale (responsive); testing on different browsers to see if video ads
render on the pages as they should.

Performance Testing:

Series of tests done by QA professionals/Testers to ensure that the project meets performance
benchmarks set by the stakeholders. Tester writes a script that checks whether all elements on
homepage load within 2 seconds on different browsers/browser versions.

Integration Testing:

Done by developers to verify that units/modules coded separately (that work on their own), also work
when put together. Parallel Test Calculator, for instance, has separate layers. UI takes input and
business logic calculates the output–then sends it back to UI to display. The tester could verify
whether they are able to relay data/output when integrated.

System Testing:

aka Black Box testing. Done by Testers/QA professionals with no context of the code or any
previously executed tests. Typically centered on a single user workflow. The check-out process on a
product website, for instance, comprises of: validating user credentials, fetching products from Cart,
checking their availability, and validating payment details–before redirecting to the bank website. The
tester could write a script to verify that the entire system is functional.

End-to-end Testing:

Also done by Testers/QA professionals, typically from the user’s point of view. The aim is to verify
that all touchpoints on the web app are functional. From the previous example, the tester could write a
series of test cases to check that sign-up, product search, checkout, review, bookmark, and all other
features function as intended (and fail when invalid values are entered in input fields).

Regression Testing:

A series of tests done to ensure that newly built features work with the existing system. From the
same example, say the product website launches a new feature (promotional codes) that automatically
apply to eligible items before checkout. The tester could write cases to verify that it doesn’t break the
rest of the checkout feature.
Well-written test suites can also automate Smoke and Sanity testing with Selenium.

Note: Selenium testing is not meant to replace manual testing. Testing automation, by its very
definition, automates that which does not merit human evaluation. You can’t automate the testing of
your newly revamped UI for human-usability. But for everything else, there’s Selenium.

Who Uses Selenium?


Short answer: Everyone who cares about the state of their web app.

Part of the reason why Selenium is so popular is its flexibility. Anyone who codes for the web can use
Selenium to test their code/app–from individual freelance developers running a quick series of tests
for debugging to UI engineers doing visual regression tests after a new integration.

In an enterprise environment, testing with Selenium falls under the purview of QA engineers. They
are tasked with writing focused, non-flaky (i.e., deterministic) scripts to maximize test coverage and
accuracy, refactoring old test suites for newer versions of the project, and maintaining test
infrastructure (from the Hub to the test-case library).

They would be the ones creating comprehensive test suites to pinpoint ‘show-stopper’ bugs and
advising stakeholders about updating performance benchmarks for the project. Their end goal is to
ensure maximum test coverage and efficacy, which in turn boosts the overall productivity of the
engineers at work.

Which Browsers can I run Selenium Tests on?


Most desktop/ mobile browsers today have built-in support for automation testing with Selenium.
Consumer browser vendors like Firefox, Chrome, Safari, IE, and Opera develop and ship their
browser’s drivers.

In the years since it was first open-sourced, others have contributed to the Selenium project by adding
third-party drivers for specialized browsers like BlackBerry 10 and HtmlUnit, as well as bindings for
integration with development frameworks like PhantomJS, Qt, etc.

Why Automated Testing


Automation testing has specific advantages for improving long-term efficiency of any
software. The key benefits of test automation are:

o Automated testing has long been considered beneficial for big software
organizations. Although, it is often thought to be too expensive or difficult for
smaller companies to implement.
o Automated testing tools can be programmed to build and execute test scripts
at a specific time without involving any human intervention.For instance,
automated test can be automatically kicked off overnight, and the testers can
analyse the results of the automated the next morning.
o Automated testing tools are able to playback pre-recorded and pre-defined
actions.
o Automation testing supports frequent regression testing.
o It provides rapid feedback to developers.
o It provides unlimited iterations of test case execution.
o It provides disciplined documentation of test cases.
o Automated test generates customized defect reports.
o Less error prone as compared to manual testing.

Automation Testing Life Cycle

Test Automation for Web Applications


If we take a look at the type of software applications prevailing in current market
scenario, most of the software applications are written as web-based applications to
be run in an internet browser. The testing strategy for web-based applications varies
widely among companies and organizations.In an era of highly interactive and
responsive software processes where many organizations are using some form of
agile methodology, test automation is frequently becoming a requirement for software
projects.

The most effective manner to carry out test automation for web application is to
adopt a pyramid testing strategy.This pyramid testing strategy includes automation
tests at three different levels. Unit testing represents the base and biggest
percentage of this test automation pyramid. Next comes, service layer, or API testing.
And finally, GUI tests sit at the top. The pyramid looks something like this:

A Comparison between Selenium and QTP(now UFT)

Quick Test Professional(QTP) is a proprietary automated testing tool previously


owned by the company Mercury Interactive before it was acquired by Hewlett-
Packard in 2006. The Selenium Tool Suite has many advantages over  QTP as
detailed below -

Advantages of Selenium over QTP

Selenium QTP
Open source, free to use, and free
Commercial.
of charge.
Highly extensible Limited add-ons
Can run tests across different Can only run tests in Firefox, Internet
browsers Explorer and Chrome
Supports various operating
Can only be used in Windows
systems
QTP Supports Mobile app test automation
Supports mobile devices (iOS & Android) using HP solution called -
HP Mobile Center
Can execute tests while the browser Needs to have the application under test to
is minimized be visible on the desktop
Can only execute in parallel but using
Can execute tests in parallel. Quality Center which is again a paid
product.

Advantages of QTP over Selenium

Advantages of QTP over Selenium


QTP Selenium
Can test both web and desktop
Can only test web applications
applications
Comes with a built-in object
Has no built-in object repository
repository
Automates at a slower rate because it
Automates faster than Selenium
does not have a native IDE and only third
because it is a fully featured IDE.
party IDE can be used for development
Data-driven testing is more cumbersome
Data-driven testing is easier to
since you have to rely on the programming
perform because it has built-in
language's capabilities for setting values
global and local data tables.
for your test data
Can access controls within the
browser(such as the Favorites bar, Cannot access elements outside of the
Address bar, Back and Forward web application under test
buttons, etc.)
Provides professional customer
No official user support is being offered.
support
Has native capability to export test Has no native capability to export runtime
data into external formats data onto external formats
Parameterization can be done via
Parameterization Support is built
programming but is difficult to implement.
Test Reports are generated No native support to generate test /bug
automatically reports.

Though clearly, QTP has more advanced capabilities, Selenium outweighs QTP in
three main areas:

 Cost(because Selenium is completely free)


 Flexibility(because of a number of programming languages, browsers,
and platforms it can support)
 Parallel testing(something that QTP is capable of but only with use of
Quality Center)
How to Choose the Right Selenium Tool for Your Need

Tool Why Choose?


 To learn about concepts on automated testing and Selenium, including:
 Selenese commands such as type, open, clickAndWait, assert, verify,
etc.
 Locators such as id, name, xpath, css selector, etc.
Selenium  Executing customized JavaScript code using runScript
IDE  Exporting test cases in various formats.
 To create tests with little or no prior knowledge in programming.
 To create simple test cases and test suites that you can export later to
RC or WebDriver.
 To test a web application against Firefox and Chrome only.

 To design a test using a more expressive language than Selenese


 To run your test against different browsers (except HtmlUnit) on
different operating systems.
 To deploy your tests across multiple environments using Selenium
Selenium RC Grid.
 To test your application against a new browser that supports
JavaScript.
 To test web applications with complex AJAX-based scenarios.

 To use a certain programming language in designing your test case.


 To test applications that are rich in AJAX-based functionalities.
WebDriver  To execute tests on the HtmlUnit browser.
 To create customized test results.

 To run your Selenium RC scripts in multiple browsers and operating


Selenium systems simultaneously.
Grid  To run a huge test suite, that needs to complete in the soonest time
possible.

Selenium Features

 Selenium is an open source and portable Web testing Framework.


 Selenium IDE provides a playback and record feature for authoring tests
without the need to learn a test scripting language.
 It can be considered as the leading cloud-based testing platform which helps
testers to record their actions and export them as a reusable script with a
simple-to-understand and easy-to-use interface.
 Selenium supports various operating systems, browsers and programming
languages. Following is the list:
o Programming Languages: C#, Java, Python, PHP, Ruby, Perl, and
JavaScript
o Operating Systems: Android, iOS, Windows, Linux, Mac, Solaris.
o Browsers: Google Chrome, Mozilla Firefox, Internet Explorer, Edge,
Opera, Safari, etc.
 It also supports parallel test execution which reduces time and increases the
efficiency of tests.
 Selenium can be integrated with frameworks like Ant and Maven for source
code compilation.
 Selenium can also be integrated with testing frameworks like TestNG for
application testing and generating reports.
 Selenium requires fewer resources as compared to other automation test
tools.
 WebDriver API has been indulged in selenium whichis one of the most
important modifications done to selenium.
 Selenium web driver does not require server installation, test scripts interact
directly with the browser.
 Selenium commands are categorized in terms of different classes which make
it easier to understand and implement.
 Selenium Remote Control (RC) in conjunction with WebDriver API is known as
Selenium 2.0. This version was built to support the vibrant web pages and
Ajax.

Limitations
 Selenium does not support automation testing for desktop applications.
 Selenium requires high skill sets in order to automate tests more effectively.
 Since Selenium is open source software, you have to rely on community
forums to get your technical issues resolved.
 We can?t perform automation tests on web services like SOAP or REST using
Selenium.
 We should know at least one of the supported programming languages to
create tests scripts in Selenium WebDriver.
 It does not have built-in Object Repository like UTF/QTP to maintain
objects/elements in centralized location. However, we can overcome this
limitation using Page Object Model.
 Selenium does not have any inbuilt reportingcapability; you have to rely on
plug-ins like JUnit and TestNG for test reports.
 It is not possible to perform testing on images. We need to integrate Selenium
with Sikuli for image based testing.
 Creating test environment in Selenium takes more time as compared to
vendor tools like UFT, RFT, Silk test, etc.
 No one is responsible for new features usage; they may or may not work
properly.
 Selenium does not provide any test tool integration for Test Management.

Selenium’s Tool Suite

Selenium is not just a single tool but a suite of software?s, each with a different
approach to support automation testing. It comprises of four major components
which include:

 Selenium Integrated Development Environment (IDE)


 Selenium Remote Control (Now Deprecated)
 WebDriver
 Selenium Grid

1.Selenium Integrated Development Environment (IDE)


Selenium IDE is implemented as Firefox extension which provides record and
playback functionality on test scripts. It allows testers to export recorded scripts in
many languages like HTML, Java, Ruby, RSpec, Python, C#, JUnit and TestNG. You
can use these exported script in Selenium RC or Webdriver.

>Selenium IDE has limited scope and the generated test scripts are not very robust
and portable.
2. Selenium Remote Control
Selenium RC (officially deprecated by selenium)allows testers to write automated web
application UI test in any of the supported programming languages. It also involves
an HTTP proxy server which enables the browser to believe that the web application
being tested comes from the domain provided by proxy server.

Selenium RC comes with two components.

 Selenium RC Server (acts as a HTTP proxy for web requests).

>
 Selenium RC Client (library containing your programming language code).

The figure given below shows the architectural representation of Selenium RC.

Selenium RC had been considered quite effective for testing complex AJAX-based web
user interfaces under a Continuous Integration System.

3. Selenium WebDriver
Selenium WebDriver (Selenium 2) is the successor to Selenium RC and is by far the
most important component of Selenium Suite. SeleniumWebDriverprovides a
programming interface to create and execute test cases. Test scripts are written in
order to identify web elements on web pages and then desired actions are performed
on those elements.

Selenium WebDriver performs much faster as compared to Selenium RC because it


makes direct calls to the web browsers. RC on the other hand needs an RC server to
interact with the web browser.
Since, WebDriver directly calls the methods of different browsers hence we have
separate driver for each browser. Some of the most widely used web drivers include:

 Mozilla Firefox Driver (Gecko Driver)


 Google Chrome Driver
 Internet Explorer Driver
 Opera Driver
 Safari Driver
 HTML Unit Driver (a special headless driver)

Note:Selenium version 2 merged the best features of Selenium RC and Selenium


WebDriver into Selenium WebDriver. The latest release Selenium 3 has new added
features and functionalities

4.Selenium Grid
Selenium Grid is also an important component of Selenium Suite which allows us to
run our tests on different machines against different browsers in parallel. In simple
words, we can run our tests simultaneously on different machines running different
browsers and operating systems.

Selenium Grid follows the Hub-Node Architecture to achieve parallel execution of


test scripts. The Hub is considered as master of the network and the other will be the
nodes. Hub controls the execution of test scripts on various nodes of the network.

Selenium WebDriver
Selenium WebDriver is the most important component of Selenium Tool's Suite. The
latest release "Selenium 2.0" is integrated with WebDriver API which provides a
simpler and more concise programming interface.

The following image will give you a fair understanding of Selenium components and
the Test Automation Tools.
Selenium WebDriver was first introduced as a part of Selenium v2.0. The initial
version of Selenium i.e Selenium v1 consisted of only IDE, RC and Grid. However,
with the release of Selenium v3, RC has been deprecated and moved to legacy
package.

In WebDriver, test scripts can be developed using any of the supported programming
languages and can be run directly in most modern web browsers. Languages
supported by WebDriver include C#, Java, Perl, PHP, Python and Ruby.

Selenium WebDriver performs much faster as compared to Selenium RC because it


makes direct calls to the web browsers. RC on the other hand needs an RC server to
interact with the browser.

WebDriver has a built-in implementation of Firefox driver (Gecko Driver). For other
browsers, you need to plug-in their browser specific drivers to communicate and run
the test. Most commonly used WebDriver's include:

 Google Chrome Driver


 Internet Explorer Driver
 Opera Driver
 Safari Driver
 HTML Unit Driver (a special headless driver)

Selenium WebDriver- Architecture


Selenium WebDriver API provides communication facility between languages and
browsers.

The following image shows the architectural representation of Selenium WebDriver.


There are four basic components of WebDriver Architecture:

 Selenium Language Bindings


 JSON Wire Protocol
 Browser Drivers
 Real Browsers

Selenium Language Bindings / Selenium Client Libraries


Selenium developers have built language bindings/Selenium Client Libraries in order
to support multiple languages. For instance, if you want to use the browser driver in
java, use the java bindings. All the supported language bindings can be downloaded
from the official website (https://www.seleniumhq.org/download/#client-drivers) of
Selenium.

JSON Wire Protocol


JSON (JavaScript Object Notation) is an open standard for exchanging data on web. It
supports data structures like object and array. So, it is easy to write and read data
from JSON. To learn more about JSON, visit https://www.javatpoint.com/json-tutorial

JSON Wire Protocol provides a transport mechanism to transfer data between a server
and a client. JSON Wire Protocol serves as an industry standard for various REST web
services. To learn more about Web Services, visit https://www.javatpoint.com/web-
services-tutorial

Browser Drivers
Selenium uses drivers, specific to each browser in order to establish a secure
connection with the browser without revealing the internal logic of browser's
functionality. The browser driver is also specific to the language used for automation
such as Java, C#, etc.

When we execute a test script using WebDriver, the following operations are
performed internally.
1. HTTP request is generated and sent to the browser driver for each Selenium
command.
2. The driver receives the HTTP request through HTTP server.
3. HTTP Server decides all the steps to perform instructions which are executed
on browser.
4. Execution status is sent back to HTTP Server which is subsequently sent back
to automation script.

Browsers
Browsers supported by Selenium WebDriver:

 Internet Explorer
 Mozilla Firefox
 Google Chrome
 Safari

Selenium WebDriver- Features


Some of the most important features of Selenium WebDriver are:

 Multiple Browser Support: Selenium WebDriver supports a diverse range of


web browsers such as Firefox, Chrome, Internet Explorer, Opera and many
more. It also supports some of the non-conventional or rare browsers like
HTMLUnit.

 Multiple Languages Support: WebDriver also supports most of the


commonly used programming languages like Java, C#, JavaScript, PHP, Ruby,
Pearl and Python. Thus, the user can choose any one of the supported
programming language based on his/her competency and start building the
test scripts.
 Speed: WebDriver performs faster as compared to other tools of Selenium
Suite. Unlike RC, it doesn't require any intermediate server to communicate
with the browser; rather the tool directly communicates with the browser.

o Simple Commands: Most of the commands used in Selenium WebDriver are


easy to implement. For instance, to launch a browser in WebDriver following
commands are used:
WebDriver driver = new FirefoxDriver(); (Firefox browser )
WebDriver driver = new ChromeDriver(); (Chrome browser)
WebDriver driver = new InternetExplorerDriver(); (Internet Explorer
browser)
o WebDriver- Methods and Classes: WebDriver provides multiple solutions to
cope with some potential challenges in automation testing.
WebDriver also allows testers to deal with complex types of web elements
such as checkboxes, dropdowns and alerts through dynamic finders.

Explanation of the Code

Import Packages/Statements
In java, import statements are used to import the classes present in another
packages. In simple words, import keyword is used to import built-in and user-
defined packages into your java source file.
o org.openqa.selenium.WebDriver - References the WebDriver interface
which is required to instantiate a new web browser.
o org.openqa.selenium.chrome.ChromeDriver - References the
ChromeDriver class that is required to instantiate a Chrome-specific driver
onto the browser instantiated by the WebDriver class.

Instantiating objects and variables


A driver object is instantiated through:
WebDriver driver=new ChromeDriver(); 

Launch Website
To launch a new website, we use navigate().to() method in WebDriver.
driver.navigate().to("http://www.google.com/");

Click on an element
In WebDriver, user interactions are performed through the use of Locators which we
would discuss in later sessions of this tutorial. For now, following instance of code is
used to locate and parse values in a specific web element.
driver.findElement(By.id("lst-ib")).sendKeys("javatpoint tutorials");  

Selenium WebDriver- Commands

As we have discussed earlier in the IDE section, Selenium commands are the set of
commands that are used to run our Selenium tests.

In Selenium WebDriver, we have an entirely different set of commands for performing


different operations. Since we are using Selenium WebDriver with Java, commands
are simply methods written in Java language.

Note: A java method is a collection of statements that are grouped together


to perform a specific operation.

Before getting into the details of commands provided by Selenium WebDriver, we


insist you to go through the Java OOPs (Object-Oriented Programming) concepts in
java programming language. You can also refer to our Java OOPs concepts section
provided in the Java Tutorial.

Now, the question arises is that how can we access methods provided by WebDriver.

Till now, we have successfully created our first test script in Selenium WebDriver.
Therefore, one possible way to view the methods provided by WebDriver is to open
the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for
WebDriver and press the dot key. It will show you all of the possible methods
provided by WebDriver.

Let us consider an example form the suggestions displayed by Eclipse to understand


the syntax of the methods provided by WebDriver.

Method Name

To access any method of any class, we need to create an object of class and then all
the public methods will appear for the object.

Parameter

A parameter is an argument which is passed to a method to perform some specific


operation.

Return type

Methods can return a value or returning nothing (void). If the void is mentioned after
the method, it means, the method is returning no value. If it is returning a value,
then it must display the type of the value for e.g. getTitle(): String.

Now, we will discuss the various commands provided by WebDriver. The commands provided by
Selenium WebDriver can be broadly classified in following categories:

1. Browser Commands
2. Navigation Commands
3. WebElement Commands

Given below are some of the most commonly used Selenium commands in WebDriver:

1. Fetching a web page


There are two methods to fetch a web page:

 Using Get method

driver.get("www.javatpoint.com")  

 Using Navigate method

driver.navigate().to("https://javatpoint.com/selenium-tutorial");
2. Locating forms and sending user inputs

driver.findElement(By.id("lst-ib")).sendKeys("javatpoint tutorials");

3. Clearing User inputs

The clear() method is used to clear the user inputs from the text box.

driver.findElement(By.name("q")).clear();

4. Fetching data over any web element

Sometimes we need to fetch the text written over a web element for performing some
assertions and debugging. We use getText() method to fetch data written over any web
element.

driver.findElement(By.id("element567")).getText();

5. Performing Click event

The click() method is used to perform click operation on any web element.

driver.findElement(By.id("btnK")).click();

6. Navigating backward in browser history

driver.navigate().back();

7. Navigating forward in browser history

driver.navigate().forward();

8. Refresh/ Reload a web page

driver.navigate().refresh();

9. Closing Browser

driver.close();

10. Closing Browser and other all other windows associated with the driver

driver.quit();

11. Moving between Windows

driver.switchTo().window("windowName");

12. Moving between Frames

driver.switchTo().frame("frameName");
13. Drag and Drop

Drag and Drop operation is performed using the Action class.

WebElement element = driver.findElement(By.name("source"));  

WebElement target = driver.findElement(By.name("target"));  

(new Actions(driver)).dragAndDrop(element, target).perform();  

Selenium WebDriver - Browser Commands

The very basic browser operations of WebDriver include opening a browser; perform
few tasks and then closing the browser.

Given are some of the most commonly used Browser commands for Selenium
WebDriver.

14. Get Command

Method:

get(String arg0) : void

In WebDriver, this method loads a new web page in the existing browser window. It
accepts String as parameter and returns void.

The respective command to load a new web page can be written as:

driver.get(URL);

// Or can be written as

String URL = "URL";

driver.get(URL);

Example: For instance, the command to load the official website of javaTpoint can be
written as:

driver.get("www.javatpoint.com")

15. Get Title Command

Method:
getTitle(): String

In WebDriver, this method fetches the title of the current web page. It accepts no
parameter and returns a String.

The respective command to fetch the title of the current page can be written as:

driver.getTitle();

// Or can be written as

String Title = driver.getTitle();

16. Get Current URL Command

Method:

getCurrentUrl(): String

In WebDriver, this method fetches the string representing the Current URL of the current
web page. It accepts nothing as parameter and returns a String value.

The respective command to fetch the string representing the current URL can be written
as:

driver.getCurrentUrl();

//Or can be written as

String CurrentUrl = driver.getCurrentUrl();

17. Get Page Source Command

Method:

getPageSource(): String

In WebDriver, this method returns the source code of the current web page loaded on
the current browser. It accepts nothing as parameter and returns a String value.

The respective command to get the source code of the current web page can be written
as:

driver.getPageSource();
//Or can be written as

String PageSource = driver.getPageSource();

18. Close Command

Method:

close(): void

This method terminates the current browser window operating by WebDriver at the
current time. If the current window is the only window operating by WebDriver, it
terminates the browser as well. This method accepts nothing as parameter and returns
void.

The respective command to terminate the browser window can be written as:

driver.close();

19. Quit Command

Method:

quit(): void

This method terminates all windows operating by WebDriver. It terminates all tabs as
well as the browser itself. It accepts nothing as parameter and returns void.

The respective command to terminate all windows can be written as:

driver.quit();

Selenium WebDriver - Navigation Commands

WebDriver provides some basic Browser Navigation Commands that allows the browser
to move backwards or forwards in the browser's history.
Just like the browser methods provided by WebDriver, we can also access the navigation
methods provided by WebDriver by typing driver.navigate() in the Eclipse panel.

Note: The methods having 'Navigation' as keyword are declared as Navigation


commands.

Given are some of the most commonly used Browser Navigation commands for Selenium
WebDriver.

20. Navigate To Command

Method:

to(String arg0) : void

In WebDriver, this method loads a new web page in the existing browser window. It
accepts String as parameter and returns void.

The respective command to load/navigate a new web page can be written as:

driver.navigate().to("www.javatpoint.com");

Note: The get command (driver.get(URL);) which lies in the browser commands section
does the same function as the navigate command
(driver.navigate().to("www.javatpoint.com");

21. Forward Command

Method:

forward (String arg0) : void

In WebDriver, this method enables the web browser to click on the forward button in the
existing browser window. It neither accepts anything nor returns anything.

The respective command that takes you forward by one page on the browser's history
can be written as:

driver.navigate().forward();

22. Back Command

Method:

back() : void

In WebDriver, this method enables the web browser to click on the back button in the
existing browser window. It neither accepts anything nor returns anything.

The respective command that takes you back by one page on the browser's history can
be written as:

driver.navigate().back();

23. Refresh Command

Method:

refresh() : void

In WebDriver, this method refresh/reloads the current web page in the existing browser
window. It neither accepts anything nor returns anything.

The respective command that takes you back by one page on the browser's history can
be written as:

driver.navigate().refresh();
Selenium WebDriver - WebElement Commands

Before proceeding with this section, first we should know the basic terminology related
to web elements in WebDriver.

What is Web Element?


The term web element refers to a HTML element. The HTML documents are composed of
HTML elements. It consists a start tag, an end tag and the content in between. For
instance, a HTML element is written as: "<tagname> content </tagname>"

In WebDriver, we have several commonly used web element commands and actions. The
following screenshot displays the eclipse web element command panel.

Note: To get the web element object, we have to write the statement as:

WebElement element = driver.findElement(By.id("UserName"));

Here, the UserName is the value of the id attribute, used as a unique identification for
the desired web element.
Given are some of the most commonly used WebElement commands for Selenium
WebDriver.

24. Clear Command

Method:

clear() : void

Command:

element.clear();

25. Sendkeys Command

Method:

sendKeys(CharSequence? KeysToSend) : void

Command:

element.sendKeys("text");

26. Click Command

Method:

click() : void
Command:

element.click();

27. IsDisplayed Command


isDisplayed( ) : boolean – This method determines if an element is currently being displayed
or not. This accepts nothing as a parameter but returns boolean value(true/false).
Command – element.isDisplayed();

Method:

isDisplayed() : boolean

Command:

element.isDisplayed();

28. IsEnabled Command


isEnabled( ) : boolean – This determines if the element currently is Enabled or not? This
accepts nothing as a parameter but returns boolean value(true/false).
Command – element.isEnabled();

Method:

isEnabled() : boolean

Command:

element.isEnabled();
29. IsSelected Command
sSelected( ) : boolean – Determine whether or not this element is selected or not. This
accepts nothing as a parameter but returns boolean value(true/false).
Command – element.isSelected();
This operation only applies to input elements such as Checkboxes, Select Options and Radio
Buttons. This returns True if the element is currently selected or checked, false otherwise.

Method:

isSelected() : boolean

Command:

element.isSelected();

30. Submit Command

Method:

submit() : void
Command:

element.submit();

31. GetText Command


It gets visible text for an element.

Method:

getText() : String

Command:

element.getText();

32. GetTagName Command


It gets the tag name of the current element.

Method:

getTagName() : String

Command:

element.getTagName();
33. getCssValue Command

Method:

getCssvalue() : String

Command:

element.getCssValue();

34. getAttribute Command

Method:

getAttribute(String Name) : String

Command:

element.getAttribute();

35. getSize Command


It Returns width and height of the element.

Method:

getSize() : Dimension

Command:

element.getSize();
36. getLocation Command
Element is where on the page

Method:

getLocation() : Point

Command:

element.getLocation();

Selenium WebDriver- Running test on Chrome Browser

Chrome browser implements the WebDriver protocol using an executable called


ChromeDriver.exe. This executable start a server on your system which in turn is
responsible for running your test scripts in Selenium.

Selenium WebDriver- Running test on Firefox Browser- Gecko


(Marionette) Driver

Before proceeding with this section, let us first understand the basics of Gecko Driver.

What is Gecko Driver?


The term Gecko refers to Gecko browser engine which was developed by Mozilla
Foundation as a part of Mozilla browser.

Gecko Driver serves as a link between your tests in Selenium and the Firefox browser. It
acts as a proxy between W3C WebDriver-compatible clients (Eclipse, Netbeans, etc.) to
interact with Gecko-based browser (Mozilla Firefox).

Marionette (the next generation of FirefoxDriver) is turned on by default from Selenium


3. Selenium uses W3C Webdriver protocol to send requests to GeckoDriver, which
translates them into a protocol named Marionette. Even if you are working with older
versions of Firefox browser, Selenium 3 expects you to set path to the driver executable
by the webdriver.gecko.driver.

Note: Selenium 3 has upgraded itself to now launch Firefox driver using Marionette
driver instead of the default initialisation supported earlier.

Before writing the test script, let us first understand how we can initialize GeckoDriver in
Selenium. There are three ways to initialize GeckoDriver:

1. Using Desired Capabilities


First, we have to set the system property for Gecko Driver.

System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe"
);

Below is the code to set gecko driver using DesiredCapabilities class.

DesiredCapabilities capabilities = DesiredCapabilities.firefox();

capabilities.setCapability("marionette",true);

Here is the complete code:

2. Using marionette property:


Gecko Driver can also be initialized using marionette property.

The code for Desired Capabilities is not required for this method.
3. Using Firefox Options:
Firefox 47 or later versions have marionette driver as a legacy system. Thus, marionette
driver can be called using Firefox Options as shown below.

Selenium WebDriver- Running test on IE Browser

Internet Explorer implements the WebDriver protocol using Internet Explorer Driver
Server. The Internet explorer Driver Server is the link between your tests in Selenium
and the Internet Explorer Browser.

Selenium WebDriver- Running test on Safari Browser

Safari Browser implements the WebDriver protocol using SafariDriver. The SafariDriver is
the link between your tests in Selenium and the Safari Browser. SafariDriver has been
implemented as a plugin in safari browser and this provides a perfect match of client and
server machine where SafariDriverServer acts as server and Selenium-Java/Language
binding acts as client.

Note: Previously SafariDriver was supporting safari browser on Windows machine but
recently Apple has decided to remove its support for windows and then execution on
safari has become the job of Mac machine. So for the same, we need mac machine
where safari browser should be installed.

Before writing the test script, let us first understand how we can initialize SafariDriver in
Selenium. Safari browser is represented by a class called SafariDriver in the
org.openqa.selenium.safari package. All we have to do is to create an instance of
SafariDriver class.
Selenium WebDriver - Find Element and FindElements

Why do you need Find Element/s command?


Interaction with a web page requires a user to locate the web element. Find Element
command is used to uniquely identify a (one) web element within the web page.
Whereas, Find Elements command is used to uniquely identify the list of web elements
within the web page. There are multiple ways to uniquely identify a web element within
the web page such as ID, Name, Class Name, Link Text, Partial Link Text, Tag Name and
XPATH.

FindElement command syntax:


Find Element command takes in the By object as the parameter and returns an object of
type WebElement. By object in turn can be used with various locator strategies such as
ID, Name, Class Name, XPATH etc. Below is the syntax of FindElement command in
Selenium web driver.

WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));

Locator Value is the unique value using which a web element can be identified. It is the
responsibility of developers and testers to make sure that web elements are uniquely
identifiable using certain properties such as ID or name.

Example:

WebElement loginLink = driver.findElement(By.linkText("Login"));

FindElements command syntax:


Find Elements command takes in By object as the parameter and returns a list of web
elements. It returns an empty list if there are no elements found using the given locator
strategy and locator value. Below is the syntax of find elements command.
List<WebElement> elementName =
driver.findElements(By.LocatorStrategy("LocatorValue"));

Example:

List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));

Find element Vs Find elements


Below are the major differences between find element and find elements commands.

Find Element Find Elements

Returns the first most web element if there


are multiple web elements found with the Returns a list of web elements
same locator

Throws exception NoSuchElementException


Returns an empty list if there are no web
if there are no elements matching the
elements matching the locator strategy
locator strategy

It will find a collection of elements whose


It will only find one web element
match the locator strategy.

Each Web element is indexed with a number


Not Applicable
starting from 0 just like an array

Summary:

 Find Element command returns the web element that matches the first most
element within the web page.

 Find Elements command returns a list of web elements that match the criteria.

 Find Element command throws NoSuchElementException if it does not find the


element matching the criteria.

 Find Elements command returns an empty list if there are no elements matching
the criteria

Selenium WebDriver- Locating Strategies

Locator is a command that tells Selenium IDE which GUI elements ( say Text Box,
Buttons, Check Boxes etc) its needs to operate on.  Identification of correct GUI
elements is a prerequisite to creating an automation script. But accurate identification of
GUI elements is more difficult than it sounds. Sometimes, you end up working with
incorrect GUI elements or no elements at all!  Hence, Selenium provides a number of
Locators to precisely locate a GUI element

A list of Locating Strategies used in WebDriver:

Locating Strategies- (By ID)

The Java Syntax for locating a web element using its id attribute is written as:

driver.findElement(By.id (<element ID>))  

driver.findElement(By.id (<"fname">))  

Locating Strategies- (By Name)

The Java Syntax for locating a web element using its name attribute is written as:

driver.findElement(By.name(<element ID>))  
driver.findElement(By.name (<"firstName">))  

Locating Strategies- (By Class Name)

The Java Syntax for locating a web element using its Class attribute is written as:

driver.findElement(By.className (<element class>))  

driver.findElement(By.className (<"Automation">))

Locating Strategies- (By Tag Name)

The Java Syntax for locating a web element using its Tag Name is written as:

driver.findElement(By.tagName (<htmltagname>))  

Therefore, for locating the textbox on the sample web page we will use the name of its
first Tag Element:

driver.findElement(By.tagName (<"input">))  

Locating Strategies- (By CSS)


CSS stands for Cascading Style Sheets. It is a Style Sheet Language which is used to
describe the look and formatting of a document written in markup language.

locating web elements through CSS involves use of CSS Selector which identifies an
element based on the combination of HTML tag, id, class and attributes.

In WebDriver, CSS Selector works in six modes to identify and locate web elements.
1. Locating Strategies- (By CSS- Tag and ID)

The Java Syntax for locating a web element through CSS - Tag and ID Selector is
written as:

driver.findElement(By.cssSelector("Tag#Value of id attribute"))  

Therefore, for locating the Textbox on the sample web page, we will use the input tag
along with the value of its id attribute:

driver.findElement(By.cssSelector("input#fname"))  

2. Locating Strategies- (By CSS-Tag and Class)

The Java Syntax for locating a web element through CSS - Tag and Class Selector is
written as:

driver.findElement(By.cssSelector("Tag.Value of Class attribute"))  
Therefore, for locating the "Automation Testing" Checkbox on the sample web page
we will use the input tag along with the value of its Class attribute:

driver.findElement(By.cssSelector("input.Automation"))  

3. Locating Strategies- (By CSS-Tag and Attribute)

The Java Syntax for locating a web element through CSS - Tag and Attribute Selector
is written as:

driver.findElement(By.cssSelector("Tag[Attribute=value]"))  

Therefore, for locating the Textbox on the sample web page we will use the input tag
with id attribute:

driver.findElement(By.cssSelector("input[id=fname]"))  
Note: You can choose attributes such as id, class and name along with their
values when you are locating through CSS - Tag and Attribute Selector

4. Locating Strategies- (By CSS-Tag, Class and Attribute)

The Java Syntax for locating a web element through CSS - Tag, Class and Attribute
Selector is written as:

driver.findElement(By.cssSelector("tag.class[attribute=value]"))  

Therefore, for locating the Textbox on the Google Search Engine page we will use the
input tag, Class and Name attribute:

driver.findElement(By.cssSelector("input.gsfi[name=q]"))  
5. Locating Strategies- (By CSS- Sub-String Matches)

WebDriver provides an interesting feature of allowing partial string matches using ^,


$ and*.

1. Starts with (^):

To select and locate a web element, we would use ^ which means 'starts with'.

The Java Syntax for locating a web element through CSS - Sub-String Matches
Technique using ^ operator is written as:

driver.findElement(By.cssSelector("Tag[attribute^=prefix of the string]"))  

Therefore, for locating the Text box on the sample web page we will use the input tag
along with the id attribute:

driver.findElement(By.cssSelector("input[id^='fna']"))  

2. Ends with ($):

To select and locate a web element, we would use $ which means 'ends with'.

The Java Syntax for locating a web element through CSS - Sub-String Matches
Technique using $ operator is written as:

driver.findElement(By.cssSelector("Tag[attribute$=suffix of the string]"))  

Therefore, for locating the Text box on the sample web page we will use the input tag
along with the id attribute:

driver.findElement(By.cssSelector("input[id$='me']"))  

3. Contains (*):<
To select and locate a web element, we would use * which means 'sub-string'.

The Java Syntax for locating a web element through CSS - Sub-String Matches
Technique using * operator is written as:

driver.findElement(By.cssSelector("Tag[attribute*=sub-string]"))  
Therefore, for locating the Text box on the sample web page we will use the input tag along with the
id attribute:

driver.findElement(By.cssSelector("input[id*='id']"))

We can also use 'contains()' in place of * operator

driver.findElement(By.cssSelector("input:contains('id')"))

Locating Strategies- (By Partial Link Text)

The Java Syntax for locating a web element through its Partial Link Text is written as:

driver.findElement(By.partialLinkText (<linktext>)) 

Therefore, for locating the Link Text on the sample web page we will use the value of its
Partial Link Text:

driver.findElement(By.partialLinkText (<"This is">))  

Accessing links using their exact link text is done through the By.linkText()
method. However, if there are two links that have the very same link text, this method
will only access the first one. Consider the HTML code below
When you try to run the WebDriver code below, you will be accessing the first "click
here" link

As a result, you will automatically be taken to Google.

The parameters for By.linkText() and By.partialLinkText() are both case-sensitive,


meaning that capitalization matters

where there are multiple links with the same link text, and we want to access the links
other than the first one, how do we go about it?

In such cases, generally, different locators viz... By.xpath(), By.cssSelector() or


By.tagName() are used.

Most commonly used is By.xpath(). It is the most reliable one but it looks complex and
non-readable too
Locating Strategies- (By XPath)
o XPath is a language used for locating nodes in XML documents.
o XPath can be used as a substitute when you don't have a suitable id or name
attribute for the element you want to locate.
o XPath allows you to select individual elements, attributes, and some other part
of XML documents for specifying location of a particular web element.

In WebDriver, the Java syntax for locating elements through XPath can be written as:
driver.findElement(By.xpath("XPath"));

However, there are different ways of writing dynamic XPaths such as:

1. Using Single Slash

2. Using Double Slash

3. Using Single Attribute

4. Using Multiple Attribute

5. Using AND

6. Using OR

7. Using contains()

8. Using starts_with()

9. Using text()

10. Using last()

1) Locating Strategies- (By XPath- Using Single Slash)


Single Slash mechanism is also known as finding elements using Absolute XPath. Single
Slash is used to create XPath with absolute XPath i.e. the XPath would be created to
start selection from the document node/ Start node/ Parent node.

Note: XPath Absolute is not recommended because it takes the complete XPath location
and if the location of the web element is changed or it belongs to some other parent
then XPath will be unable to locate the desired element.

The syntax for locating elements through XPath Absolute is written as:

//html/body/tag1[index]/tag2[index]/.../tagN[index]

Using Single Slash/Absolute XPath, we can write the Java code along with the dynamic
XPath location as:
findElement(By.xpath("html/body/div[1]/div[2]/div[2]/div[1]/form/div[1]/div/div
[1]/div/div/input[1]"));

2) Locating Strategies- (By XPath- Using Double Slash)


Double Slash mechanism is also known as finding elements using Relative XPath. Double
slash is used to create XPath with relative path i.e. the XPath would be created to start
selection from anywhere within the document. - Search in a whole page (DOM) for the
preceding string.

Note: Double Slash is recommended when you don't have a suitable id or name
attribute for the element you want to locate.

Using Double Slash/Relative XPath, we can write the Java code along with the dynamic
XPath location as:

findElement(By.xpath("//form/div[1]/div/div[1]/div/div/input[1]"));

3) Locating Strategies- (By XPath- Using Single Attribute)


Browsers like Firefox by default provides the XPath location with single attribute

The syntax for locating elements through XPath- Single Attribute can be written as:

//<HTML tag>[@attribute_name='attribute_value']

or

//*[@attribute_name='attribute_value']

Note: '*'after double slash is to match any tag with the desired text.

Copy the desired dynamic XPath location and paste it in between the Java code for
locating the web elements through XPath:

findElement(By.xpath("//*[@id="lst-ib"]"));  
4) Locating Strategies- (By XPath- Using Multiple Attribute)

The syntax for locating elements through XPath- Multiple Attribute can be written as:

//<HTML tag>[@attribute_name1='attribute_value1']
[@attribute_name2='attribute_value2]

or

//*[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]

Using XPath-Multiple, we can write the Java code along with the dynamic XPath location
as:

findElement(By.xpath("//*[@id='lst-ib'][@class='gsfi lst-d-f']"));  

5) Locating Strategies- (By XPath- Using And)

The syntax for locating elements through XPath- Using And can be written as:

//<HTML tag>[@attribute_name1='attribute_value1' and @attribute_name2='attribute_value2]

or

//*[@attribute_name1='attribute_value1' and @attribute_name2='attribute_value2]

Using XPath-And, we can write the Java code along with the dynamic XPath location as:

findElement(By.xpath("//*[@id='lst-ib' and @class='gsfi lst-d-f']"));

6) Locating Strategies- (By XPath- Using Or)

The syntax for locating elements through XPath- Using Or can be written as:

//<HTML tag>[@attribute_name1='attribute_value1' or
@attribute_name2='attribute_value2]
or

//*[@attribute_name1='attribute_value1' or @attribute_name2='attribute_value2']

Using XPath-Or, we can write the Java code along with the dynamic XPath location as:

findElement(By.xpath("//*[@id='lst-ib' or @class='gsfi lst-d-f']"));

7) Locating Strategies- (By XPath- Using contains())

"contains()" is used to identify an element, when we are familiar with some part of the
attributes value of an element.

The syntax for locating elements through XPath- Using contains() method can be written
as:

//<HTML tag>[contains(@attribute_name,'attribute_value')]

or

//*[contains(@attribute_name,'attribute_value')]

Using XPath- conatins() method, we can write the Java code along with the dynamic
XPath location as:

findElement(By.xpath("//*[contains(@id,'lst-ib')]"));  

8) Locating Strategies- (By XPath- starts-with())


"starts-with()" is used to identify an element, when we are familiar with the attributes
value (starting with the specified text) of an element.

The syntax for locating elements through XPath- Using starts-with() method can be written as:

//<HTML tag>[starts-with(@attribute_name,'attribute_value')]

or
//*[starts-with(@attribute_name,'attribute_value')]

Using XPath- starts-with method, we can write the Java code along with the dynamic
XPath location as:

findElement(By.xpath("//*[starts-with(@id,'lst')]"));  

9) Locating Strategies- (By XPath- Using text())

"text() method" is used to identify an element based on the text available on the web
page

The syntax for locating elements through XPath- Using text() method can be written as:

//*[text()='Google offered in']

Using XPath- text() method, we can write the Java code along with the dynamic XPath
location as:

findElement(By.xpath("//*[text()='Google offered in')]"));


10)Locating Strategies- (By XPath- Using last())

"last() method" selects the last element (of mentioned type) out of all input element
present

Using XPath- last() method, we can write the Java code along with the dynamic XPath
location as:

findElement(By.xpath("(//input[@type='text'])[last()]"))  

How to Click on Image in Selenium Webdriver


Accessing Image Links

Image links are the links in web pages represented by an image which when clicked
navigates to a different window or page.

Since they are images, we cannot use the By.linkText() and By.partialLinkText()
methods because image links basically have no link texts at all.

 By.cssSelector or By.xpath

In this case, we should resort to using either By.cssSelector or By.xpath. The first
method is more preferred because of its simplicity.

In the example below, we will access the "Facebook" logo on the upper left portion of
Facebook's Password Recovery page.

We will use By.cssSelector and the element's "title" attribute to access the image link.
And then we will verify if we are taken to Facebook's homepage.
Result

Conclusion:

This is all to clicking images. Accessing image link is done using By.cssSelector()

Selenium WebDriver- Handling drop-downs

Before proceeding with this section, let us first understand some of the basics of
handling drop-downs in Selenium WebDriver.

Select in Selenium WebDriver


The 'Select' class in Selenium WebDriver is used for selecting and deselecting option in a
dropdown. The objects of Select type can be initialized by passing the dropdown
webElement as parameter to its constructor.

WebElement testDropDown = driver.findElement(By.id("testingDropdown"));

Select dropdown = new Select(testDropDown);

How to select an option from drop-down menu?


WebDriver provides three ways to select an option from the drop-down menu.

i. selectByIndex
It is used to select an option based on its index, beginning with 0.

dropdown.selectByIndex(5);

ii. selectByValue
It is used to select an option based on its 'value' attribute.

dropdown.selectByValue("Database");

iii. selectByVisibleText
It is used to select an option based on the text over the option.

dropdown.selectByVisibleText("Database Testing");

select the option "Database Testing" from the drop-down menu

Select dropdown = new Select(driver.findElement(By.id("testingDropdown")));  
dropdown.selectByVisibleText("Database Testing");  

Handling Radio buttons

There are two ways of handling the radio buttons:


i. By using the customized path:

In the above, we use custom Xpath. Radio buttons contain a unique attribute, i.e., value,
so we use the value attribute to handle the radio button.

driver.findElement(By.xpath("//input[@value='Banana']")).click();

ii. By handling the radio buttons dynamically.


We will first calculate the number of radio buttons. The following is the line of code which
calculates the number of radio buttons.

int a = driver.findElements(By.xpath("//input [@name='group1']")).size();

The above line of code calculates the number of radio buttons whose name is group1.

Now, we will handle the radio buttons by using the index of a particular radio button.

driver.findElements(By.xpath("//input[@name='group1']")).get(2).click();

int a = driver.findElements(By.xpath("//input [@name='group1']")).size();  
        System.out.println(a);  
        for(int i=1;i<=a;i++)  
        {  
            driver.findElements(By.xpath("//input[@name='group1']")).get(2).click();  
 } 

In the above code, we use 'for' loop. Inside the 'for' loop, we find the third radio button
of group1 by using the get(2) method.

Handling Checkbox
Note the id attribute of a checkbox.

In the above case, we observe that 'id' is a unique attribute, so we locate the
checkbox by using an id attribute.

driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click();  
System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscou
nt']")).isSelected());  

In the above code, we haven't used the complete 'id' attribute value as it is very big.
I have used the half part of the 'Senior Citizen' checkbox, and other half part is
represented in the form of regular expression, i.e., '*='.

We have used two methods in the above code:

o isSelected(): This method determines whether the checkbox is selected or


not. If the checkbox is selected, then this method returns true otherwise false.
o click(): This method selects the locator. In this case, it is selecting the "Senior
Citizen" checkbox.

Assertions

Assertion determines the state of the application whether it is the same what we are
expecting or not. If the assertion fails, then the test case is failed and stops the
execution.

To use the Assertion in Web Driver, you need to download the Testng jar file and add it
to the eclipse. Download the Testng jar file from the link given below:

https://mvnrepository.com/artifact/org.testng/testng/6.7

There are two types of Assertion:

o Hard Assertion
o Soft Assertion

Hard Assertion
Hard Assertion is an Assertion that throws the AssertException when the test case is
failed. In the case of Hard Assertion, you can handle the error by using a catch block like
a java exception. Suppose we have two test cases in a suite. The first test case in a suite
has an assertion that fails, and if we want to run the second case in a suit, then we need
to handle the assertion error. A Hard Assertion contains the following methods:

o AssertEquals
o AssertNotEquals
o AssertTrue
o AssertFalse
o AssertNull
o AssertNotNull
AssertFalse()

Assertion verifies the boolean value returned by a condition. If the boolean value is false,
then assertion passes the test case, and if the boolean value is true, then assertion
aborts the test case by an exception. Syntax of AssertFalse() method is given below:

Assert.AssertFalse(condition);  
Let's understand through an example:

 When the condition is false

In the above code, Assert.assertFalse() contains the condition which is returning false
value. Therefore, it passes the test case.

Output on the console

 When the condition is true


In the above code, Assert.assertFalse() method contains the true condition.
Therefore, the assertion fails which means that the test case is also failed. Assertion
failure will stop the execution by exception.

Output on the console

AssertTrue()
Assertion verifies the boolean value returned by a condition. If the boolean value is true,
then assertion passes the test case, and if the boolean value is false, then assertion
aborts the test case by an exception. Syntax of AssertTrue() method is given below:

Assert.AssertTrue(condition); 
In the above code,
driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click(
); This statement is used to select the 'Senior Citizen' box. In the next statement, we
are applying assertion to check whether the test case fails or pass. The parameter inside
the Assert.assertTrue() method is returning true value, therefore the test case pass.

Output on the Console

AssertEquals()
AssertEquals() is a method used to compare the actual and expected results. If both the
actual and expected results are same, then the assertion pass with no exception and the
test case is marked as "passed". If both the actual and expected results are not the
same, then the assertion fails with an exception and the test case is marked as "failed".
Syntax of an AssertEquals() method is given below:
Assert.assertEquals(actual,expected);  
 When a number of adults is 5.

Output on the Console

 When the number of adults is not equal to 5


Output on the console

AssertNotEquals()
It is opposite to the function of AssertEquals() method. AssertNotEquals() is a method
used to compare the actual and expected results. If both the actual and expected results
are not the same, then the assertion pass with no exception and the test case is marked
as "passed". If both the actual and expected results are same, then the assertion fails
with an exception and the test case is marked as "failed". Syntax of an
AssertNotEquals() method is given below:
AssertNotEquals(actual,expected,message); 

Let's understand through an example.

 When actual string is not equal to the expected string.


In the above code, actual string, i.e., Hello is not equal to the expected string, i.e., How
are you. Therefore, the assertion passes the test case. This will execute the next
statement and the next statement is System.out.println("Hello...This is
javaTpoint");.

Output

 When the actual string is equal to the expected string

Output
AssertNull()
AssertNull() is a method that verifies whether the object is null or not. If an object is
null, then assertion passes the test case, and the test case is marked as "passed", and if
an object is not null, then assertion aborts the test case and the test case is marked as
"failed". Syntax of AssertNull() method is given below:

Assert.assertNull(object);

Let's understand through an example.

 When an object is null.

Output

 When an object is not equal to null.


Output

AssertNotNull()
AssertNotNull() is a method that verifies whether the object is null or not. If an object is
not null, then assertion passes the test case and test case is marked as "passed", and if
an object is null, then assertion aborts the test case and test case is marked as "failed".
Syntax of AssertNotNull() method is given below:

Assert.assertNotNull(object);  

Let's understand through an example.

 When an object is not null.


Output

 When an object is null.

Output
SoftAssertion
Till now, we have learnt about the Hard Assertion in Web Driver using Testng framework.
In hard assertion, if an assertion fails then it aborts the test case otherwise it continues
the execution. Sometimes we want to execute the whole script even if the assertion fails.
This is not possible in Hard Assertion. To overcome this problem, we need to use a soft
assertion in testng.

Selenium Webdriver - Mouse Click & Keyboard Event: Action Class

Handling Keyboard & Mouse Events


Handling special keyboard and mouse events are done using the Advanced User
Interactions API. It contains the Actions and the Action classes that are needed
when executing these events. The following are the most commonly used keyboard and
mouse events provided by the Actions class.

Method Description

clickAndHold() Clicks (without releasing) at the current mouse location.

Performs a context-click at the current mouse location. (Right


contextClick()
Click Mouse Action)

doubleClick() Performs a double-click at the current mouse location.

Performs click-and-hold at the location of the source element,


moves to the location of the target element, then releases the
mouse.

dragAndDrop(source, target) Parameters:

source- element to emulate button down at.

target- element to move to and release the mouse at.

dragAndDropBy(source, x-offset, Performs click-and-hold at the location of the source element,


y-offset) moves by a given offset, then releases the mouse.

Parameters:

source- element to emulate button down at.

xOffset- horizontal move offset.


yOffset- vertical move offset.

Performs a modifier key press. Does not release the modifier key
- subsequent interactions may assume it's kept pressed.

keyDown(modifier_key) Parameters:

modifier_key - any of the modifier keys (Keys.ALT,


Keys.SHIFT, or Keys.CONTROL)

Performs a key release.

Parameters:
keyUp(modifier _key)
modifier_key - any of the modifier keys (Keys.ALT,
Keys.SHIFT, or Keys.CONTROL)

Moves the mouse from its current position (or 0,0) by the given
offset.

Parameters:
moveByOffset(x-offset, y-offset)
x-offset- horizontal offset. A negative value means moving the
mouse left.

y-offset- vertical offset. A negative value means moving the


mouse down.

Moves the mouse to the middle of the element.

moveToElement(toElement) Parameters:

toElement- element to move to.

Releases the depressed left mouse button at the current mouse


release()
location

Sends a series of keystrokes onto the element.

Parameters:
sendKeys(onElement,
onElement - element that will receive the keystrokes, usually a
charsequence)
text field

charsequence - any string value representing the sequence of


keystrokes to be sent
In the following example, we shall use the moveToElement() method to mouse-over on
one Mercury Tours' table rows. See the example below.

Building a Series of Multiple Actions


You can build a series of actions using the Action and Actions classes. Just
remember to close the series with the build() method. Consider the sample code below.

Output:

The code snippet to perform drag and drop operation.


Selenium Webdriver - Upload & Download a File

Uploading Files
For this section, we will use http://demo.guru99.com/test/upload/ as our test
application. This site easily allows any visitor to upload files without requiring them to
sign up.

Uploading files in WebDriver is done by simply using the sendKeys() method on


the file-select input field to enter the path to the file to be uploaded.

Handle File upload popup in Selenium Webdriverhandle file upload popup in selenium
webdriver

Let's say we wish to upload the file "C:\newhtml.html". Our WebDriver code should be
like the one shown below.

package newproject;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PG9 {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
String baseUrl = "http://demo.guru99.com/test/upload/";
WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);
WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));

// enter the file path onto the file-selection input field


uploadElement.sendKeys("C:\\newhtml.html");

// check the "I accept the terms of service" check box


driver.findElement(By.id("terms")).click();

// click the "UploadFile" button


driver.findElement(By.name("send")).click();
}
}

After running this script, you should be able to upload the file successfully and you should
get a message similar to this.

Remember following two things when uploading files in WebDriver

1. There is no need to simulate the clicking of the "Browse" button. WebDriver automatically
enters the file path onto the file-selection text box of the <input type="file"> element
2. When setting the file path in your Java IDE, use the proper escape character for the back-
slash.
Downloading Files
WebDriver has no capability to access the Download dialog boxes presented by browsers
when you click on a download link or button. However, we can bypass these dialog boxes
using a separate program called "wget".

What is Wget?
Wget is a small and easy-to-use command-line program used to automate downloads.
Basically, we will access Wget from our WebDriver script to perform the download process.

Setting up Wget
Step 1: In your C Drive, create a new folder and name it as "Wget".

Download wget.exe from here and Place it in the Wget folder you created from the step
above.

Step 2: Open Run by pressing windows key + "R" ; type in "cmd & click ok

Type in the command "cd /" to move to the root directory


Step 3: Type in the command to check whether the given setup is working
cmd /c C:\\Wget\\wget.exe -P C: --no-check-certificate
http://demo.guru99.com/selenium/msgr11us.exe

There seems to be an issue writing into C drive.

Step 4: You need to debug the wget errors in command line before you execute the code
using Selenium Webdriver. These errors will persist in Eclipse and the error messages will
not be as informative. Best to first get wget working using command line. If it works in
command line it will definitely work in Eclipse.

In our example, as show in step 3, there is a problem writing into C drive. Let's change the
download location to D drive and check results.

cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate


http://demo.guru99.com/selenium/msgr11us.exe

Messenger was downloaded successfully.

Before you proceed further don't forget to delete the downloaded file
Using WebDriver and Wget
In the following example, we will use WebDriver and wget to download a popular chat
software called Yahoo Messenger. Our base URL shall be
http://demo.guru99.com/test/yahoo.html.

Step 1

Import the "java.io.IOException" package because we will have to catch an IOException later
in Step 4.

Step 2

Use getAttribute() to obtain the "href" value of the download link and save it as a String
variable. In this case, we named the variable as "sourceLocation".

Step 3

Set-up the syntax for wget using the following command.


Step 4
Initiate the download process by calling wget from our WebDriver code.

To sum it all up, your WebDriver code could look like the one shown below.
package newproject;
import java.io.IOException;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PG8 {
public static void main(String[] args) {

System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
String baseUrl = "http://demo.guru99.com/test/yahoo.html";
WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);
WebElement downloadButton = driver.findElement(By
.id("messenger-download"));
String sourceLocation = downloadButton.getAttribute("href");
String wget_command = "cmd /c C:\\Wget\\wget.exe -P D: --no-check-
certificate " + sourceLocation;

try {
Process exec = Runtime.getRuntime().exec(wget_command);
int exitVal = exec.waitFor();
System.out.println("Exit value: " + exitVal);
} catch (InterruptedException | IOException ex) {
System.out.println(ex.toString());
}
driver.close();
}

}
After executing this code, check your D drive and verify that the Yahoo Messenger installer
was successfully downloaded there.

Selenium WebDriver- Handling Alerts

What is Alert?
Alert is a small message box which displays on-screen notification to give the user some
kind of information or ask for permission to perform certain kind of operation. It may be
also used for warning purpose.

Here are few alert types:

a) Simple Alert
This simple alert displays some information or warning on the screen.
b) Prompt Alert.
This Prompt Alert asks some input from the user and selenium webdriver can enter the
text using sendkeys(" input…. ").

c) Confirmation Alert.
This confirmation alert asks permission to do some type of operation.

Selenium WebDriver provides three methods to accept and reject the Alert depending on
the Alert types.
void dismiss()
This method is used to click on the 'Cancel' button of the alert.

Syntax:

driver.switchTo().alert().dismiss(); 

void accept()
This method is used to click on the 'Ok' button of the alert.

Syntax:

driver.switchTo().alert().accept();

String getText()
This method is used to capture the alert message.

Syntax:

driver.switchTo().alert().getText();

void sendKeys(String stringToSend)


This method is used to send some data to the alert box.

Syntax:

driver.switchTo().alert().sendKeys("Text");

Let us consider a test case in which we will automate the following scenarios:

o Invoke Firefox Browser


o Open URL: https://www.testandquiz.com/selenium/testing.html
o Click on the "Generate Alert box" button
o Click on the "Generate Confirm box" button
o Close the browser
To automate our third and fourth test scenario, we need to write the code which click
and accept the Generate Alert box as well as click and accept the Generate Confirm
box.

Selenium WebDriver- Scrolling a web page

Let us consider a test case in which we will automate the following scenarios:

o Invoke Firefox browser


o Open URL: www.javatpoint.com
o Scroll down through the web page in order to show the Java Technology
section

To automate our third test scenario, we need to write the code which will scroll down the
webpage in order to show the Java Technology section present on the JavaTpoint
website.
To scroll a web page, we have to use the scrollBy method of JavaScript. For executing
the JavaScript method we will use JavaScript executor. The scrollBy method takes two
parameters one each for horizontal and vertical scroll in terms of pixels.

JavascriptExecutor js = (JavascriptExecutor)driver;  
js.executeScript("scrollBy(0, 4500)");  

What is Selenium Grid?

Selenium Grid is a feature in Selenium that allows you to run test cases in different
machines across different platforms. The control of triggering the test cases is on the
local machine, and when the test cases are triggered, they are automatically executed
by the remote machine. Suppose you have 5 test cases. Your local machine is running
multiple applications, so you want to run your test cases in a remote machine. You need
to configure the remote server so that the test cases can be executed there.

It supports distributed test execution. Initially, you have a local machine where you write
the test cases and executes on the same machine. However, in a big organization, you
have multiple test cases, and it's not possible to run all the test cases in the same
machine. In a large organization, you have multiple servers, so local machine distributes
the test cases across different machines/servers.

You can also run the test cases in parallel in multiple machines on Selenium Grid.

Architecture of Selenium Grid

Hub
o A Hub is a central point or a local machine that receives all the test requests
and distributes them to the right nodes. The machine which actually triggers
the test case known as Hub
o There can be only one hub in a selenium grid.
o The machine which is containing the hub triggers the test case, but you will
see the browser being automated on other machines.

Node
Nodes are the selenium instances which will execute the test cases that you loaded on
the hub. Nodes can be launched on multiple machines with different platforms and
browsers.

Note: When you are configuring nodes, you are running the test cases in different
platforms. There is no rule that if your hub is running the test in windows, then all
nodes must be executing the test cases in windows only. The nodes can have
different platforms such as Safari on MAC, Firefox on Ubuntu, Internet Explorer on
WinXP, Android.

Why Selenium Grid?


Selenium Grid is useful because of the following reasons:

 Run on different platforms:


You can run the test cases in different platforms, so it is a platform
independent. For example, you have a hub which has internet explorer 9. Hub
has internet explorer 9 as many older websites support IE 9. Now we want to
run the test cases in different platform such as Internet Explorer. As we know
that only one version of a browser can be installed on a computer. You need to
configure the node to which you want to send your test cases.

 Parallel execution:
If you set up the Selenium Grid, you can run the multiple cases at the same
time. This saves time in running the test suites.

How to set up Selenium Grid


o Download the Selenium Standalone Server to run Remote Selenium webdriver.
It is available in a single jar file.
o Store the jar file at any of the drive.
o Open the cmd.
o Register the hub through cmd. Enter the command java ?jar selenium-
server-standalone-3.8.1.jar ?role hub. This command will treat the
machine as a hub.

From the above screen, we observe that the address of hub is


http://192.168.1.12:4444/grid/register. The port number is 4444 where hub resides
and we need to register the hub with these nodes.

o Open the link, i.e., http://192.168.1.12:4444/grid/console where the server


resides. Hub is the server only.
The above screen shows that the hub has been created, but it does not create
anything as a hub is not registered with the nodes. Now we need to register the
nodes with the hub.

o Log in to another machine and register it as a node for a hub. I will remotely
connect my machine to another machine through Teamviewer. To register the
node with your hub, you can do it from the node machine only not from your
machine, so I connect my machine to another machine. In a node machine,
run the command "java ?jar selenium-server-standalone-3.141.59.jar
role webdriver ?hub >ipaddress>/grid/register ?port 5566".
o Now if we want to run the test cases in a Google chrome or Firefox browser,
then we need to download the chrome driver or geekodriver in a node
machine. In order to achieve this, we need to run the following command in a
node machine:
"java ?Dwebdriver.chrome.driver="D:\chromedriver.exe" -jar
selenium-server-standalone-3.141.59.jar role webdriver ?hub
>ipaddress>/grid/register ?port 5566"

Where, D stands for parameters from the command line. If you place D, then
command assumes that it is given a single parameter and need to take care while
running the test cases. It is mandatory to have exe file of a browser. Without exe file,
we cannot run the selenium test cases.

Note: Both the Selenium Standalone server and exe file must be in the same path.

Selenium WebDriver - Handle Web Table

Reading a HTML Web Table


There are times when we need to access elements (usually texts) that are within HTML
tables. However, it is very seldom for a web designer to provide an id or name attribute
to a certain cell in the table. Therefore, we cannot use the usual methods such as
"By.id()", "By.name()", or "By.cssSelector()". In this case, the most reliable option is to
access them using the "By.xpath()" method.

Accessing Tables:
We will use XPath to get the inner text of the cell containing the text "fourth cell."

Consider the HTML code below.

Code:

Accessing Nested Tables


Nested tables are tables located within another table.
The WebDriver code below should be able to retrieve the inner text of the cell which we
are accessing.

Using Attributes as Predicates


If the element is written deep within the HTML code such that the number to use for the
predicate is very difficult to determine, we can use that element's unique attribute
instead.
In the example below, the "New York to Chicago" cell is located deep into Mercury Tours
homepage's HTML code.

In this case, we can use the table's unique attribute (width="270") as the predicate.
Attributes are used as predicates by prefixing them with the @ symbol. In the example
above, the "New York to Chicago" cell is located in the first <td> of the fourth <tr>, and
so our XPath should be as shown below.

Remember that when we put the XPath code in Java, we should use the escape
character backward slash "\" for the double quotation marks on both sides of "270" so
that the string argument of By.xpath() will not be terminated prematurely.
We are now ready to access that cell using the code below.

Shortcut: Use Inspect Element for Accessing Tables


in Selenium
If the number or attribute of an element is extremely difficult or impossible to obtain, the
quickest way to generate the XPath code is using Inspect Element.
Consider the example below from Mercury Tours homepage.

Step 1
Use Firebug to obtain the XPath code.
Step 2
Look for the first "table" parent element and delete everything to the left of it.

Step 3
Prefix the remaining portion of the code with double forward slash "//" and copy it over to
your WebDriver code.

The WebDriver code below will be able to successfully retrieve the inner text of the element
we are accessing.
Handling Dynamic Web Tables Using Selenium
WebDriver
There are two types of HTML tables published on the web-

1. Static tables: Data is static i.e. Number of rows and columns are fixed.
2. Dynamic tables: Data is dynamic i.e. Number of rows and columns are
NOT fixed.

Below is an example of a dynamic table of Sales. Based on input date filters,


number of rows will get altered. So, it is dynamic in nature.

Handling static table is easy, but dynamic table is a little bit difficult as rows and
columns are not constant.
Example: Fetch number of rows and columns from Dynamic WebTable
When the table is dynamic in nature, we cannot predict its number of rows and
columns.

Using Selenium web driver, we can find

 Number of Rows and columns of web table


 X row or Y column's data.

Below is program for fetching total number of rows and columns of web table.

Example: Fetch cell value of a particular row and column of the Dynamic Table
Let's assume we need 3 row of the table and its second cell's data. See the table
rd

below-
In above table, data is regularly updated after some span of time. The data you try
retrieve will be different from the above screenshot. However, the code remains the
same. Here is sample program to get the 3 row and 2 column's data.
rd nd
Example: Get Maximum of all the Values in a Column of Dynamic Table
In this example, we will get the maximum of all values in a particular column.

Refer the following table -

Here is the code

You might also like