When to create a new page object
Create a new page object class whenever a step definition needs to interact with a part of the UI that is not already modelled. Good rules of thumb:- One page object per distinct page or modal (e.g.,
RiuHome,LoginModal,SearchResults) - If two scenarios share the same UI elements, they should share the same page object
- Do not put locators or
drivercalls directly inside step definitions — always go through a page object
How BasePage works
All page objects extend utilities.BasePage. BasePage holds the WebDriver instance and a WebDriverWait configured with a 10-second timeout. Every Selenium interaction in the project goes through one of its helper methods, which means explicit waits are applied automatically:
| Method | What it does |
|---|---|
find(By locator) | Waits until the element is visible, then returns it |
click(By locator) | Waits until the element is clickable, then clicks it |
type(By locator, String text) | Clears the field and types the given text |
getText(By locator) | Returns the visible inner text of the element |
isDisplayed(By locator) | Returns true if the element is visible; false otherwise |
isClickable(By locator) | Returns true if the element is clickable within the timeout |
isChecked(By locator) | Returns true if a checkbox or radio button is selected |
locatorText(By locator) | Alias for getText |
navigateTo(String url) | Calls driver.get(url) |
false inside isDisplayed, isClickable, and isChecked, your step assertions stay clean without extra try/catch blocks.
Creating a new page object
Create the class file
Add a new
.java file under src/test/java/pages/. Follow the naming convention: <PageName>.java.Add the constructor
The constructor must accept a
WebDriver argument and forward it to BasePage:super(driver) sets this.driver and creates the WebDriverWait instance that all helper methods use.Define locators as private fields
Declare every element locator as a
private final By field at the top of the class. Keep them together so they are easy to maintain:Complete example
The following is a full page object modelled on the existingRiuHome.java pattern:
src/test/java/pages/SearchResults.java
Using the page object in a step definition
Instantiate the page object inside the relevant step method by passingcontext.driver. TestContext is injected into every steps class by PicoContainer:
src/test/java/steps/SearchSteps.java
You do not need to register the page object anywhere else. PicoContainer handles
TestContext injection automatically — as long as your steps class accepts TestContext in its constructor, the shared WebDriver is available.