Book Image

Mastering Selenium WebDriver

By : Mark Collin
Book Image

Mastering Selenium WebDriver

By: Mark Collin

Overview of this book

<p>Selenium WebDriver, also known as Selenium 2, is a UI automation tool used by software developers and QA engineers to test their web applications on different web browsers. The Selenium WebDriver API is fully object oriented compared with the deprecated Selenium RC. The WebDriver API provides multi-language support and run tests on all the most popular browsers.</p> <p>In this wide and complex World Wide Web era, this book will teach you how to tame it by gaining an in-depth understanding of the Selenium API.</p> <p>This book starts with how to solve the difficult problems that you will undoubtedly come across as you start using Selenium in an enterprise environment, followed by producing the right feedback when failing, and what the common exceptions are, explain them properly (including the root cause) and tell you how to fix them. You will also see the differences between the three available implicit waits and explicit waits, and learn to working with effective page objects.</p> <p>Moving on, the book shows you how to utilize the Advanced User Interactions API, how you can run any JavaScript you need through Selenium, and how to quickly spin up a Selenium Grid using Docker containers.</p> <p>At the end, the book will discuss the upcoming Selenium W3C specification and how it is going to affect the future of Selenium.</p>
Table of Contents (17 chapters)
Mastering Selenium WebDriver
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

This book is going to focus on some of the more advanced aspects of Selenium. It will help you develop a greater understanding of Selenium as a test tool and give you a series of strategies to help you create reliable and extensible test frameworks.

In the world of automation, there is rarely only one correct way of doing things. This book will provide you with a series of minimalistic implementations that are flexible enough to be customized to your specific needs.

This book is not going to teach you how to write bloated test frameworks that hide Selenium behind an impenetrable veil of obscurity. Instead, it will show you how to complement Selenium with useful additions that fit seamlessly into the rich and well-crafted API that Selenium already offers you.

What this book covers

Chapter 1, Creating a Fast Feedback Loop, shows you how to build a small but powerful Selenium framework that enables you to get started quickly. We will focus on building something that enables people to run Selenium tests quickly and easily without having to download and configure libraries and external binaries themselves.

Chapter 2, Producing the Right Feedback When Failing, shows you how to get the framework, which was built in the first chapter, running on a continuous integration server. We will then extend the framework so that it can connect to Sauce Labs (or any other Selenium Grid) and take screenshots when tests fail. We will finish off by looking at stack traces and how we can use the information in them to find out why our tests fail.

Chapter 3, Exceptions Are Actually Oracles, examines common exceptions that are thrown by Selenium when running tests. We will work through the exceptions in detail and explore ways to make sure that our tests don't throw them when they shouldn't.

Chapter 4, The Waiting Game, explores the most common cause behind test failures in automation. It will explain in detail how waits work in Selenium and how you should use them to ensure that you have stable and reliable tests.

Chapter 5, Working with Effective Page Objects, shows you how to use page objects in Selenium. It focuses on proper separation of concerns and also demonstrates how to use the Page Factory classes in the Selenium support package. It finishes off by demonstrating how to build fluent page objects.

Chapter 6, Utilizing the Advanced User Interactions API, shows how you can automate challenging scenarios such as hover menus and drag-and-drop controls. It will also highlight some of the problems that you may come across when using the Advanced User Interactions API.

Chapter 7, JavaScript Execution with Selenium, introduces the JavaScript executor and shows how you can use it to work around complex automation problems. We will also look at how we can execute asynchronous scripts that use a callback to notify Selenium that they have completed execution.

Chapter 8, Keeping It Real, shows you what cannot be done with Selenium. We will then go through a series of scenarios that demonstrate how to extend Selenium to work with external libraries and applications so that we can use the right tool for the job.

Chapter 9, Hooking Docker into Selenium, introduces Docker. We will have a look at how we can spin up a Selenium Grid using Docker and start the Docker containers as a part of the build process.

Chapter 10, Selenium – the Future, talks about how Selenium is changing as it becomes a W3C specification. You will also find out how you can help shape the future of Selenium by contributing to the project in multiple ways.

What you need for this book

The following software are required for the book:

  • Oracle JDK8*

  • Maven 3

  • IntelliJ IDEA 14**

  • JMeter

  • Zed Attack Proxy

  • boot2docker or Docker***

  • Mozilla Firefox

  • Google Chrome

Generally, the more the browsers you install, the better. You will be able to perform all the exercises in this book if you have at least Mozilla Firefox and Google Chrome installed.

* You can try using the older versions of Oracle JDK or OpenJDK, but your mileage may vary.

** The community edition of IntelliJ IDEA is free, but it's certainly worth purchasing a license to get access to the full functionality. You can use the older versions of IntelliJ IDEA or another IDE according to your preference. The code for this book has been written in IntelliJ IDEA 14.

*** If you are running Linux, you will be able to install Docker without having to use boot2docker.

Who this book is for

This book is for testers who are interested in automation, have some experience working with Selenium, are competent with Java, and are looking forward to taking the next step in their learning journey.

Java is one of the most widely used languages in the enterprise world. You will often see Java coupled with Maven because of its extremely powerful dependency management and build capabilities. This book will show you how to start using these technologies to their full potential.

The theories in this book are transferable to any language. However, if you are not using Java, the code examples will be of limited use.

Who is this book not for?

This book is not an introduction to basic Selenium functionality. If you have not encountered Selenium before, or if you want to learn the basics of Selenium, you should start out with Selenium 2 Testing Tools: Beginner's Guide, David Burns.

It is assumed that you will be able to do simple things like finding elements in the DOM with appropriate locators, and that you will have a reasonable level of Java knowledge. This book is not going to teach you how to write Java code.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Start off by creating a src/test/java directory, and then into this directory we will create a file called basicTest.java."

A block of code is set as follows:

package com.masteringselenium;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeSuite;

public class DriverFactory {

    private static ThreadLocal<WebDriverThread> driverThread;

    @BeforeSuite
    public static void instantiateDriverObject() {
        driverThread = new ThreadLocal<WebDriverThread>() {
            @Override
            protected WebDriverThread initialValue() {
                WebDriverThread webDriverThread = new WebDriverThread();
                return webDriverThread;
            }
        };
    }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class DriverFactory {

    private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
    private static ThreadLocal<WebDriverThread> driverThread;

    @BeforeSuite
    public static void instantiateDriverObject() {
        driverThread = new ThreadLocal<WebDriverThread>() {
            @Override
            protected WebDriverThread initialValue() {
                WebDriverThread webDriverThread = new WebDriverThread();
                webDriverThreadPool.add(webDriverThread);
                return webDriverThread;
            }
        };
    }

Any command-line input or output is written as follows:

mvn clean install

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Let's start off by clicking on the Create New Project button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.