Search⌘ K
AI Features

Page Object Template

Explore how to structure page object classes for Selenium Java tests, including constructors, locator constants, and public and private methods. Discover best practices for organizing page components to keep tests maintainable and clear.

All interactions with the site pages happen in page object classes.

For each web page that is used in the test, a page object class is needed.

We will use a HomePage class for the Home Page and a ResultsPage class for the Results Page.

Both page object classes follow the same template.

Java
public class PageObjectClass
{
private WebDriver driver;
//element locator variables
//page url variable
//page title variable
public PageObjectClass(WebDriver driver)
{
this.driver = driver;
}
//methods that interact with the page
//methods that provide information from the page
}

Page class constructor

Both classes will have a constructor for creating the class’s objects. The constructor gets a driver parameter that is saved in a class variable so the class methods can use it.

Page class variables

Both classes have a driver variable that gets a value in the ...