Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Selenium WebDriver Quick Start Guide
Selenium WebDriver Quick Start Guide

Selenium WebDriver Quick Start Guide: Write clear, readable, and reliable tests with Selenium WebDriver 3

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Selenium WebDriver Quick Start Guide

Introducing Selenium WebDriver and Environment Setup

Welcome to the exciting world of test automation using Java 8 and Selenium WebDriver 3.x. Throughout this book, we will get up to speed with Selenium and its surrounding technologies. Selenium is a browser automation tool that has progressed tenfold since its initial inception. Along with tools such as AutoIt, it can be used for automating desktop applications. Selenium is getting used extensively in mobile automation nowadays. The most important point is that it is open source, has a vast developer community, and is constantly evolving. With Selenium Grid, we can simulate different browsers on a single machine.

First, we will start with understanding the basics. This chapter is a gentle introduction to Selenium, and we will be covering the following topics:

  • The need for test automation and its advantages
  • Java 8 (briefly)
  • Selenium RC
  • Selenium WebDriver
  • Various drivers in Selenium
  • Preparing for the first script

Technical requirements

Why is test automation required?

Let's get started by understanding why test automation is needed. Today's agile world needs quick feedback on the code's quality. The developers check-in application code in a source code repository like GitHub. It is imperative that these changes be tested, and the best way to do so is through automation. A test-automation suite can eliminate the mundane work of manual regression testing and can be helpful in finding bugs earlier, thus reducing manual testing time. It can be configured to run at a particular time in the day.

A cut-off time should be provided to the developers, such as 6 P.M. in the evening, by which time they should check in code, get the application build done, and the application deployed to a server like Apache Tomcat. The automation suite may be scheduled to run at 7 P.M. daily. Jenkins is a tool that's used for continuous integration, and so can be used for this purpose.

Advantages of test automation

Advantages of test automation include reducing the burden on the testers doing the manual execution so that they can focus on the functional aspects of the application. Generally, a smoke, sanity, regression test suite is created for this purpose. The advantage of having automatic triggering through Jenkins is that it facilitates test execution in an unattended mode.

Some pointers on Selenium

We will be using version 3.13, which is the latest version of Selenium at the time of writing this book. It has developed a lot from its early ancestor, Selenium 1. Selenium RC was another tool that would let you write automated web application UI tests in programming languages such as Java, C#, Python, Ruby, and so on, against a HTTP website using any JavaScript enabled browser. For the coding part, we will be working with Java 8. Learning with Java can be fun and at the same time, fast.

What's new in Java 8

Up until Java 7, we only had object-oriented features in Java. Java 8 has added many new features. Some of these features are as follows:

  • Lambda expressions and functional interfaces
  • Default and static methods in interfaces
  • The forEach() method in iterable interfaces
  • The Java Stream API for bulk data operations on collections

Don't worry if you find this intimidating. We will slowly uncover Java 8 as we progress throughout this book.

Lambda expressions and functional interfaces

Lambda expressions are essential in functional programming. Lambda expressions are constructs that exist in a standalone fashion and not as a part of any class. One particular scenario where Lambda expressions can be used is while creating classes which consist of just a single method. Lambda expressions, in this case, help to be an alternative to anonymous classes (classes without names), which might not be feasible in certain situations. We will briefly look at two examples, side by side, of how we can convert a conventional Java snippet into a Lambda expression.

In the following code, we will assign a method to a variable called blockofCodeA. This is just what we are intending to solve with the means of Lambda expressions:

blockofCodeA = public void demo(){ System.out.println("Hello World");
}

The same piece of code can be written using Lambda expressions, as shown here:

blockofCodeA = () -> {                                 
System.out.println("Hello World");
}

Remove the name, return type, and the modifier, and simply add the arrow after the brackets. This becomes your Lambda expression.

Functional interfaces

Functional interfaces contain one—and only one—abstract method. An abstract method is one which should have a body in the implementation class if the implementation class is not abstract. It can have any number of regular methods (methods which have a body in the implementation classes), but the prerequisite of a functional interface is that the number of abstract methods must be only one. These interfaces are used hand-in-hand with Lambda expressions.

