Learn Selenium - Unmesh Gundecha - E-Book

Learn Selenium E-Book

Unmesh Gundecha

0,0
43,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Learn end-to-end automation testing techniques for web and mobile browsers using Selenium WebDriver, AppiumDriver, Java, and TestNG

Key Features

  • Explore the Selenium grid architecture and build your own grid for browser and mobile devices
  • Use ExtentReports for processing results and SauceLabs for cloud-based test services
  • Unlock the full potential of Selenium to test your web applications.

Book Description

Selenium WebDriver 3.x is an open source API for testing both browser and mobile applications. With the help of this book, you can build a solid foundation and can easily perform end-to-end testing on web and mobile browsers.You'll begin by being introduced to the Selenium Page Object Model for software development. You'll architect your own framework with a scalable driver class, Java utility classes, and support for third-party tools and plugins. You'll design and build a Selenium grid from scratch to enable the framework to scale and support different browsers, mobile devices, and platforms.You'll strategize and handle a rich web UI using the advanced WebDriver API and learn techniques to handle real-time challenges in WebDriver. You'll perform different types of testing, such as cross-browser testing, load testing, and mobile testing. Finally, you will also be introduced to data-driven testing, using TestNG to create your own automation framework.By the end of this Learning Path, you'll be able to design your own automation testing framework and perform data-driven testing with Selenium WebDriver.

This Learning Path includes content from the following Packt products:

  • Selenium WebDriver 3 Practical Guide - Second Edition by Unmesh Gundecha
  • Selenium Framework Design in Data-Driven Testing by Carl Cocchiaro

What you will learn

  • Use different mobile and desktop browser platforms with Selenium 3
  • Use the Actions API for performing various keyboard and mouse actions
  • Design the Selenium Driver Class for local, remote, and third-party grid support
  • Build page object classes with the Selenium Page Object Model
  • Develop data-driven test classes using the TestNG framework
  • Encapsulate data using the JSON protocol
  • Build a Selenium Grid for RemoteWebDriver testing
  • Build and use utility classes in synchronization, file I/O, reporting and test listener classes

Who this book is for

This Learning Path is ideal for software quality assurance/testing professionals, software project managers, or software developers interested in using Selenium for testing their applications. Professionals responsible for designing and building enterprise-based testing frameworks will also find this Learning Path useful. Prior programming experience in Java are TestNG is necessary.

Unmesh Gundecha has an experience of over 16 years 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. He is the author of Selenium Testing Tools Cookbook and Learning Selenium Testing Tools with Python, both by Packt Publishing. Carl Cocchiaro has a bachelor's degree in business and over 30 years of experience in the software engineering field, designing and building test frameworks for desktop, browser, and mobile applications. He is an expert in the Selenium WebDriver/TestNG Java-based technologies. He is a certified SilkTest engineer and has architected UI and RESTful API automation frameworks for 25 major corporations. Carl is currently a software architect, quality engineering at RSA/Dell technologies, Boston, MA, USA.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 498

Veröffentlichungsjahr: 2019

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Learn Selenium

 

 

 

 

 

 

 

 

 

Build data-driven test frameworks for mobile and web applications with Selenium Web Driver 3

 

 

 

 

 

 

 

Unmesh Gundecha
Carl Cocchiaro

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Learn Selenium

 

Copyright © 2019 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 authors, 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.

 

First published: July 2019

 

Production reference: 1120719

 

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

 

ISBN 978-1-83898-304-8

 

www.packtpub.com

Contributors

About the authors

Unmesh Gundecha has an experience of over 16 years 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. He is the author of Selenium Testing Tools Cookbook and Learning Selenium Testing Tools with Python, both by Packt Publishing.

 

 

 

 

 

 

Carl Cocchiaro has a bachelor's degree in business and over 30 years of experience in the software engineering field, designing and building test frameworks for desktop, browser, and mobile applications. He is an expert in the Selenium WebDriver/TestNG Java-based technologies. He is a certified SilkTest engineer and has architected UI and RESTful API automation frameworks for 25 major corporations. Carl is currently a software architect, quality engineering at RSA/Dell technologies, Boston, MA, USA.

