Abstracting Web Page Interaction

Page Objects is the recommended pattern to abstract the Selenium behaviour behind pages that specify the user interaction in more meaningful ways. The page objects is where the Selenium magic happens and this is where we need to commit to specific API, either Selenium API or WebDriver API.

The Pages object is typically a factory class that allows users to easily get hold of the objects for each web page, and can be injected into the JBehave Steps classes. The advantage of having a pages' factory class is that even as the number of page objects grow the dependencies at the Steps class level still remains the same. Typically, the implementation of a step requires interaction with more than one page.

Using Selenium API

The Pages object is injected with the Selenium API-specific objects:

Each page extends the SeleniumPage, which provides a facade to the Selenium API. E.g.:

Here AbstractPage is actually the class extending SeleniumPage and providing some methods common to all pages.

Using WebDriver API

The Pages object is injected with the WebDriver API-specific objects:

Note that we inject a WebDriverProvider rather than a WebDriver instance. This allows the use to inject different strategies to provide WebDriver instances to the pages. For example, we have use a DefaultWebDriverProvider, which provides vanilla FirefoxWebDrivers, or PropertyWebDriverProvider, which provides WebDriver instances based on the system property browser, which must match (case-insensitively) one of the values of Browser enum.

Each page extends the WebDriverPage, which provides a facade to the WebDriver API. E.g.:

Here again AbstractPage is actually the class extending WebDriverPage and providing some methods common to all pages.