How to implement Page Object Model (POM) in Selenium?

What is Page Object Model?

It is a design pattern frequently used with test automation frameworks. It helps in reducing code duplication, increases reusability and reduces maintenance efforts. It contains different page classes which contains web elements and methods to interact with web elements. Each page class represents a page or feature in the application.


If an application has 3 pages – Login, Home and Register Page. Then we can create 3 different Page Object classes with all web elements and methods in each page as shown below:

Advantages of POM:

1. Code is much cleaner and optimized.

2. Complete abstraction of internal code from the tests.

3. Methods and objects can be reused across different tests.

4. No code duplication.

5. Maintenance efforts are much lesser compared to other frameworks.

6. It can act as an Object Repository for Automation Framework.

How to implement Page Object Model in project?

Lets take an sample login test script written in Selenium Java which has the following steps:

– launches a web application

– enters username and password

– click on submit button

public void LoginTest {
    public static WebDriver driver;
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Implementation with POM:

1. Create a new class with the following webelements and methods:

public class LoginPage {

    public static WebDriver driver;
    public static String txtUsername = "//*[@id='username']";
    public static String txtPassword = "//*[@id='password']";
    public static String btnSubmit = "//*[@id='submit']";

    public static void navigateToHomePage(String url){
        driver = new ChromeDriver();

    public static void enterUsername(String username){

    public static void enterPassword(String password){

    public static void clickSubmit(){


2. Modified Login test script

    public void loginTest(){

As we can see, our test script code is  very optimized and abstracted from the underlying code. All the logic is written within the LoginPage class and the test script only contains calls to the reusable methods.

Every time there is any change to any web element identifier or methods, we need to only change it in the Page Object classes and our test script remains unaffected.

Next: Watch this video on how to setup POM in Cucumber Selenium project – Click Here



Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

Leave a Reply

Your email address will not be published.