Packt is searching for authors like you

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.

 
mapt.io

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.

Why subscribe?

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

Packt.com

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

Table of Contents

Title Page

Copyright

Learn Selenium

Contributors

About the authors

Packt is searching for authors like you

About Packt

Why subscribe?

Packt.com

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

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

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

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

Building a Scalable Selenium Test Driver Class for Web and Mobile Applications

Introduction

Data-driven testing

Selenium Page Object Model

DRY 

What you will learn

The singleton driver class

Requirements

The class signature

Class variables

JavaDoc

Parameters

Class methods

Using preferences to support browsers and platforms

Browser preferences

Platforms

Using preferences to support mobile device simulators, emulators, and real devices

iOS preferences

Android preferences

Multithreading support for parallel and distributed testing

Passing optional arguments and parameters to the driver

varargs

The parameter for setDriver

JVM argument – -Dswitch

Parameter processing methods

Selenium Grid Architecture support using the RemoteWebDriver and AppiumDriver classes

Third-party grid architecture support including the Sauce Labs Test Cloud

Using property files to select browsers, devices, versions, platforms, languages, and many more

Summary

Selenium Framework Utility Classes

Introduction

Global variables

Synchronization utility classes

Selenium synchronization classes

The ExpectedConditions class

WebDriverWait/FluentWait classes

Custom synchronization methods

The JavascriptExecutor class

The TestNG Listener class

Building the test listener class

Logging the results to the console or log file

Including the test runner in the test class or suite

File I/O class

Property files

Lookup table files

CSV files

Log files

The image capture class

The capture screen method

The capture image method

The compare image method

The reporter class

The JavaMail class

Summary

Best Practices for Building Selenium Page Object Classes

Introduction

What you will learn

Best practices for naming conventions, comments, and folder structures

Naming conventions

Comments

Folder names and structures

Designing and building the abstract base classes for the AUT

The abstract class

Abstract methods

Common locators

Common methods

Wrap up on base classes

Designing and building subclasses for feature-specific pages using inheritance techniques

Encapsulation and using getter/setter methods to retrieve objects from the page object classes

Exception handling and synchronization in page object class methods

Implicit exception handling

TestNG difference viewer

Explicit exception handling

Try...catch exception handling

Synchronizing methods

Table classes

Summary

Defining WebDriver and AppiumDriver Page Object Elements

Introduction

Inspecting page elements on browser applications

Types of locators

Inheriting WebElements

Inspecting WebElements

Third-party plugins/tools

Inspection of page elements on mobile applications

Appium inspector

Inspecting mobile elements

Standards for using static locators

Rules for using standard locators

Simple locators

CSS locators

XPath query locators

Referencing static elements in methods

Retrieving static elements from other classes

Standards for using dynamic locators

Single attribute XPath versus CSS locators

WebElements

MobileElements

Multiple attribute XPath versus CSS locators

Using dynamic locators in methods

Summary

Building a JSON Data Provider

Introduction

What you will learn

The TestNG Data Provider class

The @DataProvider annotation

The @Test annotation

Extracting JSON data into Java objects

Filtering test data

Filtering include and exclude patterns

JSON Data File formats

The JSONObject class

Summary

Developing Data-Driven Test Classes

Introduction

Annotating test class methods using TestNG

TestNG annotations

@Test

TestNG setup/teardown methods

Setup methods

@BeforeSuite, @BeforeTest, @BeforeGroups, @BeforeClass, and @BeforeMethod

Teardown methods

@AfterSuite, @AfterTest, @AfterGroups, @AfterClass, and @AfterMethod

Order of precedence

Naming conventions for test methods

Test classes and data files

Test methods

Test parameters

Test groups

Test setup/teardown methods

Using the TestNG DataProvider

Calling page object methods in test classes

Exception handling in test classes

Test methods

The setup/teardown methods

The ITestResult class