In the following code block, the demo method is inside an interface Greeting. Therefore, this interface should only have one abstract method, which is the demo method. In order to instruct other users that this is a functional interface, we annotate this interface with the @FunctionalInterface annotation.

The type of blockofCodeA will be of this functional interface type. This annotation is optional:

@FunctionalInterface
public interface Greeting {
public void demo();
}

Default and static methods in an interface

Up until Java 1.7, it was not possible to define a method inside an interface. Now, 1.8 introduces the default methods through which we can provide implementation for a method inside the interface. Let's see an example of this here:

interface Phone{
void dial();
default void text() {
System.out.println("Texting a message");
}
}

Static methods in Java are those methods that can be invoked without creating an object of a particular class, provided that the static method is in that particular class. In Java 8, static methods can be defined inside an interface, as shown here:

interface Phone {
inx x;
void changeRingtone();
static void text() {
System.out.println("Texting");
}
}
public class PhoneDemo {
public static void main(String[] args) {
Phone.text();
}
}

You can invoke the text() method directly using the name of the interface.

The forEach method for a collection

Starting with Java 8, we can invoke the forEach method on a collection and iterate through the contents of the collection. Let's compare the 7 and 8 versions of iterating over an array list of strings.

The following code, which is from Jave 7, fetches individual fruit names from the fruits list and prints it to the console:


List<String> fruits = Arrays.asList("Apples", "Oranges", "Bananas",
"Pears");
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}

A second alternative that you can use is as follows:

for (String fruit : fruits ){
System.out.println(fruit);
}

The example shown here does the same thing in Java 8 using lambda expressions:

fruits.forEach(i -> System.out.println(i));

Streams in Java 8

As per the Java documentation's definition:

Streams are a sequence of elements supporting sequential and parallel aggregate operations.

Imagine a factory in which workers are standing with tools in their hands, and machine parts keep moving around so that the individual worker can do their part. Streams can be compared somewhat to such a scenario:

List<String> fruits = Arrays.asList("Apples","Oranges","Bananas","Pears");
fruits.stream().forEach(fruit -> System.out.println(fruit));

Understanding Selenium RC

Selenium RC is a popular UI automation library for automating browsers. Selenium RC uses a generic form of JavaScript called Selenium Core to perform automation. However, this should comply with a security policy called the same-origin policy. The same-origin policy is a security measure that prevents website scripts from accessing the scripts of other websites. For example, JavaScript present on Google cannot access or communicate with JavaScript present on Yahoo. Three things are checked for the same-origin policy: the protocol, domain, and port. If these three things match, then only the request can be said as being one from the same domain.

Selenium Core was introduced by Jason Higgins; It was nothing but a JavaScript program. Prior to Selenium RC, IT people had to install both Selenium Core and the entire web application on their local machine to make the virtual appearance as though the requests were coming from the same domain. Selenium RC introduced the RC server, which acted as a HTTP proxy and handled the requests between the web application and Selenium Core.

What is cross-site scripting (XSS)?

Another concept related to same-origin policy is cross-site scripting. Cross-site scripting refers to the situation where a website can be prone to attacks from hackers. A typical hacker injects one or more JavaScript codes into web pages that are being browsed. These JavaScript codes can be malicious, and can pull cookie information from websites, pertaining to be banks, for example. This way, the malicious script bypasses the same-origin policy control.

Selenium RC consists of two parts:

  • Selenium server
  • Client libraries

The following diagram shows the functioning of Selenium RC, where the RC Server sits in-between the libraries like Java and Python and sends instructions to Selenium Core, thereafter operating on the individual browser:

Image modelled from www.seleniumhq.org

The role of the Remote Control Server is to inject the Selenium Core in the respective browser. The client libraries send instructions in the form of requests to the RC Server, and the RC Server communicates this to the browser. After receiving a response, this is communicated back to the user by the RC Server.

Introducing Selenium WebDriver

Selenium WebDriver is used for automating web browsers by using the browser's internal plugins or dll with the individual browser drivers, which are available for each individual browser.

