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
Arrow up icon
GO TO TOP
Selenium WebDriver Quick Start Guide

You're reading from   Selenium WebDriver Quick Start Guide Write clear, readable, and reliable tests with Selenium WebDriver 3

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781789612486
Length 192 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Pinakin Chaubal Pinakin Chaubal
Author Profile Icon Pinakin Chaubal
Pinakin Chaubal
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Introducing Selenium WebDriver and Environment Setup FREE CHAPTER 2. Understanding the Document Object Model and Creating Customized XPaths 3. Basic Selenium Commands and Their Usage in Building a Framework 4. Handling Popups, Frames, and Alerts 5. Synchronization 6. The Actions Class and JavascriptExecutor 7. The Command Pattern and Creating Components 8. Hybrid Framework 9. Other Books You May Enjoy

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));
You have been reading a chapter from
Selenium WebDriver Quick Start Guide
Published in: Oct 2018
Publisher: Packt
ISBN-13: 9781789612486
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image