36,59 €
Real-world examples of cross-browser, mobile, and data-driven testing with all the latest features of Selenium WebDriver 3
Key Features
Book Description
Selenium WebDriver is an open source automation tool implemented through a browser-specific driver, which sends commands to a browser and retrieves results. The latest version of Selenium 3 brings with it a lot of new features that change the way you use and setup Selenium WebDriver. This book covers all those features along with the source code, including a demo website that allows you to work with an HMTL5 application and other examples throughout the book.
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations available. You will learn to strategize and handle rich web UI using advanced WebDriver API along with real-time challenges faced in WebDriver and solutions to handle them. You will discover different types and domains of testing such as cross-browser testing, load testing, and mobile testing with Selenium. Finally, you will also be introduced to data-driven testing using TestNG to create your own automation framework.
By the end of this book, you will be able to select any web application and automate it the way you want.
What you will learn
Who this book is for
Selenium WebDriver 3 Practical Guide is for software quality assurance/testing professionals, software project managers, or software developers interested in using Selenium for testing their applications. Prior programming experience in Java is necessary.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 270
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Kunal ChaudhariAcquisition Editor:Divya PoojariContent Development Editor:Deepti ThoreTechnical Editor:Cymon PereiraCopy Editor:Safis EditingProject Coordinator:Kinjal BariProofreader: Safis EditingIndexer:Mariammal ChettiyarGraphics:Jisha ChirayilProduction Coordinator: Arvindkumar Gupta
First published: January 2014
Second edition: July 2018
Production reference: 1300718
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78899-976-2
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Unmesh Gundecha has over 16 years, experience in Agile software development, test automation, and DevOps methodologies. He is an Agile, open source, and DevOps evangelist with extensive experience in a diverse set of tools and technologies. He has extensive hands-on experience in building sustainable and repeatable test automation solutions for web and mobile platforms, APIs, and CLI apps with continuous integration and delivery pipelines, using best-of-breed open source and commercial tools to do so. He is the author of Selenium Testing Tools Cookbook and Learning Selenium Testing Toolswith Python, both by Packt Publishing.
Pallavi Sharma is a founder of 5 Elements Learning. She has 12 years professional experience. She has worked in varied roles as a product/project manager, in presales team, marketing team, and test automation coach in the software testing domain. Being an avid learner, she also likes to keep herself up to date with the latest trends and technologies. She is a firm believer that there is no shortcut to success.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Selenium WebDriver 3 Practical Guide Second Edition
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Installing Java
Installing Eclipse
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Introducing WebDriver and WebElements
Selenium Testing Tools
Selenium WebDriver 
Selenium Server
Selenium IDE
Differences between Selenium 2 and Selenium 3 
Handling the browser  
 Having better APIs
Having developer support and advanced functionalities
Testing Mobile Apps with Appium
Setting up a project in Eclipse with Maven and TestNG using Java
WebElements
Locating WebElements using WebDriver 
The findElement method
The findElements method
Inspecting Elements with Developer Tools
Inspecting pages and elements with Mozilla Firefox
Inspecting pages and elements in Google Chrome with Developer Tools
Using the By locating mechanism
The By.id() method
The By.name() method
The By.className() method
The By.linkText() method
The By.partialLinkText() method
The By.tagName() method
The By.xpath() method
The By.cssSelector() method
Interacting with WebElements
Getting element properties and attributes
The getAttribute() method
The getText() method
The getCssValue() method
The getLocation() method
The getSize() method
The getTagName() method
Performing actions on WebElements
The sendKeys() method
The clear() method
The submit() method
Checking the WebElement state
 The isDisplayed() method