Test listener/reporter classes

Designing base setup classes

TestNG suite file structure

Suite section: <suite>

Groups section: <groups>

Listeners section: <listeners>

Test section: <test>

Suite parameters

@Parameters

Summary

Encapsulating Data in Data-Driven Testing

Introduction

Casting JSON data to Java objects

JSON object

Sequential numbering of row IDs in the data file

Using Java object getter/setter methods

Passing data to page object methods

Building in positive, negative, boundary, and limit testing

Negative testing

Confirmation and exception property files

Property files

Lookup method in DataProvider

JSON data file data

Converting confirmation/error code on the fly

Property files and parsing test data on the fly

Environment property files

System properties

Initializing property file data

Global variables versus dynamic data

Processing JVM args

Retrieving JSON data outside of test methods

Supporting multibranded applications

Multilocators

Conditional code

Runtime flags

Multiple driver support

Dual WebDriver testing

Dual WebDriver and AppiumDriver testing

Parallel testing

Suite XML file

Parallel properties method

Common setup

Summary

Designing a Selenium Grid

Introduction

Virtual grids

Grid structure

Single browser nodes

Multibrowser nodes

Single mobile device nodes

Multimobile/browser nodes

Selenium driver class – WebDriver versus RemoteWebDriver

The setDriver method for browser

The setDriver method for mobile

Overloaded setDriver method for browser

Switching from local to remote driver

Suite parameters

JVM argument

Default global variables

Processing runtime parameters

Selenium standalone server and client drivers

Local use of drivers

Remote use of drivers

Selenium standalone server and browser driver command-line options

Selenium hub

Selenium hub JSON configuration file

Selenium nodes

Selenium node JSON configuration file

Appium server and mobile simulator/emulator command-line options

Appium nodes

Appium node JSON configuration file

Selenium Grid console

Directing traffic to Selenium nodes

Multiple nodes of the same platform and version

Directing traffic using desired capabilities

Maintenance of the Selenium Grid

Summary

Third-Party Tools and Plugins

Introduction

IntelliJ IDEA Selenium plugin

Sample project files

Generating element locators

Wrap-up on Selenium Plugin

TestNG results in IntelliJ and Jenkins

IntelliJ TestNG results

Jenkins TestNG results

HTML Publisher Plugin

Installation

BrowserMob Proxy Plugin

Getting started

ExtentReports Reporter API class

ExtentHTMLReporter

Dashboard page

Categories page

Tests page

Code sample

Sauce Labs Test Cloud services

Sauce Labs Test Cloud features

Browser and mobile platforms

Driver code changes

Dashboard

SauceConnect tunnel

TestObject Real Device Cloud

Jenkins plugin

Advantages and disadvantages of using in-house versus third-party grids

Summary

Working Selenium WebDriver Framework Samples

Introduction

Selenium driver and DataProvider classes

CreateDriver.java

JSONDataProvider class

Selenium utility classes

BrowserUtils.java

Global_VARS.java

TestNG_ConsoleRunner.java

selenium.properties

ExtentReports classes

ExtentTestNGIReporterListener.java

extent-config.xml

Browser page object base and subclasses

PassionTeaCoBasePO.java

PassionTeaCoWelcomePO.java

Browser test class and data files

PassionTeaCoTest.java

PassionTeaCo.json

Browser Suite XML and Maven Pom XML files

PassionTeaCo.xml

pom.xml file

Summary

Assessments

Chapter 1 

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

The Selenium WebDriver 3.x is an open source API to test both browser and mobile applications. With a solid foundation, you can easily perform end-to-end testing on web and mobile browsers.

You’ll begin by being introduced to the Selenium page object design patterns in software development. You’ll architect your own framework with a scalable driver class, Java utility classes, and support for third-party tools and plugins. You'll design and build a Selenium grid from scratch to enable the framework to scale and support different browsers, mobile devices, and platforms. You’ll strategize and handle rich web UI using the advanced WebDriver API and learn techniques to handle real-time challenges in WebDriver. You’ll perform different types of testing, such as cross-browser testing, load testing, and mobile testing. Finally, you will also be introduced to data-driven testing using TestNG to create your own automation framework.