The following diagram shows the high-level functionality of Selenium WebDriver. The JSON API parses the instructions from languages like Java, Python, and so on, and invokes and operates on the concerned browser:

Class structure of Selenium WebDriver

The following diagram is a snapshot of the class structure of Selenium WebDriver. The WebDriver interface is the parent of Remote WebDriver, which is a public class. Drivers for Internet Explorer, Firefox, Chrome, and so on inherit from the Remote WebDriver. In future chapters, we will be digging deep into these drivers:

Drivers in Selenium

We will now take a look at the various drivers that are available in Selenium and their usage.

Remote WebDriver

Remote WebDriver is the implementation class of the WebDriver interface. Apart from WebDriver, it also implements the interfaces of TakesScreenShot, findBy, JavaScriptExecutor, and so on.

Mobile drivers

All modern web apps have implementations for mobile devices. The two most popular operating systems in mobile devices are Android and iOS. Selenium has implementations for Android and iPhone, that is, AndroidDriver and IODDriver. Both of these are direct implementations of WebDriver.

Headless browsers

Headless browsers are those that do not have a graphical user interface (GUI). Everything runs in the background. When a test is executed with a headless browser, no screen is displayed to the user. Two popular headless browsers are HTMLUnit and Phantom JS. Chrome now supports the HTMLUnit browser.

Why do we need headless browsers?

Suppose that Selenium tests have to be executed on an OS which does not have a GUI like Linux or when multiple browser behaviors have to be simulated on just one machine. The advantage of a headless browser is that the resources utilized by the test are minimal. A scenario where you can use these browsers is for test data creation. In these situations, there is no special need to display the screen to the user.

Preparing for the very first script

Follow the steps shown here to get started with Selenium WebDriver.

Installing Java 8

Follow the instructions below to install Java 8:

  1. Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and click on the appropriate version. I have selected the 64-bit Windows version since mine is a Windows machine.
  2. Once the file has downloaded, run the .exe file. Java will start installing it onto your machine. Next, we have to set two environment variables in order to use Java.
  3. Go to Control Panel and click Advanced System Settings.
  4. Click on Environment variables and add two system variables:
    • One is JAVA_HOME. Provide the path of the root folder where Java is installed. In this case, this will be C:\Program Files\Java\jdk1.8.0_152.
    • The second is the Path variable. Remember that this variable has to be appended after adding a ;. Here, the path of the bin folder has to be specified. In this case, this will be C:\Program Files\Java\jdk1.8.0_152\bin.
  5. The next step is to check our configuration. Open the Command Prompt and type java –version:

If you get an output similar to the one shown in the preceding screenshot, you are all set to start coding.

Now, let's get our hands dirty!

Setting up Eclipse

We will be using Eclipse as an IDE for developing Selenium Scripts in this book, but you are free to use whichever IDE suits you best.

Downloading Eclipse

Navigate to the Eclipse website (www.eclipse.org/ide) and click on the Download link. Here, you can find very specific instructions regarding how to install you favourite IDE version (Kepler, Neon, and so on).

Creating a Maven project

Once the IDE is installed, perform the following steps:

  1. Double click on the .exe file for Eclipse and go to File | New | Other.
  2. Select Maven Project. Click Next.
  3. Click Next on the screen that appears.
  4. Select Create a simple project (skip archetype selection). Then, click Next.
  5. Input the Group ID. Ideally, this is the package name of the project. The Artifact ID corresponds to the name of the JAR file in case you want to create one. Keep the packaging as JAR. Notice that the version is 0.0.1-SNAPSHOT. The SNAPSHOT part indicates that the project is still under development and has not been released.
  1. Click Finish. The following is a snapshot of the Project Explorer:

When you create a Maven Project, the src/main/java, src/main/resources, src/test/java, and src/test/resources folders, are created for you. Apart from these, you will see a Maven Dependencies folder that is currently empty. Marked with a black box, there is a .xml file called pom.xml. This is the place where you will place all of the dependencies for your project. By dependencies, I mean dependent JARs. JAR stands for Java archive.

