Java Testing Selenium Comprehensive Automation
Java Testing Selenium Comprehensive Automation
with Selenium
A Comprehensive Syntax Guide for
Automation
—
Sujay Raghavendra
Java Testing with
Selenium
A Comprehensive Syntax
Guide for Automation
Sujay Raghavendra
Java Testing with Selenium: A Comprehensive Syntax Guide for Automation
Sujay Raghavendra
Dharwad, Karnataka, India
Acknowledgments���������������������������������������������������������������������������xxiii
Introduction�������������������������������������������������������������������������������������� xxv
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
Mouse Movements����������������������������������������������������������������������������������������������54
Move to Element��������������������������������������������������������������������������������������������54
Move by Offset�����������������������������������������������������������������������������������������������55
Offset from Element��������������������������������������������������������������������������������������56
Offset from Viewport�������������������������������������������������������������������������������������58
Offset from Current Pointer Location�������������������������������������������������������������59
Drag and Drop on Element����������������������������������������������������������������������������60
Drag and Drop by Offset��������������������������������������������������������������������������������65
Keyboard Actions������������������������������������������������������������������������������������������������67
Keys���������������������������������������������������������������������������������������������������������������67
Key Down������������������������������������������������������������������������������������������������������68
Key Up�����������������������������������������������������������������������������������������������������������68
Send Keys������������������������������������������������������������������������������������������������������69
Scroll�������������������������������������������������������������������������������������������������������������70
Scroll to Element�������������������������������������������������������������������������������������������70
Scroll by a Given Amount�������������������������������������������������������������������������������71
Scroll from an Element by a Given Amount���������������������������������������������������72
Scroll from an Element with an Offset����������������������������������������������������������74
Scroll from an Offset of Origin by Given Amount�������������������������������������������75
Summary������������������������������������������������������������������������������������������������������������76
viii
Table of Contents
Locators��������������������������������������������������������������������������������������������������������������84
ID Locator������������������������������������������������������������������������������������������������������84
Name Locator������������������������������������������������������������������������������������������������85
Link Text Locator�������������������������������������������������������������������������������������������85
Partial Link Text Locator��������������������������������������������������������������������������������86
Tag Name Locator������������������������������������������������������������������������������������������87
Class Name Locator���������������������������������������������������������������������������������������88
XPath Locators����������������������������������������������������������������������������������������������89
CSS Selectors������������������������������������������������������������������������������������������������96
Locating Multiple Web Elements�����������������������������������������������������������������������110
Table for Locators to Locate Multiple Elements������������������������������������������112
Common Challenges for Locating Web Elements����������������������������������������114
Best Practices to Overcome Challenges������������������������������������������������������115
Summary����������������������������������������������������������������������������������������������������������117
Chapter 5: Navigations���������������������������������������������������������������������119
Hyperlinks���������������������������������������������������������������������������������������������������������120
Hyperlink by ID��������������������������������������������������������������������������������������������������120
Hyperlink by Text�����������������������������������������������������������������������������������������������121
Hyperlink by Partial Link Text����������������������������������������������������������������������������121
Hyperlink by XPath��������������������������������������������������������������������������������������������121
nth Hyperlink�����������������������������������������������������������������������������������������������������122
Return All Hyperlinks����������������������������������������������������������������������������������������122
Testing Hyperlinks���������������������������������������������������������������������������������������������122
Check for a Valid Hyperlink�������������������������������������������������������������������������������123
Check for Broken Images����������������������������������������������������������������������������������125
Data Attributes Hyperlinks��������������������������������������������������������������������������������127
Summary����������������������������������������������������������������������������������������������������������127
ix
Table of Contents
Chapter 6: Buttons����������������������������������������������������������������������������129
Standard HTML Button��������������������������������������������������������������������������������������130
Asserting Button Type����������������������������������������������������������������������������������131
Submit Button����������������������������������������������������������������������������������������������131
Asserting Button Type����������������������������������������������������������������������������������132
Image Button�����������������������������������������������������������������������������������������������133
Asserting Button Type����������������������������������������������������������������������������������134
JavaScript Button����������������������������������������������������������������������������������������������134
Disabled Button�������������������������������������������������������������������������������������������������135
Asserting Button Type����������������������������������������������������������������������������������136
Toggle Button����������������������������������������������������������������������������������������������������137
Radio Buttons����������������������������������������������������������������������������������������������������138
Locating and Selecting with Radio Buttons�������������������������������������������������139
By ID������������������������������������������������������������������������������������������������������������140
Using Label��������������������������������������������������������������������������������������������������140
By Index Values�������������������������������������������������������������������������������������������140
Myth of Deselecting Radio Buttons�������������������������������������������������������������141
Validating Your Choices with Assertions������������������������������������������������������141
Verifying the Selection State�����������������������������������������������������������������������141
Checkboxes�������������������������������������������������������������������������������������������������������142
Locating and Selecting Checkboxes������������������������������������������������������������143
By ID������������������������������������������������������������������������������������������������������������143
Using Label��������������������������������������������������������������������������������������������������144
By Name������������������������������������������������������������������������������������������������������144
Selecting a Checkbox by Visible Text�����������������������������������������������������������144
Selecting a Checkbox by Value��������������������������������������������������������������������145
Select All Checkboxes at Once��������������������������������������������������������������������145
Selecting and Deselecting by Index������������������������������������������������������������146
x
Table of Contents
xi
Table of Contents
Chapter 8: Assertions�����������������������������������������������������������������������179
What Are Assertions?����������������������������������������������������������������������������������������180
The Need for Assertions������������������������������������������������������������������������������������180
Hard Assertions (Asserts)����������������������������������������������������������������������������181
Soft Assertions (Verify)��������������������������������������������������������������������������������183
Hard vs. Soft������������������������������������������������������������������������������������������������185
xii
Table of Contents
Chapter 9: Exceptions�����������������������������������������������������������������������205
What Is an Exception in Selenium?�������������������������������������������������������������������205
Types of Exceptions������������������������������������������������������������������������������������������206
Common Exceptions in Selenium���������������������������������������������������������������������206
Connection Exception����������������������������������������������������������������������������������206
Element Interaction Exceptions�������������������������������������������������������������������207
xiii
Table of Contents
State-based Exceptions�������������������������������������������������������������������������������208
Timeout and Delay Exceptions��������������������������������������������������������������������209
Navigation Issues����������������������������������������������������������������������������������������210
Selector and Search Issues�������������������������������������������������������������������������210
JavaScript Execution Exceptions�����������������������������������������������������������������211
Session Exceptions��������������������������������������������������������������������������������������212
Driver Configuration and Capability Exceptions������������������������������������������212
Input and Argument Exceptions�������������������������������������������������������������������212
Alert and Pop-up Exceptions�����������������������������������������������������������������������213
Screenshot Exception����������������������������������������������������������������������������������213
Movement and Action Exception�����������������������������������������������������������������213
Browser Capability and Support Exception�������������������������������������������������214
Attribute and Property Exception�����������������������������������������������������������������215
Cookie Handling Exception��������������������������������������������������������������������������215
Window Handling Exceptions����������������������������������������������������������������������216
Element State Exceptions����������������������������������������������������������������������������216
Server and Response Exceptions����������������������������������������������������������������218
Other Exceptions�����������������������������������������������������������������������������������������218
Handling Exceptions in Selenium����������������������������������������������������������������������219
Why Exception Handling Is Essential in Selenium WebDriver���������������������219
Handling Exceptions�����������������������������������������������������������������������������������������220
Element Not Found Using try-catch�������������������������������������������������������������220
Timeout Exception Using try-catch-finally��������������������������������������������������������221
Stale Element Exception Using try-catch-finally with throw����������������������������222
Handling Various Exceptions Using Multiple catch Blocks��������������������������������223
Handling Custom Exceptions����������������������������������������������������������������������������224
xiv
Table of Contents
xv
Table of Contents
Decoding DOM���������������������������������������������������������������������������������������������263
Java Files in POM����������������������������������������������������������������������������������������266
Complete Analysis and Description of Creating a POM�������������������������������266
Differences between Traditional and POM���������������������������������������������������268
POM Best Practices�������������������������������������������������������������������������������������269
Factory Page�����������������������������������������������������������������������������������������������������272
Setting Up Page Factory������������������������������������������������������������������������������272
Summarizing the Setup�������������������������������������������������������������������������������276
Steps to Implement Page Factory���������������������������������������������������������������277
When to Use Page Factory��������������������������������������������������������������������������277
Differences Between POM and Page Factory����������������������������������������������278
Best Practices for Implementing Page Factory�������������������������������������������279
Limitations of POM and Page Factory���������������������������������������������������������������280
Limitations of POM��������������������������������������������������������������������������������������280
Limitations of Page Factory�������������������������������������������������������������������������281
When and Which One to Use: POM vs. Page Factory����������������������������������������281
When to Use POM����������������������������������������������������������������������������������������282
When to Use Page Factory��������������������������������������������������������������������������282
Making the Decision�����������������������������������������������������������������������������������������283
Summary����������������������������������������������������������������������������������������������������������283
xvi
Table of Contents
Index�������������������������������������������������������������������������������������������������317
xvii
About the Author
Sujay Raghavendra is a distinguished
expert in machine learning and software
testing, with a strong background in Python
programming. With a deep passion for both
disciplines, Sujay Raghavendra has dedicated
his career to exploring the intersection
of these fields and delivering innovative
solutions.
Recognizing the critical role of testing in software development, he
expanded his expertise to include automated testing using Selenium
with Python. With his books Python Testing with Selenium: Learn to
Implement Different Testing Techniques Using the Selenium WebDriver
and Beginner’s Guide to Streamlit with Python: Build Web-Based Data and
Machine Learning Applications, Sujay shares his extensive knowledge and
practical insights into leveraging Selenium’s capabilities with Python for
efficient and reliable testing. The book provides a comprehensive guide for
professionals looking to master automated testing techniques, harnessing
the power of Python and Selenium to ensure the quality and functionality
of web applications.
Sujay’s expertise in machine learning and software testing allows him
to bring a unique perspective to automated testing. He explores ways
to integrate machine learning concepts into testing practices, enabling
readers to discover innovative approaches to test analysis, anomaly
detection, and intelligent test execution.
xix
About the Author
xx
About the Technical Reviewer
Dolkun Tursun Tarim is a senior quality
assurance manager, principal software
developer, and the founder of Selenium
Master LLC. Tarim has more than 12 years
of progressive experience managing quality
assurance test automation teams and projects
and developing robust and scalable UI, API,
and database automation frameworks with
Java/.NET/Python Selenium WebDriver,
RestAssured, and database libraries. He has
technical expertise in Cypress and Playwright
automation framework implementations in the CI/CD Pipeline. He
mastered technical expertise in software accessibility, contract, and
performance testing. He has worked on various automated functional and
performance testing projects utilizing commercial and open source test
automation applications and framework APIs. In his free time, he teaches
full-stack software development engineer in test (SDET) courses online
and assists with testing automation projects.
xxi
Acknowledgments
I extend my heartfelt gratitude to my dear mother, Indumati Raghavendra,
and elder brother, Sumedh, for their steadfast love, guidance, and
support. Their unwavering belief in me has been a source of strength
and inspiration throughout my journey. My elder brother’s wisdom,
mentorship, and encouragement have significantly shaped my path and
aspirations. Together with my mother’s endless love and nurturing care,
they have been my pillars of strength, guiding me through life’s ups and
downs. I am truly blessed to have such remarkable individuals in my life,
and I am forever grateful for their unwavering presence and support.
Thank you, Mom and elder brother, for everything you have done for me.
xxiii
Introduction
Welcome to Java Testing with Selenium, a comprehensive guide designed
to help you master automated testing of web applications using Java
and Selenium WebDriver. This book is a journey through the intricacies
of Selenium WebDriver, exploring its capabilities for web testing and
leveraging the power of Java to create robust and efficient automation
scripts.
xxv
Introduction
xxvi
Introduction
Java Testing with Selenium equips you with the knowledge and skills
necessary to excel in the automated testing of web applications using
Java and Selenium WebDriver. Each chapter provides a comprehensive
discussion of topics, practical examples, and hands-on exercises to
reinforce learning. Whether you’re a novice or an experienced tester, this
book is your ultimate guide to mastering Selenium WebDriver testing with
Java. Let’s embark on this journey together and unlock the full potential of
automated testing!
xxvii
CHAPTER 1
Introduction to
Selenium: Java
Automation
Introduction
This exploration of the dynamic landscape of web application testing
delves into the indispensable role of Selenium. This open source
automation tool has fundamentally transformed the approach to ensuring
software quality and reliability. The journey begins with acknowledging
the critical need for application testing, a practice integral to identifying
and addressing potential issues before they affect the end-user experience.
A comprehensive overview introduces Selenium, highlighting its
development, history, and the key reasons behind its widespread adoption
in the web testing domain.
As you navigate through the intricacies of Selenium, the focus extends
to its robust architecture—a foundation that enables the seamless
integration and execution of complex testing scenarios. This architectural
insight provides a backdrop for understanding how Selenium distinguishes
itself from other tools in the market, offering unparalleled flexibility and
compatibility across various platforms and browsers.
Central to the narrative is the affinity between Selenium and Java, the
preferred programming language that amplifies Selenium’s capabilities.
Java’s object-oriented nature, wide adoption, and platform independence
make it an ideal companion for Selenium, enhancing your ability to
develop sophisticated and scalable test scripts.
This introductory chapter sets the stage for a deeper dive into the
functionalities, benefits, and strategic advantages of using Selenium in
conjunction with Java. The aim is to illuminate the synergies between
these powerful tools, showcasing why they remain at the forefront of web
application testing practices.
2
Chapter 1 Introduction to Selenium: Java Automation
What Is Selenium?
Selenium is a powerful and widely used open source framework for
automating web browsers. It plays a crucial role in testing web applications
by simulating user interactions with web elements. The core purpose of
Selenium is to automate web application testing across various browsers
and platforms, ensuring that web applications function correctly and
efficiently.
3
Chapter 1 Introduction to Selenium: Java Automation
4
Chapter 1 Introduction to Selenium: Java Automation
5
Chapter 1 Introduction to Selenium: Java Automation
6
Chapter 1 Introduction to Selenium: Java Automation
7
Chapter 1 Introduction to Selenium: Java Automation
8
Chapter 1 Introduction to Selenium: Java Automation
9
Chapter 1 Introduction to Selenium: Java Automation
Selenium Architecture
Explaining the architecture of Selenium, especially with the inclusion
of a block diagram, requires a comprehensive understanding of how its
various components interact within the framework. Here’s a detailed
explanation, followed by a description of a block diagram that illustrates
the architecture.
Core Components
Selenium client libraries/language bindings:
These are APIs provided by Selenium for various
programming languages like Java, Python, C#,
and Ruby. They enable writing test scripts in these
languages.
10
Chapter 1 Introduction to Selenium: Java Automation
11
Chapter 1 Introduction to Selenium: Java Automation
12
Chapter 1 Introduction to Selenium: Java Automation
13
Chapter 1 Introduction to Selenium: Java Automation
API Testing Limited, Yes, with Yes, with Limited, Yes, with
requires additional additional requires built-in
integrations components components integrations support
CI/CD Extensive Moderate Extensive Extensive Extensive
Integration
Community Extensive Moderate Moderate Extensive Moderate
Support
Test Yes, with Built-in and Built-in and Yes, with Built-in
Management third-party with third- with third- third-party and with
Integration tools party tools party tools tools third-
party
tools
Cost Free High Moderate to Free (for open Free
High source) with paid
options
Table 1-2 shows that Selenium stands out primarily due to its open
source nature, extensive browser compatibility, and support for multiple
programming languages. Its adaptability to various platforms (Windows,
macOS, Linux) and extensive CI/CD integration capabilities make it highly
versatile. Although it requires programming skills, its wide community
support, and capability to integrate with third-party tools for enhanced
functionality make it a preferred choice, especially for testing automation
projects needing a robust testing framework.
14
Chapter 1 Introduction to Selenium: Java Automation
Cross-platform compatibility/platform
independence: Java’s platform independence
is a significant advantage. Test scripts written in
Java can be executed across different operating
15
Chapter 1 Introduction to Selenium: Java Automation
16
Chapter 1 Introduction to Selenium: Java Automation
Summary
This chapter focused on Selenium’s role in automated web application
testing, detailing its necessity for developing high-quality software. It
examined the origins and evolution of Selenium, highlighting how it has
become a crucial tool in the testing field.
The analysis included a look at Selenium’s architecture, showcasing
its ability to efficiently conduct complex tests across various platforms and
browsers. This exploration emphasized Selenium’s flexibility, open source
nature, and broad support for different programming languages, which
sets it apart from other testing tools.
A key part of the discussion was the synergy between Selenium and
Java. You learned that Java’s object-oriented features and widespread
use enhance Selenium’s test script development, making it the preferred
programming language for Selenium users. This combination optimizes
test processes and utilizes Java’s strengths to improve Selenium’s
functionality.
17
Chapter 1 Introduction to Selenium: Java Automation
18
CHAPTER 2
Selenium Essentials:
Setup and Browser
Commands
This chapter embarks on a technical expedition to set up and master the
integration of Java, Eclipse IDE, and Selenium WebDriver—essential tools
in the arsenal of modern automated testing. Your focus is to configure
a robust development environment that enables you to harness the full
potential of browser automation for testing web applications.
You begin by installing Java, setting the stage with a powerful
programming language that underpins your test scripts. Next, you delve
into the Eclipse IDE, optimizing your workspace for seamless development
and testing synergy. The final setup of your installation involves integrating
Selenium WebDriver, unlocking your ability to programmatically control
web browsers and emulate user interactions with unparalleled precision.
This chapter explores the strategic manipulation of browser windows,
tailoring their size and position to mimic various user environments. This
capability is crucial for assessing responsive web designs and ensuring
compatibility across different devices. You tackle the challenges of loading
web pages, emphasizing the significance of secure connections via HTTP
and HTTPS protocols, and explore Selenium WebDriver’s navigation
20
Chapter 2 Selenium Essentials: Setup and Browser Commands
21
Chapter 2 Selenium Essentials: Setup and Browser Commands
22
Chapter 2 Selenium Essentials: Setup and Browser Commands
23
Chapter 2 Selenium Essentials: Setup and Browser Commands
24
Chapter 2 Selenium Essentials: Setup and Browser Commands
25
Chapter 2 Selenium Essentials: Setup and Browser Commands
26
Chapter 2 Selenium Essentials: Setup and Browser Commands
Selenium Installation
After successfully installing Java and Eclipse IDE, you must install
Selenium WebDriver before moving to your automation testing projects.
The following steps ensure smooth installation of Selenium WebDriver
with Eclipse IDE.
27
Chapter 2 Selenium Essentials: Setup and Browser Commands
This table covers the most popular web browsers. Each WebDriver
allows Selenium to interact with the respective browser, enabling
automated testing. The Safari WebDriver comes pre-installed with the
browser, but you may need to enable it for automation.
28
Chapter 2 Selenium Essentials: Setup and Browser Commands
29
Chapter 2 Selenium Essentials: Setup and Browser Commands
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
30
Chapter 2 Selenium Essentials: Setup and Browser Commands
Browser Commands
Opening a browser using Selenium WebDriver is a foundational step
in automating web application testing. Selenium WebDriver provides
an interface for interacting with web browsers, allowing you to control
a browser session programmatically. This includes opening a browser,
navigating web pages, and executing various user actions. The following
guide and Java code snippet demonstrate how to open a web browser
(Google Chrome, Firefox, and Microsoft Edge) using Selenium WebDriver.
To begin, ensure that you have the following prerequisites installed
and set up on your machine.
31
Chapter 2 Selenium Essentials: Setup and Browser Commands
import org.openqa.selenium.WebDriver;
// Uncomment the import for the browser you are going to use
import org.openqa.selenium.chrome.ChromeDriver;
// import org.openqa.selenium.firefox.FirefoxDriver;
// import org.openqa.selenium.edge.EdgeDriver;
32
Chapter 2 Selenium Essentials: Setup and Browser Commands
33
Chapter 2 Selenium Essentials: Setup and Browser Commands
driver.get("https://www.apress.com");
34
Chapter 2 Selenium Essentials: Setup and Browser Commands
35
Chapter 2 Selenium Essentials: Setup and Browser Commands
close Command
This command closes the current browser window or tab that’s in focus.
When you invoke the close command, the WebDriver closes the
current window or tab that’s actively in focus. If multiple tabs or windows
are open as part of the session initiated by the WebDriver, only the
active one is closed. This command is particularly useful when your test
involves working with multiple tabs or windows, and you must close them
selectively as part of the test flow.
quit Command
This method closes all windows associated with the session and safely
ends the entire WebDriver session.
The quit command is your go-to for cleaning up after completing your
tests. It ensures that all browser windows opened by the WebDriver are
closed and that the WebDriver session is terminated. This is crucial for
releasing the resources and ensuring that no orphaned browser processes
are left running after your tests have concluded.
36
Chapter 2 Selenium Essentials: Setup and Browser Commands
driver.manage().window().maximize();
By maximizing the browser window, you make sure that your tests
cover the layout and functionality of your web application at the maximum
possible viewport provided by the screen. This is particularly useful for
catching UI issues that might only manifest in a full-screen view.
driver.manage().window().minimize();
37
Chapter 2 Selenium Essentials: Setup and Browser Commands
import org.openqa.selenium.Dimension;
// Setting browser size to 1024x768
driver.manage().window().setSize(new Dimension(1024, 768));
Specifying the browser window size helps you test responsive designs
effectively. By mimicking the screen sizes of tablets, phones, or desktops,
you can ensure that your web application adapts correctly to different
resolutions. This capability is crucial in verifying the responsiveness and
fluidity of your application’s UI.
driver.manage().window().fullscreen();
38
Chapter 2 Selenium Essentials: Setup and Browser Commands
import org.openqa.selenium.Point;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
39
Chapter 2 Selenium Essentials: Setup and Browser Commands
In this example, you first open the Apress web page using a Firefox
WebDriver. Then, you set the window’s position to x=30 and y=30
coordinates on the screen, which moves the browser window to that
specific location. After that, you adjust the window’s size to 450x500 pixels,
ensuring the browser window is as large as you need for your test. This
approach gives you precise control over the testing environment, allowing
you to replicate user conditions accurately and ensure your application’s
layout and functionality are consistent across different scenarios.
By manipulating the browser window’s size and position, you can
effectively test responsive designs, position-dependent features, and the
overall user experience, ensuring your web application is robust and user-
friendly.
40
Chapter 2 Selenium Essentials: Setup and Browser Commands
41
Chapter 2 Selenium Essentials: Setup and Browser Commands
Navigating Back
To simulate pressing the browser’s back button, use WebDriver’s
navigate().back() method. This allows you to test the behavior of your
web application when a user navigates back to the previous page.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class BackClickExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Perform a click action
WebElement link = driver.findElement(By.id("link-id"));
link.click();
// Navigate back
driver.navigate().back();
driver.quit();
}
}
42
Chapter 2 Selenium Essentials: Setup and Browser Commands
Navigating Forward
You might want to move forward again in the browser’s history. The
navigate().forward() command simulates the user clicking the forward
button in their browser.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ForwardClickExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement link = driver.findElement(By.id("link-id"));
link.click();
// Navigate back
driver.navigate().back();
// Navigate forward
driver.navigate().forward();
driver.quit();
}
}
43
Chapter 2 Selenium Essentials: Setup and Browser Commands
// Navigate to a URL
driver.get("https://www.apress.com");
// Perform some actions, then refresh the page
driver.navigate().refresh();
Summary
This chapter journeyed through the foundational steps of configuring a
sophisticated automated testing environment using Java, Eclipse IDE, and
Selenium WebDriver. The exploration began with installing Java, providing
a versatile programming base for your test scripts. It then progressed to
setting up the Eclipse IDE, customizing it to serve as an efficient platform
for your development and testing endeavors.
44
Chapter 2 Selenium Essentials: Setup and Browser Commands
45
CHAPTER 3
Action Chains
The term action chain means a set of instructions are carried out sequentially
to enact the user actions in a web application to test their functionality.
Let’s use the Eclipse IDE to write test cases in Java because it is one
of Java’s most widely used IDE. Another reason to use this IDE is that it
installs the Java JRE (Java Runtime Environment), making a smooth setup
to get started.
Mouse Actions
The mouse does various operations, such as clicking, dragging, moving
from one end to another, and so on. These operations are used to enact the
user movements and test the application’s features in the browser. Let’s
start with the mouse-click functionality.
Click
The click method is one of the most used. You select an element by moving to
its center and clicking it. A click is performed with the left button of the mouse.
Code 3.1:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
48
Chapter 3 Mouse and Keyboard Actions
}
}
Double Click
A double click means clicking the left mouse button twice. The mouse
pointer first moves to the center of the web element that needs to be
double clicked.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
49
Chapter 3 Mouse and Keyboard Actions
driver.quit();
}
}
This method can trigger events like opening an item, selecting text, or
activating certain controls.
Context Click
The context click function is the action of moving the mouse pointer to the
center of an element and then right-clicking.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
50
Chapter 3 Mouse and Keyboard Actions
driver.quit();
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
51
Chapter 3 Mouse and Keyboard Actions
driver.quit();
}
}
Perform
The preceding code used the perform() function to execute the
clickAndHold() function. The perform() function enables to carry out all
mouse (except click) and keyboard actions. When more than one action
or any sequence of actions defined needs to be executed, the perform()
function is used.
Pause
A pause is a delay in actions to be performed. This delay can be used in a
sequence of actions to be performed or on a single action that needs to be
available on a web page.
52
Chapter 3 Mouse and Keyboard Actions
import java.time.Duration;
Actions actions = new Actions(driver);
actions.moveToElement(someElement)
.click()
.pause(1000) // Pause for 1000 milliseconds (1 second)
.click(anotherElement)
.perform();
Release
It is a method to release a mouse action (button) clicked earlier and held
down. One common example is drag-and-drop, where you need to drag an
element from its position to its destination and release it.
import org.openqa.selenium.interactions.Actions;
// Other imports...
Reset
This reset action in Selenium is used to reset or clear all the current state of
the actions that are listed in the action builder.
53
Chapter 3 Mouse and Keyboard Actions
actions.moveToElement(anotherElement)
.contextClick()
.perform();
As seen in the preceding code snippet, you can build a new sequence
of actions from the start without creating an Action object.
Mouse Movements
Mouse movement refers to the cursor or pointer that moves on the screen
with the help of the mouse. These movements are chained into various
actions in Selenium that are explained next.
Move to Element
This method moves the mouse cursor to the center of the defined web
element, which imitates the hover behavior. It is used in hover-related
cases like drop-down menus or exploring hidden elements when hovering.
54
Chapter 3 Mouse and Keyboard Actions
# Initialize WebDriver
driver = webdriver.Chrome()
# Navigate to website
driver.get("https://example.com")
This method moves the mouse pointer to the center of the web
element. If the web element is not available then it raises an error.
Move by Offset
In Selenium, you can move the mouse pointer by defining the number of
pixels as offset values. The mouse pointer can be at the current position
in the viewport or on a specific web element. The offset values are x and y
coordinates defining the position to move the mouse pointer.
Note Viewport is the display region of the web page that is seen on
the browser window.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
55
Chapter 3 Mouse and Keyboard Actions
// Move the mouse 50 pixels to the right and 100 pixels
down from the viewport's top-left corner
actions.moveByOffset(50, 100).perform();
The offset value starts from the upper-left corner of the screen defined
at (x=0, y=0). Remember that there is no default value of the offset and the
offset value is always relative to the current position of the mouse pointer.
Let’s look at the various methods available to move the mouse pointers.
56
Chapter 3 Mouse and Keyboard Actions
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
This code moves the mouse pointer to the center of the defined
web element (i.e., targetElement) and then immediately moves it by the
specified offset values. This combined action is executed by the perform()
function.
57
Chapter 3 Mouse and Keyboard Actions
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
// Move the mouse 50 pixels to the right and 100 pixels
down from the viewport's top-left corner
actions.moveByOffset(50, 100).perform();
58
Chapter 3 Mouse and Keyboard Actions
There are two offset values: x and y. The x offset value depicts
horizontal movement, and the y offset value depicts vertical movement
of the cursor. This example moved the mouse cursor using offset values
starting from the viewport’s top-left corner.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
59
Chapter 3 Mouse and Keyboard Actions
Note If the mouse pointer has not moved by any prior Selenium
command, then the mouse pointer is located upper-left corner of
the viewport. Also, remember the mouse pointer position remains
unchanged when the page is scrolled.
In this example, the mouse pointer moves by 30 pixels from its current
position, considering the x and y offsets. This method can be used when
the mouse pointer’s starting point varies or the movement does not
depend on any web element.
<!DOCTYPE html>
<html>
<head>
60
Chapter 3 Mouse and Keyboard Actions
<style type="text/css">
#drag, #drop {
float: left;
padding: 15px;
margin: 15px;
-moz-user-select: none;
}
#drag {
background-color: #A9A9A9;
height: 50px;
width: 50px;
border-radius: 50%;
cursor: pointer; /* Makes it clear the element is
draggable */
}
#drop {
background-color: #fd8166;
height: 100px;
width: 100px;
border-radius: 50%;
border: 2px dashed #000; /* Visual cue for drop area */
}
</style>
<script type="text/javascript">
function dragStart(event) {
event.dataTransfer.setData("Text", event.target.id);
event.dataTransfer.effectAllowed = 'move';
return true;
}
61
Chapter 3 Mouse and Keyboard Actions
function dragEnter(event) {
event.preventDefault();
return true;
}
function dragOver(event) {
event.preventDefault(); // Necessary for
allowing a drop
return false;
}
function dragDrop(event) {
var src = event.dataTransfer.getData("Text");
var srcElement = document.getElementById(src);
event.target.appendChild(srcElement);
srcElement.style.backgroundColor = 'green';
// Change color upon drop
event.stopPropagation();
return false;
}
</script>
</head>
<body>
<h1>Drag and Drop</h1>
<center>
<div id="drop" ondragenter="return dragEnter(event)"
ondrop="return dragDrop(event)" ondragover="return
dragOver(event)">
Drop here
</div>
<div id="drag" draggable="true" ondragstart="return
dragStart(event)">
<p>Drag</p>
62
Chapter 3 Mouse and Keyboard Actions
</div>
</center>
</body>
</html>
Figure 3-1 shows two circles; one is larger than the other. You can
drag the smaller circle using an element and drop into the larger circle or
drop zone. To distinguish when the circle is dropped into a larger one, the
background color of the smaller circle changes color, which is mentioned
in the JavaScript dragDrop function.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
63
Chapter 3 Mouse and Keyboard Actions
After executing the drag-and-drop method, the circle dragged into the
drop zone turns green, as seen in Figure 3-2.
64
Chapter 3 Mouse and Keyboard Actions
The background of the smaller circle turns green when the circle is
dropped. Next, let’s look at how to drag and drop using pixel values (offset
values).
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
65
Chapter 3 Mouse and Keyboard Actions
import org.openqa.selenium.Point;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
// Open the web page with the drag and drop elements
driver.get("URL_of_your_drag_and_drop_page");
66
Chapter 3 Mouse and Keyboard Actions
This code calculates the offset values based on the positions of the
source and target elements and then executes the drag-and-drop action
for the HTML created. The next section looks at keyboard actions.
Keyboard Actions
In Selenium WebDriver, keyboard actions automate key press and release.
It can be a combination of one or more such key events where the web
application’s functionality is tested. Let’s discuss various keyboard actions
used to interact with web applications.
Keys
The keys are provided with a set of constant values in the form of Unicode
representing special and non-printable keys on the keyboard. These
constant values allow you to automate key presses. The keys with their
Unicode’s are given in Table 3-1.
67
Chapter 3 Mouse and Keyboard Actions
Key Down
The keyDown method imitates key press in keyboard action. The key
press means pressing a key without releasing it. When you want letters
to be uppercased or when you want to use special characters, hold the
Shift key. To select multiple elements by holding the Ctrl key, you use the
keyDown method.
Key Up
The keyUp method releases a key pressed before and performs other
keyboard actions afterward. It is primarily used with the keyDown method
imitating a complete key press and release action.
68
Chapter 3 Mouse and Keyboard Actions
Send Keys
When you want to enter text into a web element (e.g., passwords, text
areas, and search boxes), use the sendKeys method. You can chain
multiple actions with this method. An example of typing letters in
uppercase letters is shown in the following code.
The key remains in the pressed state until you end the chain action or
use the keyUp method discussed next.
69
Chapter 3 Mouse and Keyboard Actions
This example code used the keyDown and keyUp methods with
sendKeys, simulating text entered in the Google search box in uppercase.
Scroll
When referring to operations, you can use the mouse’s actions apart from
clicks by the mouse’s scroll wheel. In Selenium WebDriver, you can scroll
the web page up or down. Selenium WebDriver does not natively support
scroll actions like mouse and keyboard actions. Next, let’s discuss actions
that are used to stimulate scrolling actions.
Scroll to Element
When dealing with elements that are present outside of the viewport,
you use the scrollToElement method. The Action class in Selenium does
not automatically scroll the target element into the viewport like more
conventional methods, such as click() or sendKeys(), do. As a result, before
performing actions on an element using the Actions class, you frequently
need to make sure that it is visible.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
70
Chapter 3 Mouse and Keyboard Actions
actions.moveToElement(elementToScrollTo).perform();
}
}
71
Chapter 3 Mouse and Keyboard Actions
This example scrolls by 50 pixels to the right and 100 pixels down. This
operation can be helpful when testing lazy-loading items, infinite scrolling
capabilities, or any other functionality that is activated after a particular
amount of scrolling.
72
Chapter 3 Mouse and Keyboard Actions
is used when you want to test web elements or functionalities that are
present immediately to the side or below a specified reference element.
The example defined the element available in the viewport and then
scrolled using the scrollIntoView method with the number of pixels. Next,
let’s look at scrolling an element with an offset.
73
Chapter 3 Mouse and Keyboard Actions
74
Chapter 3 Mouse and Keyboard Actions
// Define the scroll origin as a point 100 pixels right and 200
pixels down from the upper-left corner of the viewport
WheelInput.ScrollOrigin scrollOrigin = WheelInput.
ScrollOrigin.fromViewport(100, 200);
75
Chapter 3 Mouse and Keyboard Actions
Summary
This chapter discussed action chains for stimulating complex user
interactions incorporating several steps or using a keyboard and mouse
simultaneously. The action chains enable you to automate testing the
functionality of the web applications.
With Action chains, you can perform a variety of keyboard actions
like key presses, text entry, and key combinations. They also allow mouse
actions like clicks, double clicks, right clicks, drag and drop, and mouse
movements over elements. These actions are useful for testing interactive
functionalities such as web elements, hover states and drag-and-drop.
Action chains also facilitate scroll actions, enabling automated
scrolling to specific web elements or by predetermined offsets. This is a
crucial feature for testing web elements that are not available immediately
on a page. The chain of actions is carried out by executing with the
perform() method, enabling a comprehensive approach for stimulating
real-world scenarios of user interactions in testing a web application.
The chapter also discussed methods like reset(), release(), and pause().
76
CHAPTER 4
Web Elements
This chapter explores the essential concept of web element locators in
Selenium WebDriver, a fundamental aspect of web automation. Starting
with an introduction to locators and why they are indispensable in
Selenium, you set the foundation for understanding their role in effective
automation. You then delve into the Document Object Model (DOM),
which is essential for comprehending how Selenium interacts with web
elements.
A detailed examination of the eight primary types of locators in
Selenium follows, including their practical applications, syntax, and
scenarios. From simple ID and class name locators to complex XPath and
CSS selectors, each type is discussed to provide a thorough understanding
of their usage.
Finally, you address common challenges encountered with locators
and share best practices for overcoming these issues, aiming to enhance
the reliability and efficiency of your automation scripts. This chapter is
designed to equip you with the knowledge and expertise needed to master
the use of locators in Selenium WebDriver.
links, and more. Locators pinpoint these elements within the DOM of
a web page, allowing automated scripts or tests to perform actions like
clicking, typing, or extracting information from them.
Web element locators ensure that web application functionality and
the user interface work as intended. They are widely used in various
testing scenarios, including functional testing, regression testing, and
compatibility testing, to verify that web applications behave correctly and
consistently across different browsers and platforms.
78
Chapter 4 Web Elements
79
Chapter 4 Web Elements
Attributes
Attributes provide additional information about HTML elements and are
included within the opening tag. Attributes consist of a name and a value,
separated by an equal sign. The following is an example using the src
attribute for an image element.
80
Chapter 4 Web Elements
81
Chapter 4 Web Elements
R
elationships in the DOM
Elements in the DOM can have a parent-child relationship, sibling
relationship, or both. This hierarchical structure allows you to navigate and
manipulate the document efficiently.
• Parent-child relationship: This relationship describes
elements nested within other elements. For instance,
if you have a <div> element with a <p> element inside it,
the <div> is considered the parent and the <p> is the child.
• Sibling relationship: Siblings are elements on the same
level or share the same parent. For example, if two <div>
elements are placed one after the other within the same
parent element, they are considered siblings.
<!DOCTYPE html>
<html>
<head>
<title>Our Document</title>
</head>
<body>
<div id="content">
<h1>Welcome to Our Page</h1>
<p>This is a paragraph in our document.</p>
<a href="https://example.com">Visit Example</a>
</div>
</body>
</html>
82
Chapter 4 Web Elements
83
Chapter 4 Web Elements
Locators
Let’s explore various Selenium WebDriver locators, including ID, name,
XPath, CSS selectors, link text, partial link text, tag name, and class name.
These locators provide different methods for identifying and interacting
with web elements. Each locator type has a description, Java syntax, and
an HTML code example.
ID Locator
The ID locator in Selenium is used to find an element by its unique ID. It’s
one of the most efficient ways to locate elements, as IDs are supposed to be
unique within an HTML document.
Java Syntax
<button id="submitBtn">Submit</button>
The code uses the By.id method to find the button with the ID
submitBtn. This approach is straightforward and effective due to the ID’s
uniqueness.
84
Chapter 4 Web Elements
Name Locator
The name locator is used to find an element by its name attribute. It is
useful when the name attribute is available and can conveniently locate
elements, especially in forms.
Java Syntax
Here, an input field for a username with the name attribute set to
“username”. This attribute can be used to locate the input field.
The code locates the input field by searching for an element with the
“username” name attribute. This method is particularly useful in scenarios
like forms where the name attribute is commonly used.
85
Chapter 4 Web Elements
Java Syntax
This syntax locates link (<a>) elements based on their exact text
content. It’s ideal for scenarios where you need to interact with text-
specific links.
<a href="login.html">Login</a>
The example contains a link with the text “Login”. This text, being
distinct and exact, can be used to locate the link.
The code finds the link element that exactly matches the text. This
approach is straightforward and efficient for locating text-based links.
Java Syntax
This command finds link elements that contain the specified substring
in their text. It offers more flexibility compared to the exact link text locator.
86
Chapter 4 Web Elements
In this case, the link text is longer. If you only remember a part of it, like
“about you”, you can still locate the link.
This code finds any link that includes the text “about you” in its
visible text. It’s a versatile approach for links with long or partially
remembered texts.
Java Syntax
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
</ul>
Here, you have several list items (<li>). If you want to interact with all
list items, you can use their tag name for location.
87
Chapter 4 Web Elements
The code retrieves all elements with the <li> tag. It’s effective for
scenarios where you need to interact with or assess multiple elements of
the same type.
Java Syntax
The code locates the <div> element using its error-message class.
This method is particularly useful when dealing with elements styled or
grouped by a common class.
88
Chapter 4 Web Elements
XPath Locators
Absolute XPath
Absolute XPath starts from the root node and navigates down the
document hierarchy, specifying each element in the path. It begins with
a single slash / and provides a direct way to access any element in the
DOM. However, it’s fragile because any change in the document’s structure
can invalidate the path.
Java Syntax
<html>
<body>
<div>
<p>Paragraph text</p>
</div>
</body>
</html>
89
Chapter 4 Web Elements
The Java code navigates from the root of the HTML document down to
the specific <p> element. This approach is very specific and relies on the
exact structure of the HTML document.
R
elative XPath
Relative XPath provides a more flexible approach to locating elements. It
starts with a double slash //, indicating that the element can be anywhere
in the document. This method is less prone to breaking with changes in
the document structure.
Java Syntax
The // indicates that Selenium should search for the element at any
location within the HTML document that matches the tag and attribute
criteria.
HTML Code Example
<div>
<button id="submit">Submit</button>
</div>
This line of Java code tells Selenium to find any button element in the
document with an id of “submit”. This method is more resilient to DOM
changes than absolute XPath.
A
ttribute-Based XPath
Attribute-based XPath locates elements based on their attributes. It’s
highly useful when elements have unique attributes that can be used for
identification, like id, name, or custom attributes.
Java Syntax
This XPath expression searches for an element with a specific tag and
a given attribute value. It’s a precise way to locate elements with unique or
distinct attributes.
HTML Code Example
Suppose you want to locate an input field for username entry. This
input field has a distinctive name attribute that you can target.
Java Code to Locate Web Elements
91
Chapter 4 Web Elements
This code locates the input field based on its name attribute. Using
//input[@name='username'], you direct Selenium to find any input
element with a “username” name attribute, regardless of its position in
the DOM.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
You might be interested in selecting the second list item on this list.
The items are identical in tags and attributes, so you use their position to
distinguish them.
Java Code to Locate Web Elements
92
Chapter 4 Web Elements
The code locates the second <li> element within the unordered list.
The XPath expression (//ul/li)[2] directs Selenium to select the second
li element, demonstrating how positional filters can target elements
based on their order in the DOM.
This XPath syntax targets elements that satisfy all specified conditions.
This example finds elements with a specific tag with attr1 with value1
and attr2 with value2.
HTML Code Example
93
Chapter 4 Web Elements
The code snippet finds an input element that is both of type email
and possesses the required attribute. It demonstrates how using the
and operator in XPath can pinpoint elements that fulfill multiple specific
criteria.
Using or Operator
The or operator allows you to select elements that satisfy at least one of
multiple specified conditions. It’s useful when there are several possible
criteria for identifying an element.
Java Syntax
This syntax selects elements that meet any one of the provided
conditions. The or operator broadens the selection scope by accepting
elements that match either condition.
HTML Code Example
<button id="confirm">Confirm</button>
<button id="proceed">Proceed</button>
94
Chapter 4 Web Elements
This syntax finds elements where a specific condition is false. The not
operator is used to select elements that do not have a certain attribute or
attribute value.
HTML Code Example
95
Chapter 4 Web Elements
C
SS Selectors
CSS (cascading style sheets) are primarily used on a web page written in
HTML or XML, defining how web elements are displayed and covering
aspects like layout, colors, fonts, and so on. The CSS applies styles to one
or a group of web elements. You use CSS selector to identify and select
these elements on a web page. The CSS selectors try to match the parts of
the content in the web document to the styles defined by the CSS.
CSS selectors have various forms, allowing developers to target
elements with precision. Here are some of the primary types of selectors
and how they are used in Selenium.
Basic Selectors
Basic selectors are the simplest, directly targeting elements based on their
type, class, or ID.
96
Chapter 4 Web Elements
Type Selector
This method locates the elements based on their tag name. It is commonly
used to select all web elements of a specific type.
Java Syntax
By.cssSelector("tagName")
<p>This is a paragraph.</p>
This example targets <p> elements in the HTML document. You can
locate all paragraphs using the type selector to apply specific styles or
interactions.
Java Code to Locate Web Elements
This line of Java code uses Selenium WebDriver to locate the first
paragraph element on the web page. It uses the findElement method
with a CSS selector that specifies the tag name p, thus targeting paragraph
elements.
Class Selector
By.cssSelector(".className")
97
Chapter 4 Web Elements
The dot (.) prefix in the CSS selector indicates that you are targeting
elements by their class name. This syntax is used with the By.cssSelector
method in Selenium to find elements with the specified class attribute.
HTML Code Example
<div class="alert">Warning!</div>
Here, you aim to select elements with the class alert. The class
selector allows you to target and style all elements with class="alert",
which can be useful for highlighting warnings or important information
on a page.
Java Code to Locate Web Elements
In this snippet, the findElement method locates the first element with
the class alert using the .alert CSS selector. It allows you to interact with
or apply specific operations to elements identified by this class.
ID Selector
This method uses the element’s ID attribute for selection, ideal for
targeting a unique element within the page.
Java Syntax
By.cssSelector("#idValue")
The hash # prefix specifies that you are using an ID selector. This
is used with By.cssSelector in Selenium to locate an element with a
specific ID.
HTML Code Example
<button id="submitBtn">Submit</button>
98
Chapter 4 Web Elements
Here, the findElement method finds the button with the ID submitBtn.
This method precisely locates the element, enabling actions such as clicks
or data retrieval specific to this button.
Universal Selector
Locates all elements within the HTML document. It’s a powerful selector
for applying broad styles or actions across all page elements.
Java Syntax
By.cssSelector("*")
Combinators
Combinators are selectors that establish relationships between elements,
allowing for selecting elements based on their hierarchical relationship.
99
Chapter 4 Web Elements
By.cssSelector("ancestor descendant")
<div class="container">
<p>A paragraph inside a container.</p>
</div>
You can locate the <p> element inside a <div> with the class
container. The descendant selector allows you to select the paragraph by
specifying its relationship with its ancestor.
Java Code to Locate Web Elements
100
Chapter 4 Web Elements
Java Syntax
The > combinator is used between two selectors to target elements that
are direct children of a specified parent, providing a more precise selection
than the descendant selector.
HTML Code Example
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
</ul>
You use the child combinator to specifically target <li> elements that
are direct children of <ul>. This ensures you only select list items directly
within the unordered list, not nested lists.
Java Code to Locate Web Elements
This code finds all <li> elements that are direct children of an <ul>. It’s
particularly useful for situations where precise control over the hierarchy
is needed.
By.cssSelector("previousElement + nextElement")
<h2>Title</h2>
<p>First paragraph following the title.</p>
To select the <p> that directly follows an <h2>, the adjacent sibling
selector is ideal. It targets the first paragraph right after the specified heading.
Java Code to Locate Web Elements
This code snippet locates the first paragraph (<p>) that directly follows
an <h2> element, demonstrating how to use the adjacent sibling selector
for precise element targeting based on sibling relationships.
It finds all siblings of a specified element with the same parent, allowing
broad sibling selection.
Java Syntax
By.cssSelector("sibling ~ sibling")
<h2>Title</h2>
<p>Paragraph 1.</p>
<p>Paragraph 2, also following the title.</p>
Using the general sibling selector, you can target both <p> elements
that follow the <h2>, regardless of their immediate adjacency.
102
Chapter 4 Web Elements
This code finds all <p> elements that are siblings of and follow an <h2>
element. It’s useful for selecting multiple related elements for actions or
validations.
Combinators and selectors in CSS offer powerful ways to target elements
within a web page. When used with Selenium WebDriver in Java, they
provide precise control over which elements to interact with, enhancing the
capability to automate web testing and interactions effectively.
Attribute Selectors
Attribute selectors provide a powerful way to select elements based on
their attributes and values, offering various matching options.
Presence
By.cssSelector("[attribute]")
103
Chapter 4 Web Elements
This example can target input elements marked as required using the
presence selector. It allows you to identify all elements that the user must
fill out.
Java Code to Locate Web Elements
This code snippet locates all <input> elements with the required
attribute. It showcases using the presence attribute selector to focus on
elements essential for form validation.
Exact Value
Java Syntax
By.cssSelector("[attribute='value']")
<button type="submit">Submit</button>
<button type="button">Cancel</button>
104
Chapter 4 Web Elements
This line of Java code specifically locates the submit button by matching
the type attribute exactly with the value “submit”. It demonstrates how to
use the exact value selector for targeting elements with specific roles or
functions.
By.cssSelector("[attribute*='value']")
You use the contains selector to select links containing the substring /
profile/ in their href attribute.
Java Code to Locate Web Elements
105
Chapter 4 Web Elements
Targets elements with an attribute value that begins with a specified substring.
Java Syntax
By.cssSelector("[attribute^='value']")
<a href="https://example.com/dashboard">Dashboard</a>
This code locates <a> elements whose href attribute starts with “https://
example.com/dashboard”, demonstrating the begins with selector’s ability to
pinpoint elements based on the initial portion of their attribute values.
Java Syntax
By.cssSelector("[attribute$='value']")
106
Chapter 4 Web Elements
<img src="logo.png">
You use the ends with selector to select images ending with .png in
their src attribute.
Java Code to Locate Web Elements
This snippet locates <img> elements whose src attribute ends with
.png, showcasing the ends with the selector’s capability to focus on
elements by the terminal portion of their attribute values.
Specificity
By.cssSelector("[attribute|='value']")
107
Chapter 4 Web Elements
<html lang="en-us">
<head>
<title>Example Page</title>
</head>
<body>
<p lang="en">English Content</p>
<p lang="en-us">American English Content</p>
<p lang="en-gb">British English Content</p>
</body>
</html>
The first line of Java code uses Selenium WebDriver to locate all <p>
elements where the lang attribute is exactly “en-us” or starts with “en-”
followed by any character sequence, effectively targeting American English
content. The second line demonstrates how to target elements designated
for general English content by matching the lang attribute exactly with
“en” or starting with “en-” followed by a hyphen, although in practical use,
the exact match “en” would not follow the hyphen rule and is more about
demonstrating the syntax flexibility. This approach showcases the utility
108
Chapter 4 Web Elements
<ul id="social-media-links">
<li>Facebook</li>
<li>Twitter</li>
<li>Instagram</li>
</ul>
Three social media lists are embedded in the <ul> tag in the code.
To locate the first element on the list, use the following Java code.
Java Code to Locate Element
This line of code locates the first <li> element within the #social-
media-links list, effectively targeting the “Facebook” link. It demonstrates
using the first-child pseudo-class for selecting specific child elements.
109
Chapter 4 Web Elements
This snippet selects the last <li> element within the #social-media-
links list, focusing on the “Instagram” link. It showcases how the :last-
child pseudo-class can be utilized to pinpoint the last element in a group.
The :nth-child(n) pseudo-class selects the nth child element within its
parent, with counting starting at 1. To select the Twitter link, which is the
second element.
Java Code to Locate Web Elements
This code finds the second <li> element in the list, locating the Twitter
link. The :nth-child(2) pseudo-class enables the selection of elements
based on their order in a sequence.
110
Chapter 4 Web Elements
Java Syntax
<ul class="products">
<li>Product 1</li>
<li>Product 2</li>
<li>Product 3</li>
</ul>
In this example, the web page contains a list of products. Each product
is listed within an <li> tag. If you want to interact with all these product list
items, locate them as a group.
111
Chapter 4 Web Elements
This loop iterates through each element in the products list, printing
the text of each product. This approach is useful for scenarios where
actions need to be performed, or information needs to be extracted from a
collection of similar elements.
Locating multiple elements is a fundamental aspect of Selenium
WebDriver, enabling batch operations on groups of elements and
facilitating efficient automation of repetitive tasks. Understanding and
using findElements() effectively allows for more dynamic interaction with
web pages and broadens the scope of automation possibilities.
112
Chapter 4 Web Elements
113
Chapter 4 Web Elements
114
Chapter 4 Web Elements
115
Chapter 4 Web Elements
116
Chapter 4 Web Elements
S
ummary
This chapter delved into the essential concepts of web element locators
within the context of Selenium WebDriver, a pivotal tool in web
automation. Locators are the cornerstone for identifying and interacting
with elements on a web page, making their understanding and effective
use a prerequisite for any successful web automation, testing, or scraping
endeavor.
You began by exploring the fundamental question: What are locators,
and why are they necessary? This discussion lays the groundwork
for understanding the importance of locators in interacting with web
elements, especially considering the diverse and dynamic nature of
modern web pages.
Following this, you explored the Document Object Model (DOM),
discussing its basic concepts and the relationships between various
elements within the DOM. This understanding is crucial as locators
operate within the context of the DOM, traversing its structure to pinpoint
elements.
The chapter comprehensively explains eight types of locators used in
Selenium WebDriver. Each locator type—from ID and class name to more
complex XPath and CSS selector locators—is discussed. You have seen
the insights into their syntax, usage, and scenarios where they are most
effectively employed, accompanied by practical HTML examples and Java
code snippets for a well-rounded understanding.
Finally, the chapter addresses common challenges faced when using
locators. It discussed best practices for overcoming these challenges and
ensuring robust, reliable, and maintainable web automation scripts. This
section aims to equip readers with the knowledge to tackle common
pitfalls and optimize their locator strategies.
You now understand locator strategies, their applications, and best
practices in Selenium WebDriver, laying a strong foundation for successful
web automation projects.
117
CHAPTER 5
Navigations
This chapter explores how to work with hyperlinks using Selenium
WebDriver with Java. Hyperlinks are the clickable elements that take you
from one page to another, acting like bridges in your online journey.
They are vital to understand in testing as they are crucial in moving around
websites.
In web applications, navigation is also referred to as a link or URL
(Uniform Resource Locator) that references data such as documents,
videos, images, and so on or helps the migration between pages or within
the page. This chapter describes various ways to locate and work with
hyperlinks, exploring use locators like ID, text, partial link, and XPath
to locate hyperlinks. You learn how to list hyperlinks on a web page and
check whether they work as expected.
The chapter looks at how to check images on a web page to ensure they
are not broken and display as intended. It explains data attributes used to
store extra information and how to interact with them.
This chapter guides you through the practical knowledge and skills
needed to navigate web pages, ensuring your automated tests are strong
and effective. Your learning is supported through examples and use
cases so that you can apply them in real-world scenarios using Selenium
WebDriver and Java.
Hyperlinks
Hyperlinks are web elements that help users navigate a web page or move
to a completely different site. This navigation web element is embedded in
anchor tags <a> in HTML, representing a medium to traverse through the
World Wide Web or data that can be streamed or downloaded.
Hyperlinks are primarily associated with menus, buttons, images, and
documents styled with CSS and JavaScript. Hyperlinks are also called links.
The following is the syntax for creating a link.
<a href="URL">link_text</a>
<div class="container">Selenium
<a href="java.html" id="java", data-info="javalink">Java</a>
<a href="python.html" id="python", data-info="pythonlink">
Python </a>
<a href="csharp.html" id="csharp", data-info="csharplink">CSharp</a>
</div>
Hyperlink by ID
You can locate hyperlinks using an ID in an anchor tag. Web applications
are now available in multiple languages, and ID is the best way to locate a
hyperlink that has not changed.
120
Chapter 5 Navigations
Hyperlink by Text
It is very helpful to locate a hyperlink by its visible text when the text is
static and distinct. This method is language-dependent and may not
be suitable for multilingual websites unless the test is also adapted for
different languages.
Once the hyperlink is located using its visible text, you can perform
various interactions, such as clicking or link verification can be performed
by Selenium WebDeriver.
Hyperlink by XPath
It is a more flexible way to locate hyperlinks, especially when you cannot
locate links using simpler locator strategies or dealing with complex DOM
structures.
121
Chapter 5 Navigations
nth Hyperlink
You can use the nth hyperlink method when dealing with a list of similar
or identical hyperlinks where direct attributes may not be available or
useful. This approach is index-based and may not be effective when there
are changes in the order or number of hyperlinks; hence, it requires careful
management and validation of the index used to locate the hyperlink.
Testing Hyperlinks
Hyperlinks are the primary source for navigation and accessing various
resources and information. Testing hyperlinks is important for various
reasons.
122
Chapter 5 Navigations
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import java.net.HttpURLConnection;
import java.net.URL;
123
Chapter 5 Navigations
driver.quit();
}
}
124
Chapter 5 Navigations
1xx The request was received and the 100 Continue, 101
(Informational) process is continuing. Switching Protocols
2xx (Successful) The request was successfully 200 OK, 201 Created
received, understood, and accepted.
3xx (Redirection) Further action needs to be taken to 300 Multiple Choices,
complete the request. 301 Moved Permanently
4xx (Client The request contains bad syntax or 400 Bad Request, 404
Errors) cannot be fulfilled by the server. Not found
5xx (server The server failed to fulfill a valid 500 Internal Server Error,
Errors) request. 502 Bad Gateway
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
125
Chapter 5 Navigations
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
driver.quit();
}
}
126
Chapter 5 Navigations
Summary
This chapter explored the complex world of hyperlinks in your testing
using Selenium WebDriver and Java. You learned various strategies for
identifying and interacting with hyperlinks, utilizing different locators.
The chapter investigated image validation, ensuring that images on
a web page are not broken and displayed correctly. Furthermore, you
explored the concept of data attributes in hyperlinks, understanding
their role in storing additional information, and learned how to interact
with them.
The chapter equips you with practical knowledge and skills through
clear examples and use cases, ensuring that the strategies discussed are
applicable in real-world testing scenarios. This foundational knowledge
is crucial for navigating web pages and ensuring that automated tests are
robust and effective.
127
CHAPTER 6
Buttons
This chapter explores a wide array of interactive elements commonly
encountered in web interfaces, such as buttons, radio buttons, checkboxes,
and drop-down lists. Each element offers unique functionalities and user
interactions, making it imperative for testers to understand and effectively
automate their behavior.
Your journey begins with buttons—how users interact with web pages.
You’ll explore various types: standard, submit, image, JavaScript, disabled,
and toggle buttons. Each type introduces its own set of functionalities and
complexities. Next, you’ll focus on radio buttons and checkboxes, which
are essential for single and multiple option selections. Understanding
how to interact with and validate these elements accurately is crucial for
form-based testing. Then, you’ll explore the SelectList and MultiSelectList
elements, which are critical for user selection in drop-down formats.
Learning to interact with and validate single and multiple options in these
lists is key for testing comprehensive form-related features.
Throughout this chapter, your aim is not just to interact with these
elements but also to confirm their types and validate the outcomes of
your interactions. It outlines key considerations to consider when testing
buttons using Selenium. These points serve as a guide to help you create
thorough, effective test cases and ensure your application’s buttons are
reliable and user-friendly. This ensures your tests are precise, reliable, and
reflect real-world user experiences.
As you move from one element to the next, the chapter is designed
to provide a smooth transition, ensuring you understand how each
component fits into the broader context of web application testing.
In Selenium, you can interact with standard buttons using the click()
method after locating them with selectors like By.id, By.className, or
By.cssSelector.
130
Chapter 6 Buttons
You locate this button by its ID and perform a click action. After the
interaction, you might validate its visibility or any other page changes it
triggers.
// Type validation
Assert.assertEquals("button", standardButton.
getAttribute("type"), "Not a standard button.");
Submit Button
A submit button is specifically designed to submit a form to a server.
It’s created using the <input> element with the type attribute set to
submit (<input type=“submit”>). When clicked, it sends the form data to
the server.
<form action="/submitDestination">
<input type="submit" id="submitButton" value="Submit"
style="width:100px; height:50px;">
</form>
Figure 6-2 shows the submit button, similar to standard buttons but
often labeled with Submit or other action-oriented text.
131
Chapter 6 Buttons
// Type validation
Assert.assertEquals("submit", submitButton.
getAttribute("type"), "Not a submit button.");
132
Chapter 6 Buttons
Image Button
An image button uses an image as a clickable area and is created using
the <input> element with the type attribute set to “image” (<input
type=“image” src=“path/to/image.jpg”>). It can serve as a submit button
and a visually appealing clickable element.
<form action="/imageClickDestination">
<input type="image" id="imageButton" src="buttonImage.jpg"
alt="Submit" style="width:100px; height:50px;">
</form>
The button appears as the image provided in the src attribute, offering
a visual cue for interaction that can be seen in Figure 6-3.
You can interact with image buttons like standard or submit buttons.
The click() method is used after locating the element. Ensure you consider
the image loading time and visibility during interactions.
133
Chapter 6 Buttons
// Type validation
Assert.assertEquals("image", imageButton.getAttribute("type"),
"Not an image button.");
From the visual cues of image buttons, you delve into the more
dynamic and script-driven nature of JavaScript buttons.
JavaScript Button
A JavaScript button doesn’t necessarily refer to a specific type of button
element but to any clickable element that triggers JavaScript code. It could
be a standard button, a link, or div with an onclick event.
When you perform a click action on the button shown in Figure 6-4,
the JavaScript is triggered and pops up an alert box.
134
Chapter 6 Buttons
Interacting with these involves not just a click but handling the
dynamic elements they might invoke, such as pop-ups or changes in the
DOM, leading you into more complex wait and validation scenarios.
Disabled Button
Disabled buttons are unique as they’re meant to be non-interactive.
It’s often grayed out and is set using the disabled attribute (<button
disabled>). They’re essential for scenarios where certain conditions must
be met before an action can be taken.
135
Chapter 6 Buttons
You should first check if the button is disabled using the isEnabled()
method. This can be part of your test validation to ensure buttons are
enabled or disabled as expected under certain conditions.
Assert.assertEquals("button", disabledButton.getAttribute
("type"), "Not a standard button or incorrect type.");
// Type validation
136
Chapter 6 Buttons
Toggle Button
Toggle buttons are interactive elements that switch between two states,
such as on/off, with each click, offering a dynamic user experience.
You can interact with toggle buttons using the click() method. You may
also want to verify the button’s state before and after the click to ensure it’s
performing the toggle action correctly. This can be done by checking the
button’s attributes or associated text.
137
Chapter 6 Buttons
You click the toggle button and then validate that its state has indeed
toggled from its previous state. These require a check before or after the
click to ensure the state has changed as expected, introducing a cyclical
pattern to your interaction and validation process. In some cases, you need
to check each state’s effect when clicked.
Toggle buttons do not have a unique type, so you can validate them by
their function to toggle between the two states on/off, as intended during
user interactions.
Radio Buttons
Radio buttons are fundamental elements in web forms, ensuring users
make a singular, clear choice. Each set of radio buttons is grouped by
a shared name attribute, allowing only one button in the group to be
selected at a time. This exclusivity is vital in scenarios like surveys or
settings requiring a definite answer.
The following is HTML code for a set of radio buttons asking users
about their preferred genre of music.
<!DOCTYPE html>
<html>
<head>
<h2>Music Preference Survey</h2>
</head>
<body>
<form>
<input type="radio" id="rock" name="music" value="rock">
<label for="rock">Rock</label><br>
138
Chapter 6 Buttons
In this structure, each radio button is marked with a unique ID, making
them easily identifiable. The label tags enhance user accessibility and
provide a larger clickable area.
Next, take a look at Figure 6-7.
Figure 6-7 shows the default unchecked and checked radio buttons
when a click operation is performed. It demonstrates whether a radio
button is selected, and if selected, which option, ensuring you develop test
scenarios accordingly.
Note A black dot in a radio button shows that the button has been
selected.
139
Chapter 6 Buttons
By ID
This method is precise and efficient, especially when each radio button has
a unique ID.
Using Label
Locating by label is particularly useful when the ID is dynamic or part of a
complex structure.
140
Chapter 6 Buttons
Assert.assertEquals("radio", rockRadio.getAttribute("type"),
"The element is not a radio button.");
This assertion checks if the Rock radio button is selected. If it’s not, the
test fails, indicating a potential issue with the selection process.
141
Chapter 6 Buttons
Checkboxes
Checkboxes are a staple in web forms, allowing you to make multiple
selections. They’re versatile, providing various choices, unlike radio
buttons, which limit users to a single selection. Understanding checkbox
interactions is vital for any tester to ensure forms capture user inputs
accurately.
Consider an HTML form asking users about their hobbies. This
example serves as your testing ground.
<!DOCTYPE html>
<html>
<head>
<h2>Hobbies Selection</h2>
</head>
<body>
<form>
<input type="checkbox" id="music" name="hobby"
value="Music">
<label for="music">Music</label><br>
142
Chapter 6 Buttons
Each checkbox is uniquely identified by its ID, and the label provides
a human-readable text. Let’s look at how to locate and interact with these
checkboxes.
Figure 6-8 shows three checkboxes. The top image shows that none
of the checkboxes are checked or selected. In the bottom image, two
checkboxes are selected among the three. Figure 6-8 helps you understand
that more than one checkbox can be selected, which is not the case with
radio buttons.
By ID
You begin by identifying and interacting with checkboxes using their
unique IDs. This step is foundational, leading you naturally into the
exploration of more complex locating strategies.
143
Chapter 6 Buttons
Using Label
This method locates checkboxes by their labels, a strategy that reflects
how users might interact with the form. This approach sets the stage for
understanding the importance of user-centric testing.
By Name
When you want to interact with a group of checkboxes, a scenario that
introduces you to handling multiple elements and paves the way for more
advanced interactions, you can use the following Java snippet to find all
checkboxes by their name attribute:
144
Chapter 6 Buttons
break;
}
}
145
Chapter 6 Buttons
// Selecting by index
if (!hobbiesCheckboxes.get(0).isSelected()) { // 0 for the
first checkbox
hobbiesCheckboxes.get(0).click();
}
// Deselecting by index
if (hobbiesCheckboxes.get(1).isSelected()) { // 1 for the
second checkbox
hobbiesCheckboxes.get(1).click();
}
146
Chapter 6 Buttons
147
Chapter 6 Buttons
Asserting Selection
When you assert that a checkbox is selected, you’re not simply checking a
box but confirming that your previous actions have successfully changed
the application’s state. This assertion is vital to your testing process,
ensuring the application behaves as expected when a user selects.
Asserting Deselection
Similar to the selection assertion, when you assert that a checkbox
is deselected, you verify that your action to remove a selection has
been effective. This step is crucial for tests that involve changing or
reconsidering choices, reflecting user interactions’ dynamic and often
unpredictable nature.
148
Chapter 6 Buttons
Assert.assertEquals("checkbox", musicCheckbox.
getAttribute("type"), "The element is not a CheckBox.");
SelectList
SelectList is an interactive web element allowing only one selection from
a drop-down list at a time. As you explore this fundamental component,
you’ll understand its importance in user interfaces and how it shapes your
approach to automated testing.
Before interacting with a SelectList element, you should become
familiar with the HTML structure, laying the foundation for your
subsequent actions.
The following is an HTML code example.
<!DOCTYPE html>
<html>
<head>
<title>Country Selection</title>
</head>
<body>
<h2>Select Your Country</h2>
<form>
<label for="country">Choose a country:</label>
<select id="country" name="country">
<option value="india">India</option>
<option value="usa">United States</option>
<option value="canada">Canada</option>
149
Chapter 6 Buttons
150
Chapter 6 Buttons
countrySelectList.selectByValue("india");
151
Chapter 6 Buttons
countrySelectList.selectByValue("default"); // Assuming
'default' is a neutral option
152
Chapter 6 Buttons
Assert.assertEquals("select", countrySelectElement.
getTagName(), "The element is not a SelectList.");
153
Chapter 6 Buttons
MultiSelectList
MultiSelectList is an essential web element that allows multiple selections,
unlike SelectList, where only one option can be chosen. It’s often used in
web forms to capture all applicable user preferences or data points.
Let’s consider an HTML form where users can select multiple
programming languages they’re proficient in.
<!DOCTYPE html>
<html>
<head>
<title>Language Proficiency</title>
</head>
<body>
<h2>Select Programming Languages</h2>
<form>
<label for="languages">Languages:</label>
<select id="languages" name="languages" multiple>
<option value="java">Java</option>
<option value="python">Python</option>
<option value="javascript">JavaScript</option>
<option value="csharp">C#</option>
</select>
</form>
</body>
</html>
154
Chapter 6 Buttons
155
Chapter 6 Buttons
Deselecting Options
Unlike SelectList, MultiSelectList allows individual options to be
deselected. The following code mentions each.
// Deselecting options
languagesSelectList.deselectByVisibleText("Java");
languagesSelectList.deselectByValue("python");
languagesSelectList.deselectByIndex(2); // Deselects
"JavaScript"
156
Chapter 6 Buttons
Asserting Deselections
Similarly, you need to verify that the options you intended to deselect are
no longer selected.
157
Chapter 6 Buttons
Testing Considerations
When testing buttons in web applications using Selenium, several
considerations ensure that your tests are robust, reliable, and reflective of
user interactions. The following is a structured approach to understanding
these testing considerations.
158
Chapter 6 Buttons
Button State
• Is the button enabled or disabled? Verify the button’s
state before interaction. Testing should confirm that the
button becomes enabled or disabled under the correct
circumstances.
Button Functionality
• Does the button perform the expected action?
Confirm that clicking the button triggers the expected
outcome, whether submitting a form, navigating to a
new page, or executing a script.
Button Interaction
• How does the button respond to clicks? Check the
response time and any immediate visual feedback (like
a spinner) indicating that the click has been registered
and an action is being processed.
159
Chapter 6 Buttons
Validation Post-Interaction
• Does the UI reflect the expected changes? After
clicking, validate that the UI updates to reflect any
changes. For example, a Submit button might change
to Submitted or update a page section.
Security Considerations
• Does clicking the button expose security flaws?
Ensure that interactions with the button don’t expose
vulnerabilities like SQL injection, especially for buttons
related to form submissions.
Performance Considerations
• Does the button respond quickly? Check the button’s
responsiveness and loading time, particularly for
buttons triggering complex backend operations.
160
Chapter 6 Buttons
Error Handling
• How does the button handle errors? Test how the
button behaves in error scenarios like failed form
submissions or unavailable resources. It should
handle errors gracefully and provide appropriate user
feedback.
Summary
This chapter traversed the landscape of interactive web elements crucial
for automated testing in Selenium. From the straightforward interaction
with buttons to the nuanced selections in a MultiSelectList element, a
wide range of elements and interactions have been covered. The journey
through this chapter has not only equipped you with the skills to perform
interactions. It also underscored the importance of validating both the
actions taken and the elements themselves.
A key takeaway from this chapter is the diversity of web elements
and the unique approach each requires for interaction and validation.
Understanding the intricacies of buttons, radio buttons, checkboxes, and
SelectList and MultiSelectList elements are crucial for any tester wanting
161
Chapter 6 Buttons
162
CHAPTER 7
iframes
An iframe is an HTML document embedded within a different HTML
document on a website. The iframe HTML element is often used to add
content from different sources. Particularly, iframe is used to embed
a document within another, isolating the embedded document from
the main page. This isolation helps to keep third-party content from
interfering with the main page’s DOM or JavaScript environment.
You can embed documents like videos (e.g., YouTube videos) and
PDFs. Third-party widgets, and many more. For test automation, knowing
that content inside an iframe exists in a separate document is crucial. This
means that web elements store the web elements.
Earlier HTML used frameset tags that were similar to the present
iframe tags, but it split the browser window into various sections with
different URLs. Each of these split windows was called a frame, displaying
different documents. Unlike iframes, frameset tags cannot be placed
anywhere on the page, and their lower flexibility, the use of framesets
declined. However, iframes have remained an integral part of modern web
development.
Let’s test iframes.
<!DOCTYPE html>
<html>
<head>
<title>Iframe Example</title>
</head>
<body>
<p>This is content of the main page.</p>
164
Chapter 7 iframes and Textboxes
Switching to an iframe
When you want to test a web element or functionality embedded within an
iframe, first, you need to locate and switch to that particular iframe.
165
Chapter 7 iframes and Textboxes
Switch Using ID
To access the iframe from the web page, use the ID attribute provided. The
following method switches control to the iframe.
driver.switchTo().frame(driver.findElement(By.id("iframe0")));
Once you switch to the iframe, you are confined to its web elements
and carry out operations like locating elements or interacting with them
until you switch back to the main page or another iframe.
166
Chapter 7 iframes and Textboxes
<!DOCTYPE html>
<html>
<head>
<title>Multiple Iframes</title>
</head>
<body>
<p>This is the main page content.</p>
167
Chapter 7 iframes and Textboxes
<!DOCTYPE html>
<html>
<head>
<title>Main Document</title>
</head>
<body>
<p>This is the content of the main page.</p>
168
Chapter 7 iframes and Textboxes
</iframe>
169
Chapter 7 iframes and Textboxes
driver.switchTo().frame(driver.findElement(By.
id(“firstLevelIframe”)));
driver.switchTo().frame(driver.findElement(By.
id(“secondLevelIframe”)));
170
Chapter 7 iframes and Textboxes
driver.switchTo().defaultContent();
Switch as an Element
When you are dealing with dynamic web pages, where iframes do not
render fixed attributes, or their position could change. In such cases,
you need to locate them as a web element, just like any other element on
the page. Once the element is located then you need to switch to it. The
following is simple HTML code for an iframe.
Since the iframe is dynamic, you need to locate iframe using the
class name.
171
Chapter 7 iframes and Textboxes
Now, after locating the iframe as a web element, you need to switch to
the iframe with switchTo() function.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
172
Chapter 7 iframes and Textboxes
Textboxes
In many web forms, textboxes are important elements that facilitate user
inputs from simple searches to intricate data entries. Next, let’s look at
various types of textboxes and how to locate them using Selenium.
173
Chapter 7 iframes and Textboxes
Figure 7-2 shows what the HTML looks like on a web page.
You need to locate the textbox on a web page before you can interact with
it. So, from the provided HTML, let’s locate a textbox using the ID attribute.
// Locating Textbox
WebElement bookTextbox = driver.findElement(By.id("nook_name"));
Now, you can interact with the textbox as you have located it.
174
Chapter 7 iframes and Textboxes
You have now seen how to locate a textbox and interactions like
inserting values into it and validating the text entered using Selenium,
ensuring that a web application accepts and displays the input provided by
the user as it is.
Multiline Textbox
In a multiline textbox, a user can span multiple lines as input that can be a
form of comments, descriptions, or any form of extended text.
You can create a multiline textbox using the <textarea> tag in
HTML. The following code represents a multiline textbox in HTML.
<h1>Multiline Textbox</h1>
<label for="description">Description:</label>
<textarea id="description" name="description" rows="4"
cols="50"></textarea>
175
Chapter 7 iframes and Textboxes
Figure 7-3 shows what the HTML looks like on a web page.
Inserting Values
Now, once you located the multiline element from the web page, you can
use sendKeys() method to provide input into the multiline textbox.
176
Chapter 7 iframes and Textboxes
This code snippet returns all the entered text from the multiline
textbox. It is a crucial procedure in testing scenarios like user feedback,
comment sections or any situation where you expect extended text input
by the user.
Summary
This chapter focused on two pivot elements in web automation iframes
and textboxes. It started by handling iframes, which are crucial for
interacting with embedded web content. It explored different strategies
for switching to iframes, such as using ID, name, index, and web element.
This knowledge is necessary as iframes often contain essential elements
that require special attention because of their separate DOM structure.
In addition, you learned the challenges while handling nested iframes,
which emphasized the importance of sequential navigation and discussed
explicit waits that enabled dynamic content loading, thus ensuring the
robustness and reliability of test cases.
The chapter also discussed the nuances of single-line textboxes and
multiline textboxes. Single-line textboxes used for entering fields, like
email addresses or names, were discussed in the context of efficiently
177
Chapter 7 iframes and Textboxes
178
CHAPTER 8
Assertions
This chapter explores the fundamental concepts, techniques, and best
practices related to assertions in Selenium. Assertions are trusted tools
for validating the behavior and functionality of web applications during
automated testing.
Starting with an introduction to assertions, you learn what assertions
are and why they hold immense importance in the Selenium testing
framework. You then dive into the two primary types of assertions: hard
and soft.
Next, you explore a range of assert methods provided by Selenium,
which are essential for comparing and verifying expected outcomes
against actual results in your tests. You also delve into handling assertion
failures, where you learn what happens when assertions fail and how to
gracefully manage these failures while ensuring comprehensive logging
and reporting.
Custom assertions are another focus of discussion. You discover
how to create custom assertion methods tailored to specific application
requirements. You explore extending assertion functionality to effectively
meet your unique testing needs.
Finally, you learn the common pitfalls and mistakes made when
working with assertions in Selenium, along with strategies to avoid false
positives and negatives.
180
Chapter 8 Assertions
//HTML Code
<!DOCTYPE html>
<html>
<head>
181
Chapter 8 Assertions
<title>Sample Page</title>
</head>
<body>
<h1>Welcome to Selenium Assertions Example</h1>
<p id="textElement">This is a sample paragraph.</p>
<button id="submitButton">Submit</button>
</body>
</html>
Let’s write Java code using Selenium to interact with this HTML page
and perform assertions.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
182
Chapter 8 Assertions
183
Chapter 8 Assertions
For the preceding HTML code, implement a soft assertion to it and see
the difference in the behavior of the test.
//Java Code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.asserts.SoftAssert;
184
Chapter 8 Assertions
185
Chapter 8 Assertions
186
Chapter 8 Assertions
import org.testng.Assert;
System.out.println("Test completed.");
}
}
import org.testng.Assert;
187
Chapter 8 Assertions
System.out.println("Test completed.");
}
}
import org.testng.Assert;
System.out.println("Test completed.");
}
}
188
Chapter 8 Assertions
import org.testng.Assert;
System.out.println("Test completed.");
}
}
import org.testng.Assert;
189
Chapter 8 Assertions
System.out.println("Test completed.");
}
}
import org.testng.Assert;
System.out.println("Test completed.");
}
}
190
Chapter 8 Assertions
191
Chapter 8 Assertions
Custom Assertions
Creating custom assertionmethods in Selenium allows you to tailor
your assertions to specific application requirements and extend the
functionality of built-in assertion libraries. This can be valuable when
performing complex or domain-specific checks in your automated tests.
Let’s explore custom assertions.
192
Chapter 8 Assertions
193
Chapter 8 Assertions
In your test script, you can utilize this custom assertion as follows.
import org.testng.Assert;
194
Chapter 8 Assertions
195
Chapter 8 Assertions
import org.testng.Assert;
import java.text.SimpleDateFormat;
import java.util.Date;
Now, you can use this custom assertion in your TestNG tests.
196
Chapter 8 Assertions
197
Chapter 8 Assertions
198
Chapter 8 Assertions
False Positives
False positives occur when assertions fail due to transient issues, such as
slow page loading or network delays, rather than actual defects.
To minimize false positives, use explicit waits with reasonable timeouts
to ensure that elements are fully loaded before performing assertions.
Implement retry mechanisms for flaky tests to reduce the impact of
transient failures.
False Negatives
False negatives happen when assertions pass even though defects exist
in the application or when assertions are not correctly written to validate
critical functionality.
To minimize false negatives, ensure that your assertions are written
accurately and comprehensively to cover all critical test scenarios.
Regularly review and update your assertions as the application evolves.
Baseline Data
Ensure that your test data is consistent and reliable. False positives or
negatives can occur if your test data is inconsistent or incomplete.
Maintain a stable and well-structured test data set. Validate and verify
the test data before executing tests to ensure its correctness.
199
Chapter 8 Assertions
Environment Stability
Ensure the stability of your test environment. Changes in the testing
environment can introduce false positives or negatives.
Monitor and control the test environment to minimize environmental
variations. Document and communicate any environmental changes that
might affect test results.
Effective Reporting
Implement a robust reporting mechanism that captures and distinguishes
between true failures, false positives, and false negatives.
Use reporting frameworks that allow you to classify and report different
types of test outcomes accurately. This helps in identifying and addressing
issues effectively.
200
Chapter 8 Assertions
201
Chapter 8 Assertions
Summary
This chapter took you further into the realm of Selenium assertions,
exploring their significance in web testing automation. You began by
understanding assertions and why they are indispensable tools within the
Selenium framework. It examined hard and soft assertions in validating
test outcomes.
Continuing your exploration, you delved into a comprehensive array
of assert methods provided by Selenium, including assertEqual and
assertTrue, equipping you with powerful tools for comparing and verifying
expected outcomes against actual results.
You also addressed handling assertion failures, gaining insights into
what transpires when assertions fail, and mastering techniques for graceful
error management. You emphasized the importance of comprehensive
logging and reporting for troubleshooting and analysis.
202
Chapter 8 Assertions
203
CHAPTER 9
Exceptions
In the rapidly changing environment of web automation with Selenium
WebDriver, even the most experienced test professionals discover
themselves in unfamiliar situations. With ever-changing web elements
or their nature, intermittent network issues and browser-specific quirks
in websites and web applications lead you to many unpredictable test
scenarios. However, meticulous script writing and thorough perpetration
for most situations of the unpredictable nature of live environments
require a greater understanding of one fundamental concept of exception.
As a Selenium test expert, recognizing the distinctions of exceptions is
not only advantageous but also necessary. This chapter offers insights into
the labyrinth of exclusions pertaining to Selenium. Becoming proficient
in exceptions ensures that your automation scripts aren’t just functional
but adaptive, resilient, and robust, transforming potential roadblocks into
mere stepping stones on the path to comprehensive web testing.
For example, when you want to locate a web element that is not
available on the web page, then the Selenium WebDriver raises the
NoSuchElementException error. This exception helps you understand,
troubleshoot, and potentially the error in your automation script because
it provides specific information regarding the nature of the problem
encountered.
Next, let’s discuss various exceptions in Selenium, each representing
a unique issue or challenge raised during test automation. To develop
robust and reliable Selenium test scripts, you learn about handling these
exceptions, ensuring smooth test execution with more accuracy.
Types of Exceptions
This section discusses various exceptions encountered during the
execution of automated tests. The exceptions are divided into different
categories to simplify the cause of their occurrence.
Connection Exception
When there is unexpected loss or obstruction in the communication
process with WebDriver or browser you get the following exception.
206
Chapter 9 Exceptions
ElementNotInteractableException is thrown
when the web element is not interactable, but an
interaction attempt is made.
driver.findElement(By.id("nonInteractableElement")).
sendKeys("Test"); // This will throw
ElementNotInteractableException.
207
Chapter 9 Exceptions
driver.findElement(By.xpath("//
unselectableOption")).click(); // This will throw
ElementNotSelectableException.
State-based Exceptions
These exceptions correspond to the state of web elements or web pages
during the execution of tests.
driver.findElement(By.id("hiddenElement")).click();
// This will throw ElementNotVisibleException.
208
Chapter 9 Exceptions
driver.findElement(By.id("disabledInput")).
sendKeys("Test"); // This will throw
InvalidElementStateException.
209
Chapter 9 Exceptions
Navigation Issues
These exceptions are raised during navigation between pages or while
context switching.
driver.switchTo().window("nonExistentWindowHandle");
// This will throw NoSuchWindowException.
210
Chapter 9 Exceptions
driver.findElement(By.id("nonExistentElement"));
// This will throw NoSuchElementException.
InvalidSelectorException is similar to
NoSuchElementException. Here the specified
selector is not valid or changed dynamically.
driver.findElement(By.xpath("///invalidXPath"));
// This will throw InvalidSelectorException.
driver.switchTo().frame("nonExistentFrame");
// This will throw NoSuchFrameException.
((JavascriptExecutor) driver).executeScript("invalidJavaScript(
)"); // This will throw JavascriptException.
211
Chapter 9 Exceptions
Session Exceptions
When a session expires or is invalid, Selenium WebDriver gives you
InvalidSessionIdException.
driver.get("http://example.com");
// Suppose session terminates here for some reason
driver.getTitle(); // This will throw
InvalidSessionIdException.
driver.manage().timeouts().implicitlyWait(-5, TimeUnit.
SECONDS); // This will throw InvalidArgumentException
due to negative time.
212
Chapter 9 Exceptions
driver.get("http://example.com");
// Suppose an unexpected alert pops up here
driver.findElement(By.id("someElement")).click(); // This will
throw UnexpectedAlertPresentException if not handled.
Screenshot Exception
This exception occurs when you provide instructions to Selenium
WebDriver to take a screenshot, but it cannot get it.
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// This might throw ScreenshotException if the screenshot
capture fails.
213
Chapter 9 Exceptions
driver.get("https://insecure-certificate-website.com");
// This might throw InsecureCertificateException.
driver.manage().ime().activateEngine("IME_ENGINE"); //
This will throw ImeNotAvailableException if IME support
is not available.
driver.manage().ime().activateEngine("Invalid_
IME_ENGINE"); // This will throw
ImeActivationFailedException.
214
Chapter 9 Exceptions
215
Chapter 9 Exceptions
driver.switchTo().window("nonExistentWindowHandle");
// This will throw NoSuchWindowException.
driver.findElement(By.id("hiddenElement")).click();
// This will throw ElementNotInteractableException.
216
Chapter 9 Exceptions
driver.findElement(By.id("divElement")).setSelected();
// This can throw ElementNotSelectableException.
driver.findElement(By.id("invisibleElement")).click();
// This will throw ElementNotVisibleException.
driver.findElement(By.id("disabledTextBox")).
sendKeys("text"); // This will throw
InvalidElementStateException.
217
Chapter 9 Exceptions
• 400 – BadRequest
• 401 – Unauthorized
• 403 – Forbidden
• 405 – MethodNotAllowed
• 409 – Conflict
• 500 – InternalServerError
ErrorHandler.UnknownServerException occurs
when you don’t have the trace of the error given
by the server, then this exception is raised. It is a
response to all unrecognized server errors.
Other Exceptions
Some other exceptions are not commonly observed during the execution
of the test.
218
Chapter 9 Exceptions
219
Chapter 9 Exceptions
Handling Exceptions
When an exception occurs in an automated test script, the default flow of
execution is stopped, resulting in an error. This error may be a runtime or
WebDriver exception. Selenium supports try-catch methods that are used
in Java to handle exceptions.
220
Chapter 9 Exceptions
try {
WebElement element = driver.findElement(By.
id("optionalElement"));
element.click();
} catch (NoSuchElementException e) {
System.out.println("Element not found: " + e.getMessage());
}
Note The code you have written in the try-catch block is also called
protected code.
try {
WebDriverWait wait = new WebDriverWait(driver, Duration.
ofSeconds(10));
221
Chapter 9 Exceptions
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.id("elementId")));
} catch (TimeoutException e) {
System.out.println("Element did not appear within 10
seconds: " + e.getMessage());
} finally {
driver.quit();
}
try {
WebElement element = driver.findElement(By.
id("dynamicElement"));
element.click();
} catch (StaleElementReferenceException e) {
System.out.println("Stale Element Reference: "
+ e.getMessage());
222
Chapter 9 Exceptions
try {
WebElement element = driver.findElement
(By.id("someElement"));
element.click();
} catch (NoSuchElementException e) {
System.out.println("Element not found: " + e.getMessage());
} catch (StaleElementReferenceException e) {
System.out.println("Stale Element Reference: "
+ e.getMessage());
} catch (TimeoutException e) {
System.out.println("Operation timed out: "
+ e.getMessage());
223
Chapter 9 Exceptions
} finally {
driver.quit();
}
This example used multiple exceptions like element not found, stale
element, and a timeout exception written in each catch block separately.
You have also used the finally block when WebDriver is closed, retaining a
clean test environment. This can be used in cases where multiple failures
may occur during testing.
try {
// Selenium interactions
} catch (Exception e) {
throw new CustomSeleniumException("Custom message", e);
} finally {
// Cleanup actions
}
224
Chapter 9 Exceptions
225
Chapter 9 Exceptions
226
Chapter 9 Exceptions
Summary
In this chapter, there are two important sections: one is exceptions and
their types, second is handling them. The first section discussed what
exceptions are and the causes of their occurrence. It also classified and
listed all the exceptions that may be encountered during the execution of
automated testing.
The second section focused on handling exceptions. It explored
various try-catch blocks using Java to handle single to multiple Selenium
exceptions. By applying these techniques, you ensure that your tests won’t
fail abruptly upon encountering an error, greatly enhancing the stability
and reliability of your tests. It also discussed best practices for handling
various exceptions.
227
CHAPTER 10
Wait Strategies
in Selenium Test
Automation
In Selenium test automation, managing the timing for web elements to
load and become interactable is a key challenge. Waits are essential for
handling the asynchronous behavior of web applications, ensuring that
elements are ready for interaction before a test proceeds. This chapter
focuses on the different types of waits in Selenium—implicit, explicit, and
fluent—and provides an overview of their applications and best practices.
Waits are crucial in avoiding flaky tests, which result from attempting
to interact with elements that are not yet available. Let’s explore how to
effectively use these waits to ensure robust and reliable automated tests.
The chapter covers the distinct features of each wait type, guiding you
through their appropriate use cases and helping you understand which
wait to choose for specific testing scenarios.
This concise overview is designed to enhance your understanding
of waits in Selenium, ensuring the tools and knowledge to efficiently
handle element synchronization challenges in test automation projects.
Let’s delve into these vital components of Selenium to optimize the
performance and reliability of your automated tests.
Synchronization
The state of the test script and the web application are synchronized with
the help of waits. This synchronization is crucial for the robustness and
reliability of a test script.
Reducing Flakiness
Test scripts without having proper waits can be flaky as they might pass
sometimes and fail at other times. The use of waits makes a test script
more consistent.
230
Chapter 10 Wait Strategies in Selenium Test Automation
Wait Types
In Selenium, waits are an essential feature for handling the asynchronous
behavior of web applications. There are mainly three types of waits:
implicit, explicit, and fluent. Let’s delve into each type in more depth.
Implicit Waits
When using implicit waits, you specify a time frame that makes the
WebDriver wait for an element to be available in the DOM (Document
Object Model) to avoid throwing of NoSuchElementException. This wait is
set to last the entire WebDriver object’s life. The default time for the wait is
zero seconds.
You set a wait time period in the test script so that the defined web
element will be available on the given page. During this time period, the
WebDriver will not proceed to execute further commands and will avoid
throwing an exception. The WebDriver is made to wait for the specified wait
time. If the web element is not available or visible within this time frame, it
raises a NoSuchElementException. When the web element is loaded and
found after the wait time is set, the WebDriver executes future test commands
in the script. The concept of implicit waits is inspired by the Watir tool.
231
Chapter 10 Wait Strategies in Selenium Test Automation
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
232
Chapter 10 Wait Strategies in Selenium Test Automation
Note An implicit wait is used for web elements that are not
instantly available.
Explicit Waits
As you have seen, implicit waits make the WebDriver wait for a specific
time. The web element is made available on the web page after the
specified time has elapsed. However, implicit waits cannot be used for all
web elements because the time taken to execute the test case is longer.
This leads to the use of explicit wait, which is an improved version of
implicit wait.
The explicit wait defines the ExpectedConditions along with the
WebDriverWait. The WebDriver is made to wait for a specified condition
that must be satisfied within the time frame set. When the condition is met
or the time has elapsed, the test script begins to proceed with subsequent
actions defined in the script.
233
Chapter 10 Wait Strategies in Selenium Test Automation
The major difference between implicit and explicit waits is that the
explicit wait proceeds with code execution when the defined condition is
met and does not wait for the time to complete. The explicit wait provides
more precise control and prevents indefinite waiting time, ensuring
smooth continuation of the test script.
Example code for using explicit wait:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
dynamicElement.sendKeys("Text to type");
driver.quit();
}
}
234
Chapter 10 Wait Strategies in Selenium Test Automation
The preceding code defines a condition that waits until the specified
web element is visible on the page. The time to wait is set to 10 seconds.
When you cannot predict the wait times of a web element, you use
explicit wait, hence providing a better way to handle the asynchronous
nature of modern-day web applications. Expected conditions are
important in defining the explicit waits. These are known conditions that
are discussed in the next topic.
235
Chapter 10 Wait Strategies in Selenium Test Automation
236
Chapter 10 Wait Strategies in Selenium Test Automation
237
Chapter 10 Wait Strategies in Selenium Test Automation
Exception: ElementNotVisibleException
238
Chapter 10 Wait Strategies in Selenium Test Automation
• visibilityOfAllElements(List<WebElement>
elements) waits for all elements in the provided list to
be visible. This is useful when you have already located
the elements and need to ensure they are all visible
before proceeding.
239
Chapter 10 Wait Strategies in Selenium Test Automation
• textToBePresentInElementLocated(By locator,
String text) waits for a specific text to be present in a
particular element.
• textToBePresentInElement(WebElement element,
String text) waits for a specific text to be present in the
provided WebElement.
240
Chapter 10 Wait Strategies in Selenium Test Automation
241
Chapter 10 Wait Strategies in Selenium Test Automation
• frameToBeAvailableAndSwitchToIt(String
frameLocator) waits for the frame to be available and
then switches to it.
• frameToBeAvailableAndSwitchToIt(By locator)
waits for a frame to be available and switches to it. This
variation uses a locator to identify the frame.
242
Chapter 10 Wait Strategies in Selenium Test Automation
• frameToBeAvailableAndSwitchToIt(int
frameLocator) waits for a frame at the given index to
be available and switches to it.
243
Chapter 10 Wait Strategies in Selenium Test Automation
244
Chapter 10 Wait Strategies in Selenium Test Automation
245
Chapter 10 Wait Strategies in Selenium Test Automation
• attributeToBeNotEmpty(WebElement element,
String attribute) waits for a specific attribute of an
element to be non-empty.
246
Chapter 10 Wait Strategies in Selenium Test Automation
247
Chapter 10 Wait Strategies in Selenium Test Automation
F luent Waits
The fluent wait in Selenium is a type of explicit wait that provides advanced
wait capabilities. It allows you to set the maximum amount of time to wait
for a condition, as well as the frequency with which to check the condition.
Additionally, you can ignore specific types of exceptions while waiting,
which makes it more flexible than the standard WebDriverWait.
Let’s say you want to wait for an element to become visible, but you
expect that it might take some time, and you don’t want to check too
frequently. The following shows how you can set up a fluent wait.
248
Chapter 10 Wait Strategies in Selenium Test Automation
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.NoSuchElementException;
import java.time.Duration;
import java.util.function.Function;
try {
// Navigate to a URL
driver.get("http://example.com"); // Replace with
your target URL
249
Chapter 10 Wait Strategies in Selenium Test Automation
} finally {
// Close the browser
driver.quit();
}
}
}
250
Chapter 10 Wait Strategies in Selenium Test Automation
Implicit Wait
• How it works: An implicit wait tells WebDriver to
wait for a certain amount of time before throwing a
NoSuchElementException if an element is not found.
This wait is set globally for the lifetime of the WebDriver
instance and is applied to all element searches.
251
Chapter 10 Wait Strategies in Selenium Test Automation
driver.manage().timeouts().implicitlyWait(10, TimeUnit.
SECONDS);
E xplicit Wait
• How it works: An explicit wait instructs the WebDriver
to wait for certain conditions (Expected Conditions)
or the maximum time exceeded before throwing an
ElementNotVisibleException. Explicit wait is specific
to a particular element and its condition.
• When to use it: Use it when you need to wait for
specific conditions on certain elements, like waiting for
an element to become clickable, visible, or to have a
specific text.
252
Chapter 10 Wait Strategies in Selenium Test Automation
F luent Wait
• How it works: A fluent wait allows for more complex
configurations of wait conditions. You can set the
maximum amount of time to wait for a condition, the
frequency with which to check the condition, and
ignore certain types of exceptions during the wait.
• When to use it: It is ideal for more complex scenarios
where you need to customize the polling frequency or
ignore specific types of exceptions during the wait (e.g.,
waiting for Ajax elements).
253
Chapter 10 Wait Strategies in Selenium Test Automation
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function<WebDriver,
WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("someId"));
}
});
254
Chapter 10 Wait Strategies in Selenium Test Automation
255
Chapter 10 Wait Strategies in Selenium Test Automation
This table highlights the key differences to help you make an informed
choice about which type of wait to use in Selenium testing scenarios.
256
Chapter 10 Wait Strategies in Selenium Test Automation
257
Chapter 10 Wait Strategies in Selenium Test Automation
By following these best practices, you can create Selenium tests that
are not only accurate in terms of functionality testing but also efficient and
reliable in their execution.
258
Chapter 10 Wait Strategies in Selenium Test Automation
Summary
This chapter delved into the importance of correctly handling element
synchronization in web applications. The chapter introduced and
thoroughly explored the three primary types of waits in Selenium—
implicit, explicit, and fluent—each catering to different scenarios in test
automation. An implicit wait, a global setting, is simple but less flexible,
suitable for uniform wait conditions across all element searches. An
explicit wait, in contrast, offers more precision, allowing waits for specific
conditions on particular elements, making it ideal for more complex
synchronization needs. The most advanced, the fluent wait, provides
the highest level of control, including customizable polling intervals and
exception handling, perfect for handling highly dynamic content and Ajax
elements.
This chapter emphasized best practices, such as preferring explicit
over implicit waits for greater accuracy, avoiding fixed sleeps for efficiency,
and setting appropriate timeouts to balance test speed and reliability.
The chapter also offered practical implementation guidance with code
examples, equipping readers with the knowledge to choose and apply the
appropriate wait type effectively. This comprehensive overview aimed
to empower readers with the skills necessary to create stable, reliable,
and efficient automated tests capable of handling the asynchronous and
unpredictable nature of modern web applications.
259
CHAPTER 11
elements. This section will cover its syntax, usage, practical benefits, and
limitations within various testing scenarios. Through this exploration, you
will gain the knowledge to architect robust, scalable, and maintainable
automated testing solutions.
<html>
<head><title>Login Page</title></head>
<body>
<form id="loginForm">
<input type="text" id="username" />
<input type="password" id="password" />
<input type="submit" id="loginButton" />
</form>
</body>
</html>
In the traditional approach, tests are written directly in the test script.
Locators and operations on elements are all in the same method. The
following is what a simple login test might look like.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.Test;
262
Chapter 11 Page Object Model (POM)
What Is POM?
The Page Object Model (POM) is a design pattern that encourages a
modular and maintainable approach to Selenium test scripting. In POM,
web pages are represented as classes, and elements within those pages
are represented as variables on the class. Interactions with these elements
are encapsulated as methods within the class. This abstraction results in a
cleaner, more understandable codebase.
Decoding DOM
The Document Object Model (DOM) represents the structure of a web
page as a tree of objects. Selenium interacts with this DOM to locate
elements and execute actions. Understanding the DOM is critical as
263
Chapter 11 Page Object Model (POM)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
264
Chapter 11 Page Object Model (POM)
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.Test;
265
Chapter 11 Page Object Model (POM)
driver.quit();
}
}
In a real-world scenario, you would have a Java file for each page in
your application, and potentially multiple test files organized around the
features or functionalities they cover.
266
Chapter 11 Page Object Model (POM)
267
Chapter 11 Page Object Model (POM)
• Code organization
• Maintenance
• Readability
268
Chapter 11 Page Object Model (POM)
• Scalability
269
Chapter 11 Page Object Model (POM)
270
Chapter 11 Page Object Model (POM)
• Avoid duplication.
271
Chapter 11 Page Object Model (POM)
Factory Page
Page Factory is a Selenium support class that allows you to initialize web
elements in a more streamlined way. It uses annotations to identify web
elements and reduces the amount of boilerplate code you need to write.
Page Factory is essentially a way to initialize the elements of page
objects in a cleaner, more concise manner; instead of using driver.
findElement(), you use annotations provided by Page Factory to declare
elements on a page.
272
Chapter 11 Page Object Model (POM)
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
@FindBy(id = "password")
private WebElement passwordField;
@FindBy(id = "loginButton")
private WebElement loginButton;
273
Chapter 11 Page Object Model (POM)
DashboardPage.java: Representing
the Dashboard Page
When users log in successfully, a page having dashboard is represented.
You see a similar structure to LoginPage.java with elements and methods
relevant to the dashboard.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
274
Chapter 11 Page Object Model (POM)
PageFactory.initElements(driver, this);
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.Test;
275
Chapter 11 Page Object Model (POM)
driver.quit();
}
}
This structure ensures that test code remains clean, maintainable, and
easy to understand. The Page Factory pattern significantly simplifies the
code by reducing boilerplate and improving readability, allowing you to
focus more on the test logic itself.
276
Chapter 11 Page Object Model (POM)
277
Chapter 11 Page Object Model (POM)
• Initialization
• Readability
• Element initialization
• Maintenance
278
Chapter 11 Page Object Model (POM)
• Performance
279
Chapter 11 Page Object Model (POM)
Limitations of POM
• Increased initial effort: Setting up POM requires a
considerable initial effort. For small projects or simple
test cases, this overhead might not be justified.
280
Chapter 11 Page Object Model (POM)
281
Chapter 11 Page Object Model (POM)
282
Chapter 11 Page Object Model (POM)
Summary
This chapter began by exploring the conventional way of Selenium testing,
where test scripts directly interact with web elements using locators and
actions within the same method. While straightforward, this approach
often leads to code duplication and maintenance challenges as the test
suite grows in complexity.
It then transitioned to the Page Object Model (POM) and Page Factory.
POM enhances maintainability and reusability by encapsulating web page
structures and behaviors within separate classes. Page Factory builds on
283
Chapter 11 Page Object Model (POM)
284
CHAPTER 12
TestNG
This chapter explains how TestNG can significantly enhance your
Selenium testing practices, whether you are just starting in automation
testing or are an experienced professional looking to leverage the synergies
of these powerful tools.
The chapter explores the essentials of TestNG, including its key
features like annotations, assertions, and data-driven testing, and how
they complement Selenium’s functionalities. Through practical examples,
make these concepts accessible to beginners while delving into advanced
features like parallel execution and dependency testing for the more
seasoned testers.
This journey demonstrates how TestNG can strategically enhance
Selenium tests, improving their efficiency, scalability, and maintainability.
You’ll encounter real-world scenarios and code examples showing how to
address complex testing challenges effectively.
By the end of this chapter, you will be equipped with the knowledge
to integrate TestNG with Selenium, enhancing both the effectiveness of
your tests and recognizing how crucial it is to adhere to the best standards
in the dynamic field of automated testing with the overall quality of
your automation projects. Let’s dive into this journey, unlocking the full
potential of automated testing with TestNG and Selenium.
JUnit Overview
JUnit, a long-standing staple in the Java testing world, is a framework
primarily designed for unit testing. It’s a tool many have relied on for years,
evolving with the changing dynamics of software development.
As of the latest iteration, JUnit 5, or Jupiter, represents a significant
advancement from its predecessors, introducing more flexibility and
features for modern testing needs.
286
Chapter 12 TestNG
TestNG Features
TestNG, evolving from the foundations laid by JUnit, extends these
capabilities with features that cater to more complex testing needs.
287
Chapter 12 TestNG
288
Chapter 12 TestNG
Table 12-1 shows why TestNG is often the preferred choice, particularly
for complex and large-scale testing scenarios, such as those encountered
in Selenium-based web application automation testing.
289
Chapter 12 TestNG
290
Chapter 12 TestNG
IntelliJ IDEA
1. Open IntelliJ IDEA and proceed to your project
workspace.
291
Chapter 12 TestNG
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.x.x</version> <!-- Use the latest
version for optimal features -->
292
Chapter 12 TestNG
<scope>test</scope>
</dependency>
</dependencies>
293
Chapter 12 TestNG
import org.testng.annotations.Test;
294
Chapter 12 TestNG
@Test(priority = 1)
public void firstTest() {
// This test will run first due to its higher
priority (lower number)
System.out.println("First test method");
}
@Test(enabled = false)
public void disabledTest() {
// This test will not be executed
System.out.println("This test method is
disabled.");
}
295
Chapter 12 TestNG
@Test(dependsOnMethods = {"firstTest"})
public void dependentTest() {
// This test runs only after ‘firstTest’ has
successfully completed
System.out.println("Runs after firstTest");
}
@Test(groups = {"sanity"})
public void sanityTest() {
// Part of the 'sanity' group of tests
System.out.println("Part of sanity tests");
}
296
Chapter 12 TestNG
@Test(dataProvider = "dataMethod")
public void dataDrivenTest(String input) {
// This test will run multiple times with
different inputs
System.out.println("Data driven test with input: "
+ input);
}
@DataProvider
public Object[][] dataMethod() {
return new Object[][] {{"data1"}, {"data2"}};
}
Use this method when testing for conditions that are expected to throw
exceptions.
297
Chapter 12 TestNG
@BeforeSuite
public void globalSetup() {
// Code for global setup
System.out.println("Global setup before any suite is
executed.");
}
@AfterSuite
public void globalTeardown() {
// Code for global teardown
System.out.println("Global teardown after all suites are
executed.");
}
298
Chapter 12 TestNG
@BeforeTest/@AfterTest
Methods annotated with these run before and after the test methods
inside the <test> tag in the TestNG XML. They’re suitable for preparing
and cleaning up conditions common to all tests within a particular
<test> group.
@BeforeTest
public void setupTest() {
// Setup code for a group of tests
System.out.println("Setting up before a group of tests.");
}
@AfterTest
public void teardownTest() {
// Teardown code for a group of tests
System.out.println("Cleaning up after a group of tests.");
}
@BeforeClass/@AfterClass
These annotations are used to run methods before the first method of
the current class is invoked and after all the test methods of the current
class have been run. Ideal for setup and cleanup activities specific to a
particular class.
@BeforeClass
public void setupClass() {
// Code executed before any method of the class runs
System.out.println("Setup actions before any test method of
this class.");
}
@AfterClass
299
Chapter 12 TestNG
@BeforeMethod/@AfterMethod
The annotate methods are used to run them before and after each
test method. They’re perfect for preparing and cleaning up the test
environment for every individual test method.
@BeforeMethod
public void setupMethod() {
// Code to run before each test method
System.out.println("Running before each test method.");
}
@AfterMethod
public void teardownMethod() {
// Code to run after each test method
System.out.println("Running after each test method.");
}
300
Chapter 12 TestNG
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>Welcome to the Test Page</h1>
<button id="testButton">Click Me</button>
</body>
</html>
301
Chapter 12 TestNG
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver", "path/to/
chromedriver");
driver = new ChromeDriver();
}
@Test
public void testButtonPresence() {
// Replace the path with the absolute path to your
example.html
driver.get("file:///path/to/example.html");
302
Chapter 12 TestNG
@AfterMethod
public void tearDown() {
driver.quit();
}
}
• Setting up WebDriver
• @BeforeMethod - setUp
• @Test - testButtonPresence
• @AfterMethod - tearDown
Finally, run the test by executing as a TestNG test from IDE. Open the
specified HTML file in Chrome, verify the presence of the button, and then
close the browser.
303
Chapter 12 TestNG
TestNG Assertions
TestNG provides a set of assertion methods in the Assert class, which are
crucial for verifying the correctness of test conditions. These assertions
are key in validating the expected and actual results in TestNG tests. Let’s
explore some commonly used assertions: assertEquals, assertTrue,
assertFalse, assertNull, and assertNotNull.
@Test
public void testEquality() {
String expected = "TestNG";
String actual = "Test" + "NG";
Assert.assertEquals(actual, expected, "Strings are
not equal");
}
304
Chapter 12 TestNG
@Test
public void testCondition() {
boolean condition = 5 > 3;
Assert.assertTrue(condition, "Condition is
not true");
}
@Test
public void testFalseCondition() {
boolean condition = 3 > 5;
Assert.assertFalse(condition, "Condition is not
false");
}
@Test
public void testNull() {
Object myObject = null;
Assert.assertNull(myObject, "Object is not null");
}
305
Chapter 12 TestNG
@Test
public void testNotNull() {
Object myObject = new Object();
Assert.assertNotNull(myObject, "Object is null");
}
This assertion verifies that myObject is not null. If myObject is null, the
test fails with an “Object is null” message.
An earlier chapter explored the capabilities of Selenium in performing
assertions directly within web automation scripts. As you integrate the
TestNG framework with Selenium, it’s important to remember that these
Selenium-based assertions can also serve as effective assertion methods
within your TestNG tests. Selenium, primarily a tool for web interactions,
offers a unique approach to validations directly from the web elements.
Combining this with TestNG’s structured testing approach creates a
comprehensive and robust testing environment.
In essence, while leveraging TestNG for its structured approach to
writing tests, you should not overlook the practicality and directness
of Selenium’s assertions. They complement the TestNG framework,
particularly when direct interaction with web elements is crucial. This
integration enriches test automation capabilities, ensuring a more
thorough and reliable validation process in web application testing
endeavors.
306
Chapter 12 TestNG
import org.testng.annotations.DataProvider;
@DataProvider(name = "loginData")
public Object[][] provideLoginData() {
return new Object[][] {
{ "[email protected]", "password1" },
{ "[email protected]", "password2" },
307
Chapter 12 TestNG
import org.testng.Assert;
import org.testng.annotations.Test;
@Test(dataProvider = "loginData")
public void testLogin(String email, String password) {
System.out.println("Attempting login with: " + email +
" / " + password);
// Imagine a function that attempts login and returns
a boolean
boolean result = attemptLogin(email, password);
Assert.assertTrue(result, "Login should be
successful");
}
308
Chapter 12 TestNG
When this test is run, TestNG invokes testLogin three times, once for
each set of credentials provided by the provideLoginData method. This
demonstrates how parameterized tests can efficiently test multiple data
sets with a single piece of test code.
309
Chapter 12 TestNG
features of TestNG that address these needs. This section delves into how
to utilize testng.xml for custom test execution and to enable parallel
testing, enhancing test suite manageability and efficiency.
310
Chapter 12 TestNG
package com.example.tests;
import org.testng.annotations.Test;
@Test
public void testMethodOne() {
// Simulate test execution time
Thread.sleep(2000); // Just for demonstration
System.out.println("Test Method One.");
}
311
Chapter 12 TestNG
@Test
public void testMethodTwo() {
Thread.sleep(2000); // Just for demonstration
System.out.println("Test Method Two.");
}
312
Chapter 12 TestNG
313
Chapter 12 TestNG
By following these best practices, you can harness the full potential of
TestNG and Selenium in your testing projects, ensuring that automated
tests are not only effective and reliable but also adaptable to the changing
needs of your applications.
314
Chapter 12 TestNG
Summary
This final chapter presented a detailed exploration of integrating TestNG
with Selenium, a combination that elevates automated web testing to
new heights. It began with an overview of TestNG, highlighting its robust
features like parallel execution, flexible configurations, and powerful
annotations. These features complement Selenium’s web automation
capabilities and are crucial for efficient test management.
Key topics included a deep dive into TestNG annotations, essential
for structuring automated tests, and examining TestNG’s assertions for
validating test outcomes. The chapter emphasized the significance of
parameterized testing using TestNG’s @DataProvider, illustrating how
to run the same test with varying data sets—a core aspect of data-driven
testing.
The synergy between TestNG and Selenium was the focal point,
showcasing how their integration enhances the creation of robust,
scalable, and maintainable test suites. Practical examples provided a
hands-on perspective, marrying theory with application. The advanced
TestNG configurations and the pivotal role of parallel execution showcase
how to leverage testng.xml for customized test suites and significantly
reduce execution times by running tests concurrently, optimizing the
efficiency and scalability of Selenium automated testing.
In conclusion, this final chapter served as a comprehensive guide
to harnessing the combined power of TestNG and Selenium, offering
invaluable insights for testers aiming to refine their automated testing
strategies. The best practices discussed herein form a crucial part of this
learning, ensuring that testers are equipped with technical knowledge
and the methodologies to apply these tools effectively in their testing
environments.
315
Index
A mistakes, 197
reporting, 192, 200
Action chains, 47, 76
simple and specific, 201
@After annotations, 313
soft, 183, 185
@AfterMehod-tearDown, 303
test cases, 202
Ajax calls, 258
testing, 197, 198
Annotations, 272, 273, 281, 286,
use, 200
288, 289, 294, 298–301, 307,
assertNotEqual method, 187
309, 313
assertNotNull method, 306
Apache Maven, 293
assertNull method, 189, 305, 306
assertEquals method, 187, 304
assertTrue method, 188, 305
assertFalse method, 189, 305
Attributes, 80–81, 85, 91, 103, 127,
AssertionError exception, 191
166, 215, 294
Assertions, 286
Automated test scripts, 180, 220,
automated tests, 180, 181
262, 263
baseline data, 199
custom (see Custom assertions)
definition, 180 B
environment stability, 200
@BeforeMethod, 303, 313
error messages, 201
Browser commands, 31
fails, 191
Browser position, 39, 41, 210
false negatives, 199
Browser size
false positives, 199
coordinates, 39, 40
handle assertion failures,
full-screen mode, 38
191, 192
maximizing, 37
hard, 181–183, 185
minimize() method, 37
logging, 192
window size, 38, 41
methods, 187–190
318
INDEX
319
INDEX
320
INDEX
321
INDEX
322
INDEX
323
INDEX
P Q
Page Factory quit command, 35, 36
applications, 282
dashboard, 274
implement, 279 R
limitations, 281 Radio buttons, 138–139
login page, 273, 274 deselect, 141
page objects, 275 index values, 140
vs. POM, 278, 279 label, 140
setting up, 272 locating elements, 139
setup, 276 unique ID, 140
steps to implement, 277 validating, 141
use, 277 verify the state, 141
PageFactory.initElements Realistic timeout settings, 257
method, 274 Reusability, 79, 268, 283
Page Object Model (POM), 261, 263
applications, 282
best practices, 269–272 S
components, 266, 267 Scalability, 79, 269, 312
Java files, 266 SelectList element
limitations, 280 all options, 152
vs. traditional, 268, 269 assert, 153
Parallel execution, 288, 314 assert element type, 153
Parameterized testing default/neutral option, 152
@DataProvider annotation, deselecting, 152
307, 308 HTML code, 149, 150
@DataProvider test method, interact, 150
308, 309 locate/select by index, 151
definition, 307 locate/select by value, 151
Parameterized tests, 287 retrieving options, 151
Partial link text locator, 86, 87 validating
Project object model (POM), 293 selections, 153
provideLoginData method, 308, 309 visible text, 151
324
INDEX
325
INDEX
326
Index
327
Index
328