Page Object Model using Selenium and Java

Explanation and examples

Page Object Model is a design pattern.

Pages are essentially your classes and within these classes are the routes to elements and your methods. These routes/ methods live outside the test. They are accessed by the test when an instance of the class is created within the test and it’s methods are subsequently called. This will become clearer with the examples below but the basic advantages and idea of POM is that the code is easier to maintain, plus it makes for more readable code which is easier to reuse.

Below I’ll show you a couple of examples. The code is testing the login function on a Wordpress site.

Testing Login (First Example)

In all of these examples I have a class (i.e. page) which contains the routes to the elements on the page that I want to interact with and the methods which trigger this interaction.

In this first example I have my LoginPage class (the page) and VerifyWordpressLogin class with a test method verifyValidLogin().

In the LoginPage class I’m using the By class to write the route to an element and I’m ascribing a variable to it. We then need to create a constructor and pass the driver to it as a parameter, so that we can instantiate it later in our tests. We then have our methods which reference the routes e.g. findElement(username), findElement(password) which we then pass a string to in the method itself.

Login Class

Below is the class VerifyWordPressLogin with the test method verifyValidLogin(). Here we’re creating an instance of the LoginPage class i.e. login, passing the driver to it and calling the methods to verify they’re working correctly.

VerifyWordPressLogin class (which is the test)

Testing Login(Second Example)

This example is similar to the first. We have the same classes, but this time in LoginPage1 we’re passing in data through the parameters of the methods by assigning a variable to it e.g. TypeUserName(String userid), TypePassword(String pword). The actual string is then added in the test, when we call the method e.g:


In reality, you might not do this, as you could be pulling in data from an excel file or something similar.

LoginPage1 — passing data in through the parameters of the method

Here in the test method verifyValidLogin() I am passing in the actual username and password in the userid and pword placeholders.

VerifyWordPressLogin1 class (test)

Testing Login (Third Example)

This is just a streamlined version of the other two examples. This time we’re bunching all of the methods into an overarching one e.g.:

public void VerifyLoginAllInOneMethod(String userid, String pword)

It makes sense to bunch functionality into chunks like this, so it becomes easier to reuse and to follow the output of your tests.

LoginPage2 with just the one method

Here you can see that we’re only calling the one method.

VerifyWordPressLogin2 class (test), calling the one method

One more note none of the examples have any kind of verification in, which you would need for this to be a meaningful test. It would make sense to do the following here;

  • Verify page title
  • Verify error messages
  • Verify successful login

Further thoughts

Since writing this post I’ve discovered a different way to write how you locate your objects/ elements which uses @FindBy. Basically it’s like this;

@FindBy (name =’q’)
public WebElement TxtSeach;

or in C#

[FindsBy(How = How.Name, Using = “q”)]
public IWebElement TxtSearch;

This is explained in a bit more depth in the post Automation Framework Design and Development which has a section where I talk about implementing the Page Object Model in Java and C#. Please take a look at it.

That’s it for now. I will have a go at doing the verification mentioned above and post it here at a later date.

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.