Download (Ebook) Python Testing with Selenium: Learn to Implement Different Testing Techniques Using the Selenium WebDriver by Sujay Raghavendra ISBN 9781484262481, 9781484262498, 1484262484, 1484262492 ebook All Chapters PDF
Download (Ebook) Python Testing with Selenium: Learn to Implement Different Testing Techniques Using the Selenium WebDriver by Sujay Raghavendra ISBN 9781484262481, 9781484262498, 1484262484, 1484262492 ebook All Chapters PDF
com
OR CLICK HERE
DOWLOAD EBOOK
(Ebook) Selenium WebDriver Recipes in C#: Practical Testing Solutions for Selenium
WebDriver by Courtney Zhan ISBN 9798868800238, 8868800233
https://ebooknice.com/product/selenium-webdriver-recipes-in-c-practical-testing-
solutions-for-selenium-webdriver-55667478
ebooknice.com
(Ebook) Selenium Essentials : get to grips with automated web testing with the
amazing power of Selenium WebDriver by Sams, Prashanth ISBN 9781784394332,
9781784397685, 1784394335, 1784397687
https://ebooknice.com/product/selenium-essentials-get-to-grips-with-automated-
web-testing-with-the-amazing-power-of-selenium-webdriver-6657726
ebooknice.com
(Ebook) Selenium WebDriver Recipes in Python: The problem solving guide to Selenium
WebDriver in Python by Zhimin Zhan ISBN 9781514256572, 1514256576
https://ebooknice.com/product/selenium-webdriver-recipes-in-python-the-problem-
solving-guide-to-selenium-webdriver-in-python-24667606
ebooknice.com
(Ebook) Selenium WebDriver Recipes in C#: Practical Testing Solutions for Selenium
WebDriver by Courtney Zhan, (edited by) Zhimin Zhan ISBN 9798868800221,
9798868800238, 8868800225, 8868800233, B0CJ2DBVNG, B0CKSCRP5V
https://ebooknice.com/product/selenium-webdriver-recipes-in-c-practical-testing-
solutions-for-selenium-webdriver-54554740
ebooknice.com
(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason; Viles, James ISBN
9781459699816, 9781743365571, 9781925268492, 1459699815, 1743365578, 1925268497
https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374
ebooknice.com
(Ebook) Learning Selenium Testing Tools with Python by Gundecha, Unmesh ISBN
9781783983506, 1783983507
https://ebooknice.com/product/learning-selenium-testing-tools-with-
python-55668144
ebooknice.com
(Ebook) Selenium Testing Tools Cookbook, 2nd Edition: Over 90 recipes to help you
build and run automated tests for your web applications with Selenium WebDriver by
Unmesh Gundecha ISBN 9781784392512, 1784392510
https://ebooknice.com/product/selenium-testing-tools-cookbook-2nd-edition-
over-90-recipes-to-help-you-build-and-run-automated-tests-for-your-web-
applications-with-selenium-webdriver-5474490
ebooknice.com
(Ebook) Test-Driven Development with Python. Obey the Testing Goat. Using Django,
Selenium, and JavaScript by Harry J. W. Percival ISBN 9781491958674, 9781491958704,
1491958677, 1491958707
https://ebooknice.com/product/test-driven-development-with-python-obey-the-
testing-goat-using-django-selenium-and-javascript-6712714
ebooknice.com
(Ebook) Selenium with Python - A Beginner’s Guide: Get started with Selenium using
Python as a programming language by Sharma, Pallavi R ISBN 9789389328813, 9389328810
https://ebooknice.com/product/selenium-with-python-a-beginners-guide-get-
started-with-selenium-using-python-as-a-programming-language-37465562
ebooknice.com
Python Testing
with Selenium
Learn to Implement Different
Testing Techniques Using the
Selenium WebDriver
—
Sujay Raghavendra
Python Testing
with Selenium
Learn to Implement Different
Testing Techniques Using the
Selenium WebDriver
Sujay Raghavendra
Python Testing with Selenium
Sujay Raghavendra
Dharwad, Karnataka, India
Introduction������������������������������������������������������������������������������������������������������������xxi
v
Table of Contents
Flexible������������������������������������������������������������������������������������������������������������������������������������ 8
Hardware Resources��������������������������������������������������������������������������������������������������������������� 8
Simulation������������������������������������������������������������������������������������������������������������������������������� 8
Other Testing Tools������������������������������������������������������������������������������������������������������������������ 8
Integrating Python with Selenium����������������������������������������������������������������������������������������������� 10
Summary������������������������������������������������������������������������������������������������������������������������������������ 14
vii
Table of Contents
Locator Issues����������������������������������������������������������������������������������������������������������������������������� 66
Attribute Change�������������������������������������������������������������������������������������������������������������������� 67
No Web Element�������������������������������������������������������������������������������������������������������������������� 67
Web Element Not Visible�������������������������������������������������������������������������������������������������������� 67
Test Case Mismatch�������������������������������������������������������������������������������������������������������������� 68
iframe Web Element�������������������������������������������������������������������������������������������������������������� 68
Summary������������������������������������������������������������������������������������������������������������������������������������ 68
Chapter 5: Navigation��������������������������������������������������������������������������������������������� 69
Hyperlinks����������������������������������������������������������������������������������������������������������������������������������� 69
Testing Hyperlinks����������������������������������������������������������������������������������������������������������������������� 70
Hyperlink by ID���������������������������������������������������������������������������������������������������������������������������� 71
Hyperlink by Text������������������������������������������������������������������������������������������������������������������������� 71
Hyperlink by Partial Link������������������������������������������������������������������������������������������������������������� 71
Hyperlink by XPath���������������������������������������������������������������������������������������������������������������������� 71
Nth Hyperlink������������������������������������������������������������������������������������������������������������������������������ 71
Return All Hyperlinks������������������������������������������������������������������������������������������������������������������ 72
Check for a Valid Hyperlink��������������������������������������������������������������������������������������������������������� 72
Check for Broken Images������������������������������������������������������������������������������������������������������������ 73
Data Attributes Hyperlink������������������������������������������������������������������������������������������������������������ 74
Summary������������������������������������������������������������������������������������������������������������������������������������ 74
Chapter 6: Buttons�������������������������������������������������������������������������������������������������� 75
Default Button����������������������������������������������������������������������������������������������������������������������������� 75
Select������������������������������������������������������������������������������������������������������������������������������������ 76
Submit/Form Button�������������������������������������������������������������������������������������������������������������������� 78
Select by Visible Text������������������������������������������������������������������������������������������������������������� 79
Image as a Button����������������������������������������������������������������������������������������������������������������������� 79
Assert for Button������������������������������������������������������������������������������������������������������������������� 80
Radio Buttons������������������������������������������������������������������������������������������������������������������������������ 81
Select Radio Buttons������������������������������������������������������������������������������������������������������������� 82
Unselect/Clear Selection������������������������������������������������������������������������������������������������������� 83
viii
Table of Contents
ix
Table of Contents
x
Table of Contents
xi
Table of Contents
ScreenshotException����������������������������������������������������������������������������������������������������������� 123
StaleElementReferenceException��������������������������������������������������������������������������������������� 123
TimeoutException���������������������������������������������������������������������������������������������������������������� 123
UnableToSetCookieException���������������������������������������������������������������������������������������������� 123
UnexpectedAlertPresentException�������������������������������������������������������������������������������������� 123
UnexpectedTagNameException������������������������������������������������������������������������������������������� 124
UnknownMethodException�������������������������������������������������������������������������������������������������� 124
WebDriverException������������������������������������������������������������������������������������������������������������ 124
Exception Handling������������������������������������������������������������������������������������������������������������������� 124
Timeout Exception��������������������������������������������������������������������������������������������������������������� 125
Element Not Found�������������������������������������������������������������������������������������������������������������� 125
Stale Element���������������������������������������������������������������������������������������������������������������������� 126
Summary���������������������������������������������������������������������������������������������������������������������������������� 127
xii
Table of Contents
Elements.py������������������������������������������������������������������������������������������������������������������������� 147
Locators.py�������������������������������������������������������������������������������������������������������������������������� 148
Advantages of Page Objects Model������������������������������������������������������������������������������������������ 149
Limitations of the Page Object Model��������������������������������������������������������������������������������������� 150
Summary���������������������������������������������������������������������������������������������������������������������������������� 150
Index��������������������������������������������������������������������������������������������������������������������� 165
xiii
About the Author
Sujay Raghavendra works primarily in data science,
machine/deep learning, and artificial intelligence.
He is currently the Executive Director of Raghavendra
Training & Consultancy (RTC), a startup company based
in Dharwad, Karnataka, India. He co-founded RTC with
Sumedh Raghavendra, his brother, in 2014. He plans and
evaluates new technology projects in research and product
development. His projects include analyzing a pap smear
filter for microscopic medical images, thermal heat sensing
in hospitals, OCR for handwritten characters, satellite
image analysis, and network automation for maps, forecasting models, text analytics,
predictions, and more.
Raghavendra is also a consultant on building research centers for technical
universities and colleges. His recent interests include automating test cases through
machine learning. He has published numerous research articles in international journals
and has been on reviewer committees for various journals and conferences.
xv
About the Technical Reviewer
Supreeth Chandrashekhar is a seasoned software
technology professional associated with Philips Healthcare,
Bangalore. He has more than eight years of architect
experience in building/running highly robust, massively
scalable, and extremely secure systems, as well as setting
up and managing mid-sized tech teams. He is extremely
passionate about building great products that exceed
customer expectations.
xvii
Acknowledgments
The person behind who I am today is my mother, Mrs. Indumati Raghavendra.
xix
Introduction
This book focuses on how to implement testing techniques using Selenium WebDriver
with the Python programming language. This quick reference provides simple,
functional test cases with a syntax-based approach for Selenium WebDriver.
You’ll begin by reviewing the basics of Selenium WebDriver and its architectural
design history. Next, you move on to the configuration and installation of the Selenium
library in various web browsers, including the basic commands needed to start test
scripts. You’ll review keyboard and mouse action commands for testing user interactions
on a web page and see how hyperlinks are tested.
The book examines various web elements using the eight different locators provided
by Selenium to help you choose the one best suited to your needs. All Python scripts are
real ready-to-test examples that are explained thoroughly in problem statements. You’ll
use different Python design patterns to automate test scripts that can be incorporated
with Selenium.
Python Testing with Selenium teaches the expertise to write your own test cases.
xxi
CHAPTER 1
Introduction to Selenium
Before Selenium, testing the functionality of a web application was done manually,
which took many hours. The testing tended to rely on different scenarios. Each
scenario was considered a test case to enact the behavior of the web app before its
implementation. These test cases were deployed on various browsers to affirm any
issues in the source code.
It requires a dedicated team of testers to check all test cases. Accuracy and time are
major constraints in web development, which has led to automated test cases that can
be used in different web applications without changing the source code. Selenium was
developed to automate test cases.
This first chapter of the book offers a complete overview of Selenium and its core
architectural design. The subtopics explain using Selenium and compares it to other
testing tools in the domain. Later in the chapter, integrating Python with Selenium is
explained. Let’s start with a brief history and description of the Selenium tool and the
reasons to use it.
What Is Selenium?
Selenium came into existence in 2004 at ThoughtWorks to test a web application named
Time and Expenses by Jason Huggins. The tool was developed to test the front-end
behavior of an application in various browsers. The tool was popular and open source.
The increase in demand for automated testing led to the development of several versions
of Selenium over the years, which are discussed next.
1
© Sujay Raghavendra 2021
S. Raghavendra, Python Testing with Selenium, https://doi.org/10.1007/978-1-4842-6249-8_1
Chapter 1 Introduction to Selenium
6HOHQLXP
7RROV
6HOHQLXP
6HOHQLXP
Any one (or more) of these Selenium tools can be used by an organization; the
choice depends on the test environment’s requirements. The first tool developed by
ThoughtWorks was Selenium Core in 2004. It allowed the tester to write their own code/
script to automate front-end interactions like keyboard or mouse activities. These
activities were similar to a user interacting with the application.
In web application security, there is a policy that grants a test script permission to
access data in web pages from the same origin; this policy is called a same-host policy.
A same-host policy only allows a test case to access pages within the same domain. For
example, a test script can access multiple pages within www.apress.com, such as www.
apress.com/in/python and www.apress.com/in/about, because of the same-host
policy; however, this policy does not allow access to pages from different sites, such as
https://google.com or https://wikipedia.org.
Due to the same-host policy, access to code elements is denied or blocked when
using external scripts. To avoid this complication, Huggins and Paul Hammant
developed a server component that enables you to test a web app with a test script that
makes the browser believe that both are from the same source. This core Selenium was
eventually known as Driven Selenium or Selenium B.
2
Chapter 1 Introduction to Selenium
Selenium IDE
In 2006, a completely integrated development environment (IDE) was developed by
Shinya Kasatani for testers. It was in the form of a plugin for Mozilla Firefox and Google
Chrome web browsers. Selenium IDE used functional tests in a live environment. The
feature of this IDE involved tests to record/replay and debug/edit, which was known as
Selenium Recorder. The recording scripts were stored in a test script called a Selenese
script in Selenium. The test scripts were written in languages like Java, Ruby, JavaScript,
and PHP. The IDE also provided data retrieval options for test cases performed for web
apps. Selenium IDE is currently actively maintained by Kantu and Katalon.
Selenium Grid
It was difficult to test web applications on the new technologically-enabled devices
that were emerging on the market. To solve this issue, in 2008, Philippe Hanrigou at
ThoughtWorks developed a grid architecture that allowed you to test apps on any
number of remote devices via a browser, which became Selenium Grid. It reduced the
time to test scripts on any number of remote devices because it was done in parallel.
The test command tests on remote devices via a browser. There are two components
necessary to execute a test script on a remote device: a hub/server and a node/remote
device.
The hub/server gets requests from the web driver client that allows access and routes
it to remote drivers. These drivers are registered on remote devices. A node/remote
device has a local OS and browser. The web driver is the part of a browser that performs
tests. When defining a script, you need to name a remote device, platform, browser, and
so forth, to locate a specific node, and then test scripts are executed for that node.
3
Chapter 1 Introduction to Selenium
S
elenium WebDriver
Selenium WebDriver is a widely used tool. The advancement in Selenium RC resulted in
the development of Selenium WebDriver. The commands in WebDriver are accepted via
a client API and are sent to different browsers, such as Mozilla Firefox, Apple Safari, and
so forth. Almost all browsers support Selenium WebDriver. Each browser has a specific
driver associated with it, which are listed in Table 1-1.
Each driver listed in the table is maintained to support automation for its respective
browser. Another browser driver, HTMLUnitDriver, stimulates browsers using a headless
browser (HtmlUnit).
Selenium WebDriver allows you to start a web browser directly and manages it by
executing commands. To avoid security conflicts and issues, WebDriver uses native OS
functionality instead of browser-based JavaScript commands. The Selenium WebDriver
version of WebDriver focused on the interface. The later versions are Selenium 2.0 and
Selenium 3.0.
S
elenium 2.0
In 2007, Simon Stewart at ThoughtWorks created Selenium 2.0, which enables
automation on almost all browsers. This version has fewer calls and allows testers/
developers to create their own domain-specific language (DSL). The Watir web driver,
which is implemented in Ruby, is one of the best examples of DSL.
4
Chapter 1 Introduction to Selenium
S
elenium 3.0
Developers Simon Stewart and David Burns made a draft to standardize Selenium,
which was fully accepted and became a W3C standard protocol in 2019 when it became
known as Selenium 3.0.
This completes the overview of Selenium and its evolution through the years; now
let’s consider the Selenium architecture before diving into the test cases, which are
covered in the upcoming chapters of this book.
C
lient Library
A client library is a package of languages supported by Selenium. The core languages
supported by Selenium are Python, Java, JavaScript, Ruby, and C# (https://selenium.
dev/downloads/). Languages like Perl, PHP, GO, DART, R, and Haskell are maintained
and developed by a third party (https://selenium.dev/thirdparty/). Selenium does
not officially support these third-party language bindings.
5
Chapter 1 Introduction to Selenium
W
eb Drivers
Each browser has a web driver associated with it (refer to Table 1-1 for more
information). These web drivers are responsible for executing the commands received
from the client library. The execution of these commands is done in a web browser,
which communicates via HTTP.
W
eb Browsers
The commands received from an HTTP response from web drivers are executed. Like
a client library, core and third-party browsers may be used. The browsers supported by
Selenium are Firefox, Internet Explorer, Safari, Opera, Chrome, and Edge. These browsers
can be run on any operating system, such as Windows, macOS, or Linux. There are third-
party web drivers developed for these web browsers, but they are not recommended.
W
hy Selenium?
Having examined the Selenium WebDriver architecture, let’s further enhance your
understanding of Selenium.
6
Chapter 1 Introduction to Selenium
Due to the availability of testing tools on the market, a question arises: Why use
Selenium for testing? There are several answers to this question, but the primary reason
to use Selenium is that it is open source (i.e., freely available to use). The benefits of
using Selenium as a testing tool are discussed next.
Open Source
Selenium is open source and can be used at no cost. There is a large community of
developers/testers who continuously maintain and support it. You can modify, integrate,
or extend Selenium for any testing environment because the code is open source.
Platforms
Selenium WebDriver is cross-platform, which means that it has the flexibility to
automate test cases in any operating system, such as Windows, macOS, Unix, and Linux.
These test cases written in one OS can be used in any other.
Language Support
Another reason that Selenium has a large community is that it supports multiple
programming languages and scripts, like Python, Java, Ruby, Perl, PHP, JavaScript,
Groovy, GO, DART, and so forth. This is supported by the Selenium WebDriver language
bindings/client library.
Browser
As with platform flexibility, Selenium WebDriver supports almost all browsers. Mozilla
Firefox, Google Chrome, Safari, Opera, Internet Explorer, and Edge are supported; they
are the most widely used around the globe.
Reuse
Once written, the test scripts can be used as needed across any browser or OS. There are
no restrictions on using a test script multiple times.
7
Chapter 1 Introduction to Selenium
E asy Implementation
The implementation of Selenium WebDriver depends on the environment and the script
used by the developer/tester or organization. This variety is due to the vast number of
potential OS and browser combinations. You can develop a customized web driver or
framework to implement in a specific testing environment.
F lexible
Refactoring or regrouping in a test script enables you to reduce the amount of code
duplication and other complications. Selenium provides developers/testers with this
flexibility in a test script.
H
ardware Resources
Unlike other testing tools—like UFT, CompleteTest, Katalon Studio, and so forth—
Selenium requires fewer hardware resources.
S
imulation
Selenium’s simulations create the real-time behavior of a mouse and keyboard. This
helps to test advanced events like drag, drop, click, hold, scroll, and so forth, with a
mouse and similar keypress events made on a keyboard.
The various reasons discussed so far should satisfy reasons to use Selenium as a
testing tool but comparing it with the other tools available ensures that Selenium is best
at automating tests. This is discussed next.
8
Chapter 1 Introduction to Selenium
9
Chapter 1 Introduction to Selenium
Now that you can see that Selenium is the best-suited automated tool for testing web
applications, let’s look at why Python is the best language to integrate with Selenium.
• There is immense library support for Python due to the large number
of communities behind it, who maintain and update it regularly.
Selenium WebDriver can be extended to build more advanced test
cases by automating as per the needs of the organization or individual.
Listings 1-1 through 1-5 present a simple program for the languages that are
supported by Selenium WebDriver. The program opens a browser, visits a specified URL,
and searches a query in it. The program first imports the necessary Selenium WebDriver
libraries. WebDriver then opens the Mozilla Firefox browser and specifies www.apress.
com as the URL to visit. Next, it locates the search bar element in the Apress site. After
locating the element, this book’s title, Python Testing with Selenium, is entered as a query
in the search bar and submitted. The submitted query provides a new web page with the
books associated with the query.
Listings 1-1 through 1-5 illustrate how easy Python is to implement test cases with
Selenium web driver when compared to other programming languages, such as Java, C#,
Ruby, and PHP.
11
Chapter 1 Introduction to Selenium
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
//Closing browser
browser.close();
}
}
//Importing Libraries in C#
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
class Apress
{
staticvoid Main()
{
// Opening Browser
WebDriver browser = newFirefoxDriver();
12
Discovering Diverse Content Through
Random Scribd Documents
The Project Gutenberg eBook of Suomalaisia
näyttelijöitä
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: Finnish
Kirj.
AARNI KOUTA
Esisanat.
Ida Aalberg.
Kaarola Avellan.
Emelie Stenberg.
Aurora Aspegren.
August Aspegren.
Taavi Pesonen.
Axel Ahlberg.
Adolf Lindfors.
Knut Weckman.
Katri Rautio.
Aleksis Rautio.
Iisakki Lattu.
Olga Poppius.
Kaarle Halme.
Esisana.
Ihaillessamme jotakin suurta ja jo täyden kasvuvoimansa
saavuttanutta puuta ei liene monenkaan meidän mieleemme
juohtunut ne eri kehityskaudet ja -muodot, jotka tämä puu on elänyt
ja taistellut muodostuessaan pienestä, tuskin maankamaraa
ylemmästä idusta siksi korkeaksi kasviksi, joka majaamme varjostaa.
Me näemme ihastuksella vain puun, joka työntää latvansa taivasta
kohti, ojentaa oksansa ilman tuulien tuuditeltaviksi, mutta emme
ajattele pientä tainta, joka maan uumenista kerran pyrki ja pääsi
päivän valoon, juurrutti ja levitti juurensa tahdon rantavoimalla
syvälle mullan mustaan helmaan, mistä sitten imi elämisen voiman ja
tarmon. Ja lähemmin tarkastaessamme on tässä alkutilassa ja
alkukamppailussa yhtä paljon mieltäkiinnittävää ja ihastuttavaa kuin
myöhemmässä kypsyyden kaudessakin. Vieläpä tuon taimen
kehitysilmaisuja tutkistellessamme voimme nähdä viittauksia
vastaisuuteen, edellytyksiä, edellyttäviä luonteenominaisuuksia ja
hentoja yhdyssiteitä tulevaisuuteen.
Helsingissä v. 1910.
Aarni Kouta.
Ida Aalberg.
Kaarola Avellan.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com