Selenium WebDriver Quick Start Guide
eBook - ePub

Selenium WebDriver Quick Start Guide

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

Pinakin Chaubal

Buch teilen
  1. 192 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Selenium WebDriver Quick Start Guide

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

Pinakin Chaubal

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Selenium WebDriver Quick Start Guide als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Selenium WebDriver Quick Start Guide von Pinakin Chaubal im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Ciencia de la computación & Control y garantía de calidad. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

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...

Inhaltsverzeichnis