Book Image

Selenium WebDriver Quick Start Guide

By : Pinakin Chaubal
Book Image

Selenium WebDriver Quick Start Guide

By: Pinakin Chaubal

Overview of this book

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.
Table of Contents (10 chapters)

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));