Hands-On Selenium WebDriver with Java A Deep Dive into the Development of End-to-End Tests (Boni Garcia) (Z-Library)
Author: Boni Garcia
技术
No Description
📄 File Format:
PDF
💾 File Size:
17.9 MB
53
Views
0
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
Hands-On Selenium WebDriver with Java A Deep Dive into the Development of End-to-End Tests Boni García Foreword by Simon Stewart Covers Selenium 4 G a rcía
📄 Page
2
(This page has no text content)
📄 Page
3
Boni García Hands-On Selenium WebDriver with Java A Deep Dive into the Development of End-to-End Tests Boston Farnham Sebastopol TokyoBeijing
📄 Page
4
978-1-098-11000-0 [LSI] Hands-On Selenium WebDriver with Java by Boni García Copyright © 2022 Boni García. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (https://oreilly.com). For more information, contact our corporate/institu‐ tional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Suzanne McQuade Development Editor: Rita Fernando Production Editor: Kristen Brown Copyeditor: Piper Editorial Consulting, LLC Proofreader: JM Olejarz Indexer: Sam Arnold-Boyd Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Kate Dullea April 2022: First Edition Revision History for the First Edition 2022-03-31: First Release See https://oreilly.com/catalog/errata.csp?isbn=9781098110000 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Hands-On Selenium WebDriver with Java, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
📄 Page
5
To the most precious thing to me in the world: my children, Pablo and Carlos. I love you more than anything.
📄 Page
6
(This page has no text content)
📄 Page
7
Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Part I. Introduction 1. A Primer on Selenium. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Selenium Core Components 3 Selenium WebDriver 5 Selenium Grid 6 Selenium IDE 8 Selenium Ecosystem 10 Language Bindings 10 Driver Managers 11 Locator Tools 12 Frameworks 12 Browser Infrastructure 14 Community 15 Software Testing Fundamentals 16 Levels of Testing 16 Types of Testing 19 Testing Methodologies 21 Test Automation Tools 24 Summary and Outlook 28 v
📄 Page
8
2. Preparing for Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Requirements 29 Java Virtual Machine 30 Text Editor or IDE 30 Browsers and Drivers 30 Build Tools 31 Optional Software 31 Project Setup 32 Project Layout 32 Dependencies 34 Hello World 45 Using Additional Browsers 48 Summary and Outlook 49 Part II. The Selenium WebDriver API 3. WebDriver Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Basic WebDriver Usage 53 WebDriver Creation 53 WebDriver Methods 57 Session Identifier 58 WebDriver Disposal 59 Locating WebElements 59 The Document Object Model (DOM) 59 WebElement Methods 61 Location Strategies 62 Finding Locators on a Web Page 72 Compound Locators 74 Relative Locators 76 What Strategy Should You Use? 80 Keyboard Actions 82 File Uploading 83 Range Sliders 84 Mouse Actions 85 Web Navigation 85 Checkboxes and Radio Buttons 86 User Gestures 86 Right-Click and Double-Click 88 Mouseover 89 Drag and Drop 90 vi | Table of Contents
📄 Page
9
Click and Hold 91 Copy and Paste 92 Waiting Strategies 94 Implicit Wait 94 Explicit Wait 96 Fluent Wait 98 Summary and Outlook 100 4. Browser-Agnostic Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Executing JavaScript 101 Synchronous Scripts 102 Pinned Scripts 108 Asynchronous Scripts 109 Timeouts 110 Page Loading Timeout 110 Script Loading Timeout 111 Screenshots 112 WebElement Screenshots 115 Window Size and Position 116 Browser History 117 The Shadow DOM 118 Cookies 120 Dropdown Lists 125 Data List Elements 127 Navigation Targets 128 Tabs and Windows 129 Frames and Iframes 131 Dialog Boxes 133 Alerts, Confirms, and Prompts 134 Modal Windows 136 Web Storage 137 Event Listeners 138 WebDriver Exceptions 142 Summary and Outlook 144 5. Browser-Specific Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Browser Capabilities 145 Headless Browser 147 Page Loading Strategies 151 Device Emulation 153 Web Extensions 155 Table of Contents | vii
📄 Page
10
Geolocation 160 Notifications 162 Browser Binary 165 Web Proxies 166 Log Gathering 168 Get User Media 169 Loading Insecure Pages 171 Localization 173 Incognito 175 Edge in Internet Explorer Mode 175 The Chrome DevTools Protocol 177 CDP Selenium Wrappers 177 CDP Raw Commands 180 Location Context 191 Web Authentication 191 Print Page 193 WebDriver BiDi 194 Summary and Outlook 196 6. Remote WebDriver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Selenium WebDriver Architecture 197 Creation of RemoteWebDriver Objects 199 RemoteWebDriver Constructor 199 RemoteWebDriver Builder 201 WebDriverManager Builder 201 Selenium-Jupiter 202 Selenium Grid 203 Standalone 203 Hub-nodes 207 Fully Distributed 208 Observability 213 Configuration 216 Cloud Providers 217 Browsers in Docker Containers 219 Docker Images for Selenium Grid 220 Selenoid 222 WebDriverManager 224 Selenium-Jupiter 227 Summary and Outlook 228 viii | Table of Contents
📄 Page
11
Part III. Advanced Concepts 7. The Page Object Model (POM). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Motivation 231 The POM Design Pattern 232 Page Objects 234 Robust Page Objects 236 Creating a Domain Specific Language (DSL) 240 Page Factory 242 Summary and Outlook 244 8. Testing Framework Specifics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Parameterized Tests 245 Cross-Browser Testing 252 Categorizing and Filtering Tests 256 Ordering Tests 261 Failure Analysis 265 Retrying Tests 273 Parallel Test Execution 278 Test Listeners 282 Disabled Tests 286 Summary and Outlook 289 9. Third-Party Integrations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 File Download 291 Using Browser-Specific Capabilities 292 Using an HTTP Client 294 Capture Network Traffic 296 Nonfunctional Testing 298 Performance 298 Security 303 Accessibility 306 A/B Testing 307 Fluent API 308 Test Data 309 Reporting 312 Behavior Driven Development 316 Web Frameworks 320 Summary and Outlook 322 Table of Contents | ix
📄 Page
12
10. Beyond Selenium. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Mobile Apps 325 Mobile Testing 326 Appium 327 REST Services 332 REST Assured 334 Alternatives to Selenium 336 Cypress 336 WebDriverIO 339 TestCafe 340 Puppeteer 341 Playwright 342 Summary and Final Remarks 344 A. What’s New in Selenium 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 B. Driver Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 C. Examples Repository Setup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 x | Table of Contents
📄 Page
13
Foreword In 1999, Kent Beck wrote Extreme Programming Explained. This introduced the world to Extreme Programming (XP). For many people this was the way they first heard about Agile software development. Over the next 20 years, many of the ideas behind the book faded away, but there was one idea that stuck: we should be writing automated tests that verify our code is working as it should. XP expected these tests to be written before the app logic, leading to Test Driven Development (TDD). Today, while strict TDD is seldom practiced, the idea of writing tests is prevalent (though not always popular!). Most companies now acknowledge the need for some kind of automated testing. Many of us actually write tests! Even “regular” QA roles now frequently require people to write code. In 2004, Jason Huggins started Selenium at a software development consultancy called Thoughtworks, which specialized in Agile development. Employees were stee‐ ped in XP and were keen proponents of TDD. From the very beginning, Selenium has been closely associated with testing. Back then, testing websites in a browser was relatively simple. These were the olden days, when there were dozens of browsers to choose between and JS was still spelled “JavaScript.” Sites were small, functionality limited, and the interactions the user could have via the browser were limited too: maybe just filling out a form and click‐ ing a “submit” button. This is the world that Selenium was born into, and the APIs and functionality it offered were as focused as the platform it tested. Then the world discovered XMLHttpRequest hiding in Internet Explorer, it was implemented in Firefox, and suddenly “Web 2.0” became the hot new buzzword. Google Maps showed the world what browsers could do, and the world loved it! Web‐ sites started offering more functionality, driven by carefully handcrafted JS. Selenium adapted and evolved too. I wrote the WebDriver APIs, and these came to the fore. Although they aimed to lead people in a certain direction, the underlying complexity of what Selenium was trying to automate meant that it became a more complicated tool. xi
📄 Page
14
As I write this, browsers are more capable, powerful, and flexible than ever before. We don’t write “websites” any more. We write “web apps,” the current ultimate expression of that being the “Single Page App” or SPA. These push browsers harder than ever, but they’re a natural evolution. Fortunately, once again, Selenium has evolved and grown to allow these kinds of apps to be automated, adding a range of new features in Selenium 4 to help cope with the new testing needs. Adding this func‐ tionality has made Selenium an even more complicated tool. But despite having grown more complicated over the years, Selenium is a tool used by people of all levels of programming comfort and ability. There’s more to writing a successful Selenium test than “just” learning the APIs. There’s a wealth of technology that surrounds it, from the test frameworks you can use, to the Design Patterns you can (and should!) follow when writing the tests, to how we manage the binary depen‐ dencies required by our tests. If we want our tests to run in a reasonable amount of time, we need to have access to infrastructure that supports this. There’s just so much to learn, and there’s surprisingly little guidance for how all the pieces fit together. That’s why I’m so glad that Boni has written this book. It starts by explaining what Selenium is, and the various components within it, and then each chapter builds on the previous ones, gradually introducing more ideas and concepts in a way that feels natural and obvious. Better yet, Boni goes further than just discussing how to use the raw APIs. He also describes the ecosystem of services, tools, and test runners that people need to under‐ stand to get the most from the tool. His experience using Selenium and providing some of these supporting tools shines through: you’re in the hands of a master here. The way this book is structured allows anyone using Selenium to dive in at the point that feels right to them. Just getting started? Then start at the beginning of the book, as Boni lays out the basics for you in an engaging and approachable way. Maybe you’re familiar with Selenium but want to know what’s new in Selenium 4, or some of the less well-known features it offers? Then just jump into the middle of the book; there’s so much there, even I learned a few things! One thing that I hope people take away from this book is that Selenium is only part of the puzzle that is automated testing. Boni covers this too, introducing readers (you!) to how to integrate it into your test frameworks, to the various unit testing libraries you might want to use, and to Design Patterns that can help keep your tests maintain‐ able and fresh. After all, although it may take time to write an automated test in the first place, it can live for years, and being able to work on it with ease is important. xii | Foreword
📄 Page
15
This book paves the road to mastering Selenium and using it effectively. I sincerely hope that this makes using it easier and—dare I say it?—more enjoyable. — Simon Mavi Stewart Creator of WebDriver, Selenium Project Lead 2009–2021, and coeditor of the W3C WebDriver and WebDriver BiDi specifications London, January 2022 Foreword | xiii
📄 Page
16
(This page has no text content)
📄 Page
17
Preface Selenium is an open source umbrella project that enables the automation of web browsers. The core component of the Selenium project is Selenium WebDriver, a library for controlling browsers (e.g., Chrome, Firefox, Edge, Safari, or Opera) pro‐ grammatically. Selenium WebDriver provides a cross-browser Application Program‐ ming Interface (API) in several programming languages (officially supported in Java, JavaScript, Python, C#, or Ruby). Although we can use Selenium WebDriver for multiple purposes related to browser automation, its primary use is implementing end-to-end tests for web application verification. Thousands of organizations and testers now use Selenium worldwide, and it is one of the leading solutions for end-to-end testing, supporting a multi- million-dollar industry. Who Should Read This Book This book provides a comprehensive summary of the main features of Selenium WebDriver version 4, using Java as language binding. It reviews the main aspects of automated web navigation, browser manipulation, web element interaction, user impersonation, automated driver management, the Page Object Model (POM) design pattern, use of remote and cloud infrastructure, integration with Docker and third- party tools, and much more. The primary audience of this book includes Java coders of different levels (from beginner to advanced), such as developers, testers, QA engineers, etc. Thus, you need a basic knowledge of the Java language and object-oriented programming. The final goal is to have a comprehensive understanding of the main aspects of Selenium Web‐ Driver to create end-to-end tests in Java using different testing frameworks of your choice (e.g., JUnit or TestNG). xv
📄 Page
18
Why I Wrote This Book Test automation is a software testing technique that leverages automation tools to control test execution. It allows increased efficiency and effectiveness while ensuring the overall quality of a software system. In this arena, Selenium WebDriver is the de facto standard library to develop end-to-end tests for web applications. This book provides the first complete review of Selenium 4 to date. The book follows a learn-by-doing approach. To that aim, we review the main fea‐ tures of Selenium WebDriver through ready-to-be-executed test examples. These examples are publicly available in a GitHub open source repository (https:// github.com/bonigarcia/selenium-webdriver-java). For the sake of completeness, this repository contains each test example in different flavors of the embedding testing framework: JUnit 4, JUnit 5 (alone or with Selenium-Jupiter), and TestNG. Navigating This Book The content of this book is divided into 3 parts and 10 chapters: Part I, Introduction Part I provides technological background on Selenium, test automation, and project setup. This part, more theoretical than practical, is composed of two chapters: • Chapter 1, “A Primer on Selenium”, presents the core components of the Sele‐ nium project (WebDriver, Grid, and IDE) and its ecosystem (i.e., the tools and technologies around Selenium). In addition, this chapter reviews the principles of end-to-end testing related to Selenium. • Chapter 2, “Preparing for Testing”, explains how to set up a Java project (Maven and Gradle) containing end-to-end tests that use the Selenium WebDriver API. Then, you will learn how to develop your first WebDriver tests using different testing frameworks: JUnit 4, JUnit 5 (alone or in conjunction with Selenium- Jupiter), and TestNG. Part II, The Selenium WebDriver API Part II provides practical insight into the Selenium WebDriver API. This part is gui‐ ded by tests available in the examples repository and includes the following chapters: • Chapter 3, “WebDriver Fundamentals”, describes the primary aspects of the Sele‐ nium WebDriver API for carrying out automated interaction with web applica‐ tions. Thus, this chapter reviews several strategies for locating and waiting for web elements. In addition, you will discover how to impersonate user actions (i.e., automated interactions using the keyboard and mouse) in a browser. xvi | Preface
📄 Page
19
• Chapter 4, “Browser-Agnostic Features”, reviews those aspects of the Selenium WebDriver API that are interoperable in different browsers. Hence, this chapter shows how to execute JavaScript, create event listeners, manage windows, make screenshots, handle the shadow DOM, manipulate cookies, access the browser history or web storage, or interact with windows, tabs, and iframes, among other elements. • Chapter 5, “Browser-Specific Manipulation”, explains those aspects of the Sele‐ nium WebDriver API particular to specific browsers. This group of features cov‐ ers browser capabilities (options, arguments, preferences, etc.), the Chrome DevTools Protocol (CDP), geolocation functions, basic and web authentication, printing pages to PDF, or the WebDriver BiDi API. • Chapter 6, “Remote WebDriver”, describes how to use the Selenium WebDriver API to control remote browsers. Then, you will learn how to set up and use Sele‐ nium Grid version 4. Finally, you will discover how to use advanced infrastruc‐ ture for Selenium tests in cloud providers (e.g., Sauce Labs, BrowserStack, or CrossBrowserTesting, among others) and browsers in Docker containers. Part III, Advanced Concepts Part III focuses on leveraging the Selenium WebDriver API in different ambits and use cases. This part includes the following chapters: • Chapter 7, “The Page Object Model (POM)”, introduces POM, a popular design pattern used in conjunction with Selenium WebDriver. This pattern allows users to model web pages using object-oriented classes to ease test maintenance and reduce code duplication. • Chapter 8, “Testing Framework Specifics”, reviews several particular features of the unit testing framework used together with Selenium WebDriver that allow improvements to different aspects of the overall testing process. To that aim, this chapter first explains how to carry out cross-browser testing (i.e., reusing the same test logic for verifying web applications using different browsers) using par‐ ameterized tests and test templates. Then, you will learn how to split tests into different categories for execution filtering, ordering tests, failure analysis (i.e., collecting and analyzing data to determine the cause of a failure), retrying tests, parallel test execution, test listeners, or disabling tests. • Chapter 9, “Third-Party Integrations”, reviews different technologies you can use to enhance your Selenium WebDriver tests, such as reporting tools, test data gen‐ eration, and other frameworks (e.g., Cucumber or Spring). Moreover, this chap‐ ter describes how to use external libraries with Selenium to implement specific use cases, such as file downloading or nonfunctional tests (such as load, security, or accessibility). Preface | xvii
📄 Page
20
• Chapter 10, “Beyond Selenium”, presents a couple of automation frameworks related to Selenium: Appium (for mobile testing) and REST Assured (for testing REST web services). To conclude, we review some of the most relevant current alternatives to Selenium WebDriver, such as Cypress, WebDriverIO, TestCafe, Puppeteer, or Playwright. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program ele‐ ments such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that the user should type literally. Constant width italic Shows text that should be replaced with user-supplied values or by values deter‐ mined by context. This element signifies a tip or suggestion. This element signifies a general note. This element indicates a warning or caution. xviii | Preface
The above is a preview of the first 20 pages. Register to read the complete e-book.