Understanding pom.xml

It's time to explore pom.xml. This is what pom.xml looks like:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.packt.selenium</groupId>
<artifactId>FirstSelenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>

Group ID and Artifact ID that you added in the previous screens have appeared in the preceding file, inside the Project tag. In order to work with Selenium, we will need to add Selenium dependencies within the Project tag. Let's go ahead and add those from the Maven repository:

  1. Go to the Maven repository (https://mvnrepository.com) and grab the dependency shown here:
      <!-- 
https://mvnrepository.com/artifact/org.seleniumhq.selenium/
selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.13.0</version>
</dependency>
  1. Place this dependency inside a dependencies tag, as shown in the pom.xml file here:
      <project xmlns="http://maven.apache.org/POM/4.0.0"    
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.packt.selenium</groupId>
<artifactId>FirstSelenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.seleniumhq.selenium/
selenium-java -->

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>
</project>
  1. Save the pom.xml. You will see a small activity in the bottom-right corner of Eclipse, stating that the project is being built.

The Maven Dependencies folder now gets populated with all of the downloaded JARs, as shown previously.

Manual configuration

With this, we are ready, and have the basic Eclipse setup for Selenium WebDriver. But we are not done yet. It might occur that, under a corporate firewall, you are unable to download the required JARS. In this situation, perform the following steps:

  1. Simply create a plain Java project.
  2. Right -click on the project in Project Explorer.
  3. Select Build Path | Configure Build Path.
  4. Click on Add External JARs and add the required JARs manually.
  5. Next, we will write a very simple script which just opens www.google.com (this is shown in the following section). Right-click the Project and select new class.

Creating the first script

Type the following code. What the following script does is simply opens a new Chrome browser and navigates to the URL http://www.google.com:

public class FirstTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\SeleniumWD\\src\\main\\resources\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}

Right-click the file and click Run as Java Application and hurrah! A chrome browser opens and http://www.google.com gets loaded.

You have successfully created your first Selenium Script.

Summary

This chapter gave you an idea of what Selenium RC and WebDriver are and also touched upon concepts like same-origin policy and cross-site scripting. We also did a basic Eclipse setup using Maven as well as without Maven, and finally we created a very simple program to open a URL in the browser.

In Chapter 2, Understanding the Document Object Model and Creating Customized XPaths, we will learn about the Document Object Model and its various traversal techniques.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

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

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.

Who is this book for?

This book is for software testers or developers.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 30, 2018
Length: 192 pages
Edition : 1st
Language : English
ISBN-13 : 9781789611342
Category :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 30, 2018
Length: 192 pages
Edition : 1st
Language : English
ISBN-13 : 9781789611342
Category :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 94.97
Mastering Selenium WebDriver 3.0
€36.99
Selenium WebDriver Quick Start Guide
€24.99
Selenium WebDriver 3 Practical Guide
€32.99
Total 94.97 Stars icon
Banner background image

Table of Contents

9 Chapters
Introducing Selenium WebDriver and Environment Setup Chevron down icon Chevron up icon
Understanding the Document Object Model and Creating Customized XPaths Chevron down icon Chevron up icon
Basic Selenium Commands and Their Usage in Building a Framework Chevron down icon Chevron up icon
Handling Popups, Frames, and Alerts Chevron down icon Chevron up icon
Synchronization Chevron down icon Chevron up icon
The Actions Class and JavascriptExecutor Chevron down icon Chevron up icon
The Command Pattern and Creating Components Chevron down icon Chevron up icon
Hybrid Framework Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(3 Ratings)
5 star 66.7%
4 star 0%
3 star 0%
2 star 33.3%
1 star 0%
Ramkrishna Bhandare Jan 23, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
value for money
Amazon Verified review Amazon
Ashutosh Rai May 28, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is simple and easy to understand, If you are new in Selenium Automation its a good starting point. If you are already good in Selenium, this book works as a reference book to use the variety of code snippets.
Amazon Verified review Amazon
Abhishek Kar Apr 05, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Not elaborate enough to understand everything clearly
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.