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.

Example:

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

@Test
public void LoginTest {
    public static WebDriver driver;
        System.setProperty("webdriver.chrome.driver","C:\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://dbankdemo.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.findElement(By.id("username")).sendKeys("test");
        driver.findElement(By.id("password")).sendKeys("test123");
        driver.findElement(By.id("submit")).click();
}

Implementation with POM:

1. Create a new class LoginPage.java 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){
        System.setProperty("webdriver.chrome.driver","C:\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(url);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

    }
    public static void enterUsername(String username){

        driver.findElement(By.xpath(txtUsername)).sendKeys(username);
    }
    public static void enterPassword(String password){

        driver.findElement(By.xpath(txtPassword)).sendKeys(password);
    }
    public static void clickSubmit(){

        driver.findElement(By.xpath(btnSubmit)).click();
    }
}

2. Modified Login test script

@Test
    public void loginTest(){
        LoginPage.navigateToHomePage("http://dbankdemo.com");
        LoginPage.enterUsername("test");
        LoginPage.enterPassword("test123");
        LoginPage.clickSubmit();
    }

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

 

Share

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. Required fields are marked *

error: Content is protected !!