By the end of this Learning Path, you’ll be able to design your own automation testing framework and perform data-driven testing with Selenium WebDriver.

This Learning Path includes content from the following Packt products:

Selenium WebDriver 3 Practical Guide - Second Edition by Unmesh Gundecha

Selenium Framework Design in Data-Driven Testing by Carl Cocchiaro

Who this book is for

This Learning Path is ideal for software quality assurance/testing professionals, software project managers, or software developers interested in using Selenium for testing their applications. Professionals responsible for designing and building enterprise-based testing frameworks will also find this Learning Path useful. Prior programming experience in Java and TestNG is necessary.

What this book covers

Chapter 1, Introducing WebDriver and WebElements, will start off with an overview of Selenium and it's 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, Using Java 8 Features 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 3, 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 4, Exploring Advanced Interactions of WebDriver, 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 5, 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 backnavigation invocation, script execution completion, and so on. We will use these events to run accessibility and performance checks.

Chapter 6, 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 7, 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 8, 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.

Chapter 9, Building a Scalable Selenium Test Driver Class for Web and Mobile Applications, shows users how to get started with designing and building the Selenium Framework driver class. This class is the engine that drives the browser and mobile applications. With Selenium WebDriver technology, users can test all the popular browsers and mobile devices using the same driver class and programming language. The Selenium WebDriver technology is platform independent and has various language bindings to support cross-browser and device testing in a single code base.

Chapter 10, Selenium Framework Utility Classes, describes how to design Java utility classes to support the framework components that are non-specific to any of the applications under test. Users will learn how to build classes to perform file I/O operations, data extraction, logging, synchronization, result processing, reporting, global variables, and many more.

Chapter 11, Best Practices for Building Selenium Page Object Classes, introduces users to designing and building application-specific classes following the Selenium Page Object Model. Users will be guided through designing abstract base classes, deriving sub-classes, and structuring classes to use common inheritance methods to ensure that page elements and methods are stored in central locations. In following these design principles, users will create an abstract separation layer between the page object and test classes in the framework.

Chapter 12, Defining WebDriver and AppiumDriver Page Object Elements, presents users with design techniques to ensure that elements are defined using best practices for locators, minimum number of elements defined in page object classes, how to build locators on the fly, and when to use static verses dynamic locators to test page object elements.

Chapter 13, Building a JSON Data Provider, explains how to design and build a TestNG DataProvider class using the JSON protocol to store data. The concept of data-driven test frameworks is introduced, and how to use a DataProvider to extract data on the fly to ensure that standards for data encapsulation and DRY approaches are being followed is covered.

Chapter 14, Developing Data-Driven Test Classes, explores how to design data-driven test classes using the TestNG technologies. This includes TestNG features such as annotations, parameters, attributes, use of DataProviders in test classes, data extraction, exception handling, and setup/teardown techniques.

Chapter 15, Encapsulating Data in Data-Driven Testing, describes the use of encapsulation in data-driven testing. This will include JSON data manipulation, use of property files, processing JVM arguments, casting JSON data to Java objects, supporting multiple drivers, and parallel testing.

Chapter 16, Designing a Selenium Grid, presents the Selenium Grid Architecture, including designing a virtual grid in the Cloud, how to build the grid hub, browser nodes, and Appium mobile nodes, using the grid console, how to cast tests to the RemoteWebDriver, and supporting third-party grids.

Chapter 17, Third-Party Tools and Plugins, details methodologies in using third-party tools and plugins in the Selenium Framework design. This will include the IntelliJ IDEA Selenium plugin, TestNG for results processing, the HTML Publisher Plugin, BrowserMob, ExtentReports, and Sauce Labs.

