Book Image

Selenium Essentials

By : Prashanth Sams
Book Image

Selenium Essentials

By: Prashanth Sams

Overview of this book

Table of Contents (12 chapters)

The PageObject pattern


PageObject is an approach widely used in testing to reduce code duplication and increase the reusability of code. It is a design pattern that defines a page using objects. Moreover, the page object provides easy maintenance of code, and the scripts can be read easily anytime by any user. It is a pattern representing an entire page or a portion of the page in an object-oriented behavior.

Let's discuss the PageObject design pattern with an example on the Google web page. To start with the example, let's create a class that emphasizes how to write PageObject methods for a page in detail (GoogleSearchPage.java). The search() and assertTitle()methods let you perform Google searches and to assert the page title on the results page. Here's how we create the aforementioned class for this example:

public class GoogleSearchPage {
  public WebDriver driver;
  private final Wait<WebDriver> wait;

  public GoogleSearchPage(WebDriver driver) {
    this.driver = driver;
    wait...