Book Image

Learn Selenium

By : UNMESH GUNDECHA, Carl Cocchiaro
Book Image

Learn Selenium

By: UNMESH GUNDECHA, Carl Cocchiaro

Overview of this book

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 learn to easily perform end-to-end testing on web and mobile browsers. You'll begin by focusing on 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. Next, 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 also strategize and handle a rich web UI using the advanced WebDriver API, and learn techniques to tackle real-time challenges in WebDriver. Later chapters will guide you through performing different types of testing, such as cross-browser testing, load testing, and mobile testing. Finally, you will 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 books: • Selenium WebDriver 3 Practical Guide - Second Edition by Unmesh Gundecha • Selenium Framework Design in Data-Driven Testing by Carl Cocchiaro
Table of Contents (25 chapters)
Title Page

Extracting JSON data into Java objects

Now that the basic syntax has been covered, we will start building the JSON DataProvider method. First, we need a file I/O method to read the JSON data from a file. The parameter to the method will be the filename, including the path and string type. The method will be static and return JSONObject. Here is the code sample:

/**
* extractData_JSON - method to extract JSON data from a file
*
* @param file (including path)
* @return JSONObject
* @throws Exception
*/
public static JSONObject extractData_JSON(String file) throws Exception {
FileReader reader = new FileReader(file);
JSONParser jsonParser = new JSONParser();

return (JSONObject) jsonParser.parse(reader);
}

In cases where users might want to extract only specific sets of JSON data, as when filtering for specific test cases, they could create a wrapper method around the extractData_JSON...