Chapter 18, Working Selenium WebDriver Framework Samples, provides users with a real working sample framework including Selenium driver and utility classes, page object base and subclasses, DataProvider class, data-driven test class, JSON data file, TestNG test IListener class, and ExtentReports IReporter classes. Users will be able to install the files in their own project, use the supplied Maven pom.xml file to pull down the required JAR files, and run the sample data-driven tests against a real practice website across multiple browser types.

To get the most out of this book

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:

Java JDK 1.8

IntelliJ IDEA 2017.3+

Selenium WebDriver 3.7.1+ JAR

Selenium Stand-alone Server 3.7.1+ JAR

Appium Java Client 5.0.4+ JAR

Appium Server 1.7.1 JAR for iOS or Linux

 

TestNG 6.11 JAR

ExtentReports 3.1.0 JAR

Browsers: Google Chrome 62.0, Mozilla Firefox 57.0, Microsoft Internet Explorer 11.0

Drivers: chromedriver.exe 2.33, geckodriver.exe 0.19.1, IEDriverServer.exe 3.7.1+

Apple Xcode and iPhone Simulators for iOS

Google Android SDK and Samsung Galaxy emulators for Linux

VMware virtual machines

Installing Java

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

Installing Eclipse

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

Download the example code files

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/Learn-Selenium. 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!

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and 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.packt.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.

Reviews

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

Introducing WebDriver and WebElements

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 Testing Tools

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 

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.

The Selenium Client API is a language-specific Selenium library that provides a consistent Selenium API in programming languages such as Java, C#, Python, Ruby, and JavaScript. These languages bindings let tests to launch a WebDriver session and communicate with the browser or Selenium Server.

Selenium Server

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

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

The Selenium IDE for Firefox stopped working after the Firefox 55 moved to the WebExtension format from XPI format and it is currently no longer maintained.

Differences between Selenium 2 and Selenium 3 

Before we dive further into Selenium 3, let's understand the differences between Selenium 2 and Selenium.

Handling the browser  

As the Selenium WebDriver has been 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.

 Having better APIs

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.

Having developer support and advanced functionalities

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. 

Testing Mobile Apps with Appium

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.

WebElements

A web page is composed of many different types of HTML elements, such as links, textboxes, dropdown buttons, a body, labels, and forms. These are called WebElements in the context of WebDriver. Together, these elements on a web page will achieve the user functionality. For example, let's look at the HTML code of the login page of a website:    

<html>

<body>

<form

id

="loginForm"

>

<label>

Enter Username:

</label>

<input

type

="text"

name

="Username"

/>

<label>

Enter Password:

</label>

<input

type

="password"

name

="Password"

/>

<input

type

="submit"

/>

</form>

<a

href

="forgotPassword.html"

>

Forgot Password ?

</a>

</body>

</html>

In the preceding HTML code, there are different types of WebElements, such as <html>, <body>, <form>, <label>, <input>, and <a>, which together make a web page provide the Login feature for the user. Let's analyze the following WebElement:

<label>

Enter Username:

</label>

Here, <label> is the start tag of the WebElement label. Enter Username: is the text present on the label element. Finally, </label> is the end tag, which indicates the end of a WebElement.

Similarly, take another WebElement:                                                                 

<input

type

="text"

name

="Username"

/>

In the preceding code, type and name are the attributes of the WebElement input with the text and Username values, respectively.

UI-automation using Selenium is mostly about locating these WebElements on a web page and executing user actions on them. In the rest of the chapter, we will use various methods to locate WebElements and execute relevant user actions on them.

The findElement method