The isEnabled() method
The isSelected() method
Summary
Questions
Further information
Different Available WebDrivers
Firefox Driver
Using GeckoDriver
Using Headless Mode
Understanding the Firefox profile
Adding the extension to Firefox
Storing and retrieving a profile
Dealing with Firefox preferences
Setting preferences
Understanding frozen preferences
Chrome Driver
Using Headless Mode
Using Mobile Emulation for testing mobile web applications
Adding ChromeExtensions
InternetExplorerDriver
Writing your first test script for the IE browser
Understanding IEDriver capabilities
Edge Driver
Writing your first test script for the Edge browser
Safari Driver
Writing your first test script for the Safari browser
Summary
Questions
Further information
Using Java 8 Features with Selenium
Introducing Java 8 Stream API
Stream.filter()
Stream.sort()
Stream.map()
Stream.collect()
Stream.min() and Stream.max()
Stream.count()
Using Stream API with Selenium WebDriver
Filtering and counting WebElements 
Filtering element attributes
Using the Map function to get the text value from elements
Filtering and performing actions on WebElements
Summary
Questions
Further information
Exploring the Features of WebDriver
Taking screenshots
Locating target windows and Frames
Switching among windows
Switching between frames            
Handling alerts   
Exploring Navigate
Waiting for WebElements to load
Implicit wait time
Explicit wait time
Handling cookies
Summary
Questions
Further information
Exploring Advanced Interactions of WebDriver
Understanding the build and perform actions
Learning mouse based interactions
The moveByOffset action
The click at current location action
The click on a WebElement action
The click and hold at current location action
The click and hold a WebElement action
The release at current location action
The release on another WebElement action 
The moveToElement action
The dragAndDropBy action
The dragAndDrop action
The double click at current location action
The double click on WebElement action
The context click on WebElement action
The context click at current location action
Learning keyboard-based interactions
The keyDown and keyUp actions
The sendKeys method
Summary
Questions
Further information
Understanding WebDriver Events
Introducing the eventFiringWebDriver and eventListener classes 
Creating an instance of EventListener 
Implementing WebDriverEventListener 
Extending AbstractWebDriverEventListener
Creating a WebDriver instance 
Creating EventFiringWebDriver and EventListener instances
Registering EventListener with EventFiringWebDriver
Executing and verifying the events
Registering multiple EventListeners
Exploring different WebDriver event listeners
Listening for WebElement value changes
Listening for the clicked WebElement
Listening for a WebElement search event
Listening for browser back-navigation
Listening for browser forward-navigation
Listening for browser NavigateTo events
Listening for script execution
Listening for an exception
Unregistering EventListener with EventFiringWebDriver
Performing accessibility testing
Capturing page-performance metrics
Summary
Questions
Further information
Exploring RemoteWebDriver
Introducing RemoteWebDriver
Understanding Selenium Standalone Server
Downloading Selenium Standalone Server
Running the server
Understanding the RemoteWebDriver client
Converting an existing test script to use the RemoteWebDriver server
Using RemoteWebDriver for Firefox
Using RemoteWebDriver for Internet Explorer
Understanding the JSON wire protocol                                                            
Summary
Questions
Further information
Setting up Selenium Grid
Exploring Selenium Grid
Understanding the hub
Understanding the node
Modifying the existing test script to use Selenium Grid
Requesting for non-registered capabilities
Queuing up the request if the node is busy 
Dealing with two nodes with matching capabilities                                              
Configuring Selenium Grid
Specifying node-configuration parameters
Setting supported browsers by a node
Setting node timeouts
Setting the limit on browser instances
Reregistering the node automatically
Setting node health-check times
Unregistering an unavailable node
Setting the browser timeout
Hub-configuration parameters
Waiting for a match of the desired capability
Customized CapabilityMatcher
WaitTimeout for a new session
Different ways to specify the configuration
Using cloud-based grids for cross-browser testing
Summary
Questions
Further information
The PageObject Pattern
Creating test cases for our WordPress blog
Test case 1 – adding a new post to our WordPress blog
Test case 2 – deleting a post from our WordPress blog
Test case 3 – counting the number of posts on our WordPress blog
What is the PageObject pattern?
Using the @FindBy annotation
Understanding PageFactory
Good practices for the PageObjects design
Think of a web page as a services provider
Always look for implied services
Using PageObjects within a PageObject
The AddNewPost PageObject
The AllPostsPage PageObject
Think of methods in PageObjects as services and not as user actions
Identifying some WebElements on the fly
Keeping the page-specific details off the test script
Understanding loadable components
Working on an end-to-end example of WordPress
Looking at all the PageObjects
The AdminLoginPage PageObject
The AllPostsPage PageObject
The AddNewPostPage PageObject
The EditPostPage PageObject
The DeletePostPage PageObject
Looking at the test cases
Adding a new post
Editing a post
Deleting a post
Counting posts
Summary
Questions
Further information
Mobile Testing on iOS and Android using Appium
Different forms of mobile applications
Available software tools
Automating iOS and Android tests using Appium
Automating iOS Application tests
Automating Android application tests
Prerequisites for Appium
Setting up Xcode
Setting up Android SDK
Creating the Android Emulator
Installing Appium 
Automating for iOS
Automating for Android
Using Device Cloud to run tests on Real Devices
Summary
Questions
Further information
Data-Driven Testing with TestNG
Overview of data-driven testing
Parameterizing Tests using suite parameters
Parameterizing Tests with a Data Provider
Reading data from a CSV file
Reading data from an Excel file
Summary
Questions
Further information
Assessments
Chapter 1 
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Other Books You May Enjoy
Leave a review - let other readers know what you think
This book is about Selenium WebDriver, that is, a browser automation tool used by software developers and QA engineers to test their web application on different web browsers. This book can be used as a reference for your day-to-day usage of WebDriver.
Selenium is a set of tools for automating browsers. It is largely used for testing applications, but its usages are not limited only to testing. It can also be used for screen scraping and automating repetitive tasks in a browser window. Selenium supports automation on all the major browsers, including Firefox, Internet Explorer, Google Chrome, Safari, and Opera. Selenium WebDriver is now a part of W3C standards, and it is supported by major browser vendors.
If you are a quality assurance/testing professional, test engineer, software developer, or web application developer looking to create automated test suites for your web applications, this is the perfect guide for you! As a prerequisite, this book expects you to have a basic understanding of Java programming although any previous knowledge of WebDriver or Selenium is not needed. By the end of this book, you will have acquired a comprehensive knowledge of WebDriver, which will help you in writing your automation tests.
Chapter 1, Introducing WebDriver and WebElements, will start off with an overview of Selenium and the features. Then, we quickly jump into WebDriver by describing how it perceives a web page. We will also look at what a WebDriver's WebElement is. Then, we talk about locating WebElements on a web page and performing some basic actions on them.
Chapter 2, Working with Browser Drivers, will talk about various implementations of WebDriver, such as FirefoxDriver, IEDriver, and ChromeDriver. We will configure browser options to run tests in headless mode, mobile emulation, and use custom profiles. With WebDriver becoming a W3C specification, all major browser vendors now support WebDriver natively in the browser.
Chapter 3, Using Java 8 Features along with Selenium, will talk about prominent Java 8 features such as Streams API and Lambda expressions for processing the list of WebElements. The Stream API and Lambda expression help in applying functional programming style to create readable and fluent tests.
Chapter 4, Exploring the Features of WebDriver, will talk about some advanced features of WebDriver, such as taking screenshots of web pages, executing JavaScript, handling cookies, and handling Windows and Frames.
Chapter 5, Exploring Advanced Interaction API, will dive deeply into more advanced actions that WebDriver can perform on the WebElements of a web page, such as the dragging and dropping of elements from one frame of a page to another and right/context-clicking on WebElements. We're sure you will find this chapter interesting to read.
Chapter 6, Understanding WebDriver Events, will deal with the event-handling aspect of WebDriver. To state a few, events can be a value change on a WebElement, a browser back-navigation invocation, script execution completion, and so on. We will use these events to run accessibility and performance checks.
Chapter 7, Exploring RemoteWebDriver, will talk about using RemoteWebDriver and Selenium Standalone Server for executing tests on remote machines from your machine. You can use the RemoteWebDriver class to communicate with the Selenium Standalone Server on a remote machine to run commands on the desired browser installed on the remote machine. One of its popular use cases is browser compatibility testing.
Chapter 8, Setting up Selenium Grid, will talk about one important and interesting feature of Selenium named Selenium Grid. Using this, you can execute automated tests on a distributed computer network using Selenium Grid. We will configure a Hub and Nodes for cross-browser testing. This also enables running tests in parallel and in a distributed architecture.
Chapter 9, The PageObject Pattern, will talk about a well-known design pattern named the PageObject pattern. This is a proven pattern that will give you a better handle on your automation framework and scenarios for better maintainability.
Chapter 10, Mobile Testing on iOS and Android Using Appium, will take you through how WebDriver can be used to automate your test scripts for iOS and Android platform using Appium.
Chapter 11, Data-Driven Testing with TestNG, will talk about using the data-driven testing technique with TestNG. Using the data-driven testing approach, we can reuse a test with multiple sets of test data to gain additional coverage.
The reader is expected to have a basic idea of programming, preferably using Java because we take the reader through several features of WebDriver using code examples. The following software is required for the book:
Oracle JDK8
Eclipse IDE
Maven 3
Google Chrome
Mozilla Firefox
Internet Explorer or Edge (on Windows)
Apple Safari
Appium
In this book, all the code examples that we show covering various features of WebDriver will be in Java. To follow these examples and write your own code, you need the Java Development Kit installed on your computer. The latest version of JDK can be downloaded from the following link:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
This book is a practical guide that expects the user to write and execute WebDriver examples. For this, it would be handy to install a Java IDE. The Eclipse IDE is a popular choice in Java user community. The Eclipse IDE can be downloaded from https://www.eclipse.org/downloads/.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Selenium-WebDriver-3-Practical-Guide-Second-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download from https://www.packtpub.com/sites/default/files/downloads/SeleniumWebDriver3PracticalGuideSecondEdition_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
In this chapter, we will look briefly into Selenium, its various components, such as Appium, and proceed to the basic components of a web page, including the various types of WebElements. We will learn different ways to locate WebElements on a web page and execute various user actions on them. We will cover the following topics in this chapter:
Various components of Selenium Testing Tools
Setting up a project in Eclipse with Maven and TestNG
Locating WebElements on a Web Page
Actions that can be taken on the WebElements
Selenium is a set of widely popular tools used to automate browsers. It is largely used to test applications, but its usages are not limited to testing. It can also be used to perform screen scraping and automate repetitive tasks in a browser window. Selenium supports automation on all the major browsers, including Google Chrome, Mozilla Firefox, Microsoft Internet Explorer and Edge, Apple Safari, and Opera. Selenium 3.0 is now a part of W3C standards and is supported by major browser vendors.
Selenium 3.0 offers three important tools, Selenium WebDriver, Selenium Server, and Selenium IDE. Each of these tools provides features to create, debug, and run tests on supported browsers and operating systems. Let's explore each of them in detail.
Selenium WebDriver is the successor of Selenium RC (Remote Control), which has been officially deprecated. Selenium WebDriver accepts commands using the JSON-Wire protocol (also called Client API) and sends them to a browser launched by the specific driver class (such as ChromeDriver, FirefoxDriver, or IEDriver). This is implemented through a browser-specific browser driver. It works with the following sequence:
The driver listens to the commands from Selenium
It converts these commands into the browser's native API
The driver takes the result of native commands and sends the result back to Selenium:
We can use Selenium WebDriver to do the following:
Create robust, browser-based regression automation
Scale and distribute scripts across many browsers and platforms
Create scripts in your favourite programming language
Selenium WebDriver offers a collection of language-specific bindings (client libraries) to drive a browser. WebDriver comes with a better set of APIs that meet the expectations of most developers by being similar to object-oriented programming in its implementation. WebDriver is being actively developed over a period of time, and you can see many advanced interactions with the web as well as mobile applications.
Selenium Server allows us to run tests on browser instances running on remote machines and in parallel, thus spreading a load of testing across several machines. We can create a Selenium Grid, where one server runs as the Hub, managing a pool of Nodes. We can configure our tests to connect to the Hub, which then obtains a node that is free and matches the browser we need to run the tests. The hub has a list of nodes that provide access to browser instances, and lets tests use these instances similarly to a load balancer. Selenium Grid enables us to execute tests in parallel on multiple machines by managing different types of browsers, their versions, and operating system configurations centrally.
Selenium IDE is a Firefox add-on that allows users to record, edit, debug, and play back tests captured in the Selenese format, which was introduced in the Selenium Core version. It also provides us with the ability to convert these tests into the Selenium RC or Selenium WebDriver format. We can use Selenium IDE to do the following:
Create quick and simple scripts using record and replay, or use them in exploratory testing
Create scripts to aid in automation-aided exploratory testing
Create macros to perform repetitive tasks on Web pages
Before we dive further into Selenium 3, let's understand the differences between Selenium 2 and Selenium.
As the Selenium WebDriver has been accepted as the W3C Standard, Selenium 3 brings a number of changes to the browser implementations. All of the major browser vendors now support WebDriver specification and provide the necessary features along with the browser. For example, Microsoft came with EdgeDriver, and Apple supports the SafariDriver implementation. We will see some of these changes later in this book.
As W3C-standard WebDriver comes with a better set of APIs, which meet the expectations of most developers by being similar to the implementation of object-oriented programming.
WebDriver is being actively developed and is now supported by Browser vendors per W3C specification; you can see many advanced interactions with the web as well as mobile applications, such as File-Handling and Touch APIs.
One of the major differences introduced in Selenium 3 was the introduction of the Appium project. The mobile-testing features that were part of Selenium 2 are now moved into a separate project named Appium.
Appium is an open source mobile-automation framework for testing native, hybrid, and web mobile apps on iOS and Android platforms using the JSON-Wire protocol with Selenium WebDriver. Appium replaces the iPhoneDriver and AndroidDriver APIs in Selenium 2 that were used to test mobile web applications.
Appium enables the use and extension of the existing Selenium WebDriver framework to build mobile tests. As it uses Selenium WebDriver to drive the tests, we can use any programming language to create tests for a Selenium client library.
