Structuring your Page Objects


“If you have WebDriver APIs in your test methods, You’re Doing It Wrong.” - Simon Stewart

Simon Stewart, the lead committer of the Selenium Project, shared in his initial vision of Selenium that Page Objects are the mandatory layer on top of the WebDriver. Both of them belong together. Ranorex Webtestit was created with this fact in mind.

In order to use Ranorex Webtestit efficiently, you have to understand the Page Object Pattern.

So what is a Page Object?

The Page Object pattern is a classic example of encapsulation. It wraps the mechanics required to find and manipulate the data in the GUI with an application-specific API. The basic rule of thumb for a Page Object is that it should allow a software client to do and see anything that a human can.

Despite the name, do not make the mistake to create a Page Object for each page! Create them rather for the significant components on a page, such as headers, menus or content regions.


We use our demo shop as an example of how to derive Page Objects.

We can divide the initial start page into at least three Page Objects:

  1. HeaderPo - can be found on every page, so it makes sense to extract this as a Page Object
  2. ItemsOverviewPo - this component contains all the items available for sale
  3. ShoppingCartPo - this shopping cart summary expands out of the header, but it contains enough functionality and elements to make it its own Page Object

Page Object actions

Now that we have our page divided into Page Objects, let’s find out how we can use them in tests.

The Page Object pattern’s intention is to separate the Application and Assertion APIs from the WebDriver API. This means, that tests should never use HTML elements or WebDriver directly, but only consume exposed (public) methods, referred to as Actions.

These actions encapsulate units of user interaction, such as “click on a button”, “log in” or “submit a form”, internally using the WebDriver API and the elements associated with the Page Object.

Therefore, when creating an action method, keep the following best practices in mind:

  1. Never return an element directly, instead:
    • Return values of basic types (strings, integers, dates, …) if you want to work with them.
    • Return the Page Object instance itself (this) when you’re just acting upon elements.
    • Return another Page Object to indicate a context switch, e.g. after clicking on a link, or submitting a form.
    • This allows you to chain Page Object actions together, leading up to a final returned value, that you can then use in your assertion.
  2. Do not use assertions in Page Objects, instead:
    • Return the relevant properties of an element, e.g. its text.
    • Alternatively, pass the comparison values as arguments, and return a boolean.
    • Use these results for assertions in your test files.
  3. Good Page Object actions are reusable. Imagine them as the building blocks for your tests.

Using the Page Objects from above, let’s build a simple test, with the rules we have just learned.
Our test should (1) add one item to the cart, (2) go to the cart, and (3) assert, that the total price is correct.

Note: this example uses Java, but the same principles apply to TypeScript projects.

  1. This action takes place in the ItemsOverviewPo, and adds an item to the cart. Just adding an item to the cart should not navigate us anywhere, and we might want to do more in the ItemsOverviewPo in other tests, so we return this:
    public ItemsOverviewPo addItemToCart() {
        return this;
  2. In the same Page Object, when we click the View cart button, we expect to land on the Cart page, so we return a new instance of the corresponding Page Object class, CartPo:
    public CartPo viewCart() {
        return new CartPo(this.driver);
  3. Finally we are ready to verify that the price is displayed correctly. Do not put assertions into the Page Object action! Instead, we create a method that returns the text of the element holding the total price:
    public String getTotalPrice() {
        String totalPriceText = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.totalPrice)).getText();
        return totalPriceText;
    Now we can use these actions in a test. Note how, thanks to returning Page Object instances in our actions, we can chain the action methods like this, improving readability of the test:
    public void checkTotalPrice() {
        // 1. Arrange
        WebDriver driver = getDriver();
        ItemsOverviewPo itemsOverviewPo = new ItemsOverviewPo(driver)
        // 2. Act
        String totalPrice = itemsOverviewPo
        // 3. Assert
        Assert.assertEquals(totalPrice, "€1,500.00");


After reading this article, you have learned about the Page Object pattern, how to divide a complex web site into individual Page Objects, and how to structure your Page Object actions using best practices.

If you want to know more about Page Objects, be sure to read Martin Fowler’s article about PageObjects.

Getting started with Python and Ranorex Webtestit
Instantiate multiple page objects in 1 test file
listed #3