Selenium Online
Selenium Online
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:
Accepts access requests from WebDriver client. Routes JSON test commands to remote drivers on
registered ‘nodes’.
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:
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.
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…
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.
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…
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:
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.
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.
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.
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?
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).
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.
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.
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.
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.
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.
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:
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.
Though clearly, QTP has more advanced capabilities, Selenium outweighs QTP in
three main areas:
Selenium Features
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 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 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 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.
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 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.
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:
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
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.
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");
As we have discussed earlier in the IDE section, Selenium commands are the set of
commands that are used to run our Selenium tests.
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.
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
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:
driver.get("www.javatpoint.com")
driver.navigate().to("https://javatpoint.com/selenium-tutorial");
2. Locating forms and sending user inputs
driver.findElement(By.id("lst-ib")).sendKeys("javatpoint tutorials");
The clear() method is used to clear the user inputs from the text box.
driver.findElement(By.name("q")).clear();
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();
The click() method is used to perform click operation on any web element.
driver.findElement(By.id("btnK")).click();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
9. Closing Browser
driver.close();
10. Closing Browser and other all other windows associated with the driver
driver.quit();
driver.switchTo().window("windowName");
driver.switchTo().frame("frameName");
13. Drag and Drop
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
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.
Method:
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
driver.get(URL);
Example: For instance, the command to load the official website of javaTpoint can be
written as:
driver.get("www.javatpoint.com")
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
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();
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
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();
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.
driver.quit();
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.
Given are some of the most commonly used Browser Navigation commands for Selenium
WebDriver.
Method:
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");
Method:
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();
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();
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.
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:
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.
Method:
clear() : void
Command:
element.clear();
Method:
Command:
element.sendKeys("text");
Method:
click() : void
Command:
element.click();
Method:
isDisplayed() : boolean
Command:
element.isDisplayed();
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();
Method:
submit() : void
Command:
element.submit();
Method:
getText() : String
Command:
element.getText();
Method:
getTagName() : String
Command:
element.getTagName();
33. getCssValue Command
Method:
getCssvalue() : String
Command:
element.getCssValue();
Method:
Command:
element.getAttribute();
Method:
getSize() : Dimension
Command:
element.getSize();
36. getLocation Command
Element is where on the page
Method:
getLocation() : Point
Command:
element.getLocation();
Before proceeding with this section, let us first understand the basics of Gecko Driver.
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).
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:
System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe"
);
capabilities.setCapability("marionette",true);
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.
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.
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
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:
Example:
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 Elements command returns an empty list if there are no elements matching
the criteria
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
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">))
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">))
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">))
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 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"))
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"))
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
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)
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']"))
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']"))
driver.findElement(By.cssSelector("input:contains('id')"))
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
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?
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:
5. Using AND
6. Using OR
7. Using contains()
8. Using starts_with()
9. Using text()
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]"));
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]"));
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']"));
The syntax for locating elements through XPath- Using And can be written as:
or
Using XPath-And, we can write the Java code along with the dynamic XPath location as:
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:
"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')]"));
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')]"));
"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:
Using XPath- text() method, we can write the Java code along with the dynamic XPath
location as:
"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()]"))
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()
Before proceeding with this section, let us first understand some of the basics of
handling drop-downs in Selenium WebDriver.
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 dropdown = new Select(driver.findElement(By.id("testingDropdown")));
dropdown.selectByVisibleText("Database Testing");
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();
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., '*='.
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
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:
In the above code, Assert.assertFalse() contains the condition which is returning false
value. Therefore, it passes the test case.
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.
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.
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);
Output
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);
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);
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.
Method Description
Parameters:
Performs a modifier key press. Does not release the modifier key
- subsequent interactions may assume it's kept pressed.
keyDown(modifier_key) Parameters:
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.
moveToElement(toElement) Parameters:
Parameters:
sendKeys(onElement,
onElement - element that will receive the keystrokes, usually a
charsequence)
text field
Output:
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.
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"));
After running this script, you should be able to upload the file successfully and you should
get a message similar to this.
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
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.
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
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.
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.
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();
Syntax:
driver.switchTo().alert().sendKeys("Text");
Let us consider a test case in which we will automate the following scenarios:
Let us consider a test case in which we will automate the following scenarios:
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)");
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.
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.
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.
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.
Accessing Tables:
We will use XPath to get the inner text of the cell containing the text "fourth cell."
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.
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.
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.
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.