In UI automation, locating an element is the first step before executing any user actions on it. WebDriver's findElement() method is a convenient way to locate an element on the web page. According to WebDriver's Javadoc (http://selenium.googlecode.com/git/docs/api/java/index.html), the method declaration is as follows:

WebElement findElement(By by)

So, the input parameter for the findElement() method is the By instance. The By instance is a WebElement-locating mechanism. There are eight different ways to locate a WebElement on a web page. We will see each of these eight methods later in the chapter.

The return type of the findElement() method is the WebElement instance that represents the actual HTML element or component of the web page. The method returns the first WebElement that the driver comes across that satisfies the locating-mechanism condition. This WebElement instance will act as a handle to that component from then on. Appropriate actions can be taken on that component by the test-script developer using this returned WebElement instance.

If WebDriver doesn't find the element, it throws a runtime exception named NoSuchElementException, which the invoking class or method should handle. 

The findElements method

For finding multiple elements matching the same locator criteria on a web page, the findElements() method can be used. It returns a list of WebElements found for a given locating mechanism. The method declaration of the findElements() method is as follows:

java.util.List findElements(By by)

The input parameter is the same as the findElement() method, which is an instance of the By class. The difference lies in the return type. Here, if no element is found, an empty list is returned and if there are multiple WebElements present that satisfy the locating mechanism, all of them are returned to the caller in a list.

Inspecting Elements with Developer Tools

Before we start exploring how to find elements on a page and what locator mechanism to use, we need to look at the HTML code of the page to understand the Document Object Model (DOM) tree, what properties or attributes are defined for the elements displayed on the page, and how JavaScript or AJAX calls are made from the application. browsers use the HTML code written for the page to render visual elements in the browser window. It uses other resources, including JavaScript, CSS, and images, to decide on the look, feel, and behavior of these elements.

Here is an example of a login page of the demo application and the HTML code written to render this page in a browser, as displayed in the following screenshot:

We need tools that can display the HTML code of the page in a structured and easy-to-understand format. Almost all browsers now offer Developer tools to inspect the structure of the page and associated resources.

Inspecting pages and elements with Mozilla Firefox

The newer versions of Mozilla Firefox provide built-in ways to inspect the page and elements. To inspect an element from the page, move the mouse over the desired element and right-click to open the pop-up menu. Select the Inspect Element option, as shown in the following screenshot:

This will display the Inspector tab with the HTML code in a tree format with the selected element highlighted, as shown in the following screenshot:

Using Inspector, we can also validate the XPath or CSS Selectors using the search box shown in the Inspector section. Just enter the XPath or CSS Selector and Inspector will highlight the elements that match the expression, as shown in the following screenshot:

The Developer tools provide various other debugging features. It also generates XPath and CSS selectors for elements. For this, select the desired element in the tree, right-click, and select the Copy >XPath or Copy > CSS Path option from the pop-up menu, as shown in the following screenshot:  

This will paste the suggested XPath or CSS selector value to the clipboard to be used later with the findElement() method.

Inspecting pages and elements in Google Chrome with Developer Tools

Similar to Mozilla Firefox, Google Chrome also provides a built-in feature to inspect pages and elements. We can move the mouse over a desired element on the page, right-click to open the pop-up menu, and then select the Inspect element option. This will open Developer tools in the browser, which displays information similar to that of Firefox, as shown in the following screenshot:

Similar to Firefox, we can also test XPath and CSS Selectors in Google Chrome Developer tools. Press Ctrl + F (on Mac, use Command + F) in the Elements tab. This will display a search box. Just enter XPath or CSS Selector, and matching elements will be highlighted in the tree, as shown in the following screenshot:

Chrome Developer Tools also provides a feature where you can get the XPath for an element by right-clicking on the desired element in the tree and selecting the Copy XPath option from the pop-up menu.

Similar to Mozilla Firefox and Google Chrome, you will find similar Developer tools in any major browser, including Microsoft Internet Explorer and Edge.

Browser developer tools come in really handy during the test-script development. These tools will help you to find the locator details for the elements with which you need to interact as part of the test. These tools parse the code for a page and display the information in a hierarchal tree. 

WebElements on a web page may not have all the attributes declared. It is up to the developer of the test script to select the attribute that uniquely identifies the WebElement on the web page for the automation.

Using the By locating mechanism

By is the locating mechanism passed to the findElement() method or the findElements() method to fetch the respective WebElement(s) on a web page. There are eight different locating mechanisms; that is, eight different ways to identify

an HTML element on a web page. They are located by ID, Name, ClassName, TagName, LinkText, PartialLinkText, XPath, and CSS Selector.