Selenium WebDriver Quick Start Guide
eBook - ePub

Selenium WebDriver Quick Start Guide

Write clear, readable, and reliable tests with Selenium WebDriver 3

Pinakin Chaubal

Share book
  1. 192 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Selenium WebDriver Quick Start Guide

Write clear, readable, and reliable tests with Selenium WebDriver 3

Pinakin Chaubal

Book details
Book preview
Table of contents
Citations

About This Book

Get writing tests and learn to design your own testing framework with Selenium WebDriver API

Key Features

  • Learn Selenium from the ground up
  • Design your own testing framework
  • Create reusable functionality in your framework

Book Description

Selenium WebDriver is a platform-independent API for automating the testing of both browser and mobile applications. It is also a core technology in many other browser automation tools, APIs, and frameworks. This book will guide you through the WebDriver APIs that are used in automation tests.

Chapter by chapter, we will construct the building blocks of a page object model framework as you learn about the required Java and Selenium methods and terminology.

The book starts with an introduction to the same-origin policy, cross-site scripting dangers, and the Document Object Model (DOM). Moving ahead, we'll learn about XPath, which allows us to select items on a page, and how to design a customized XPath. After that, we will be creating singleton patterns and drivers. Then you will learn about synchronization and handling pop-up windows. You will see how to create a factory for browsers and understand command design patterns applicable to this area.

At the end of the book, we tie all this together by creating a framework and implementing multi-browser testing with Selenium Grid.

What you will learn

  • Understand what an XPath is and how to design a customized XPath
  • Learn how to create a Maven project and build
  • Create a Singleton driver
  • Get to grips with Jenkins integration
  • Create a factory for browsers
  • Implement multi-browser testing with Selenium Grid
  • Create a sample pop-up window and JavaScript alert
  • Report using Extent Reports

Who this book is for

This book is for software testers or developers.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Selenium WebDriver Quick Start Guide an online PDF/ePUB?
Yes, you can access Selenium WebDriver Quick Start Guide by Pinakin Chaubal in PDF and/or ePUB format, as well as other popular books in Ciencia de la computación & Control y garantía de calidad. We have over one million books available in our catalogue for you to explore.

Information

Hybrid Framework

We are in the final chapter, where we will explore two new concepts: TestNG Listeners and DataProviders. We will integrate our framework with reports and logging. In this chapter, we will look at the following concepts:
  • Introducing WebDriverManager
  • DataProviders in TestNG
  • TestNG listeners and assertions
  • Incorporating logging and reporting in the framework
  • Steps to add keywords to the framework
Let's start the chapter with an introduction to a new concept: WebDriverManager.

Technical requirements

You will be required to have Ashot API in addition to software mentioned in Chapter 7, The Command Pattern and Creating Components.
The code files of this chapter can be found on GitHub:
https://github.com/PacktPublishing/Selenium-WebDriver-Quick-Start-Guide/tree/master/Chapter08
Check out the following video to see the code in action:
http://bit.ly/2O8KoXW

Introducing the WebDriverManager library

One constraint in Selenium automation is that we need to download the driver binary executable for Chrome, Firefox, and Internet Explorer. After downloading the executable files for Java, the absolute path of this executable file has to be set to JVM properties using System.setProperty. This was an overhead which has been removed with the introduction of a library called WebDriverManager.
Remember to use JDK 1.8 with this library. It won't work with JDK 1.7.
With this library, there is no need to download the individual binaries for the different browsers. Earlier, we had to manage the versions of the binaries manually. With this library, this gets handled automatically.

How to use the WebDriverManager library

The WebDriverManager library can be used in three ways:
  • As a Java dependency
  • As a command-line interface
  • As a server in itself
Let's look at each of these individually.

WebDriverManager as a Java dependency

One of the ways to use the WebDriverManager library is as a Java dependency. Add the dependency shown in the following code in the Dependencies section. This dependency can be found on GitHub:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
3.0.0 is the latest version at the time of writing.
Prior to WebDriverManager, we had to write the following in order to work with the browsers. Sample code for the Chrome browser is shown here:
private WebDriver driver = null;
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
driver = new ChromeDriver();
The same code can be written efficiently, as shown here:
private WebDriver driver = null;
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
As we can seen, we don't require to set the binary path using System.setProperty. WebDriverManager handles it for us.
The following code fragment shows how to use WebDriverManager for Firefox:
private WebDriver driver=null;
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
The following code fragment shows how to use WebDriverManager for Internet Explorer:
private WebDriver driver=null;
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
The sample code is shown here:
public class BrowserFactory {
private WebDriver driver = null;
public void openBrowser(String browser) {
driver = null;
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
}
We will next see how to use WebDriverManager on the command line.

WebDriverManager as CLI

We can use WebDriverManager on the command line as well the the Java dependency. We do this simply by using the following Maven command:
mvn exec:java -Dexec.args="chrome"
On executing this command, the following logs get displayed in the console:

WebDriverManagerServer

WebDriverManager can also be used as a server. There are two ways to use it as a server:
  • Directly from the code and Maven
  • Using WebDriverManager as a fat JAR
Let's look at both of these methods:
  1. Directly from the code and Maven: The command to be used is: mvn exec:java -Dexec.args="server <port>". If the second argument is omitted, the default port (4041) will be used: C:\ Maven mvn exec:java -Dexec.args="server":
  1. Using WebDriverManager as a fat-jar: We can also use WebDriverManager as a fat JAR. For instance: java -jar webdrivermanager-3.0.0-fat.jar chrome.
After executing this command, the following gets printed in the console:
When WebDriverManager gets up and running, HTTP requests can be done to resolve driver binary executables (chromedriver, geckodriver, and so on). For example, suppose that WebDriverManager is running the localhost and in the default port, navigates to the following URLs to get the latest versions of the driver executables:
  • http://localhost:4041/chromedriver: The latest version of chromedriver
  • http://localhost:4041/firefoxdriver: The latest version of geckodriver
  • http://localhost:4041/operadriver: The latest version of operadriver
  • http://localhost:4041/phantomjs: The latest version of phantomjs driver
  • http://localhost:4041/edgedriver: The latest version of MicrosoftWebDriver
  • http://localhost:4041/iedriver: The latest version of IEDriverServer
These requests can be initiated using a normal browser. The driver binary is then automatically downloaded by the browser as an attachment in the HTTP response when the response is sent back.

Advantages of WebDriverManager

Let's see what advantages the WebDriverManager offers:
  • It checks the browser version installed on your machine (for example, Chrome, Firefox, Internet Explorer, and so on).
  • It checks the version of the driver (for example, chromedriver, geckodriver). If the version is not known, it uses the latest version of the driver.
  • It downloads the WebDriver binary executable if it is not present in the WebDriverManager cache in the Maven repository (~/.m2/repository/webdriver by default).
  • It exports the appropriate WebDriver Java environment variables required by Selenium (not done when WebDriverManager is used from the the command-line interface or as a server).
Now that we know about the WebDriverManager library, we will look at what DataProviders are in TestNG.

DataProviders in TestNG

DataProviders in Tes...

Table of contents