Friday, 25 March 2016

Page object models in Selenium Webdriver frameworks

There are various frameworks that can be used in Automation as mentioned

One of them is Page object model which is specially used in Selenium Automation.

Major features of Page Object models are
  1. It's a object oriented framework.
  2. All application pages are modeled as Page classes. Each Page class has fields (Elements on the page). Each Page class has methods that get or set the values for the elements. This helps in the maintenance of the project as elements are not duplicated.
  3. Step classes are created which can use multiple page models to  perform specific operation like login, book a ticket, cancel a ticket, log out etc.
  4. Test classes contain test methods which use Step classes to automate the test flow. From test class, we pass on the input data to steps classes and steps classes pass that data to page classes.
Architectural diagram of Page Object models

Example in Java - Page Object models

Just imagine that you have a website and you want to design the Page object model framework for that website.

Steps to create page object models -
  1. Identify all pages in the application and create a class for each page.
  2. Identify elements and create methods to access to those elements.
  3. Write the Step class to use one or multiple Page classes. This step class is optional as We can access the Page members directly from Tests by instantiating the Page classes.
  4. Then write the Test classes that uses steps to develop the test case.
For example - Consider this Selenium Test page 

So sample page class for this page will look like below.

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import pages.common.BasePage;

public class SeleniumTestPage extends BasePage {

    private String firstName="//td[contains(text(),'First Name')]" +

    public SeleniumTestPage(WebDriver driver){

    public void setFirstName(String name){

    public String getFirstName(){
    return driver.findElement(By.xpath(firstName)).getAttribute("value");


We can create instances of this class from our test and then perform any operation on that page.

package page_object_model_tests;

import org.junit.Assert;
import org.junit.Test;
import page_object_model_tests.BaseTests.BaseTest;
import pages.SeleniumTestPage;

public class SamplePageTests extends BaseTest {

    @Test    public void testPage() {
     SeleniumTestPage seleniumTestPage = new SeleniumTestPage(driver);
     String name = seleniumTestPage.getFirstName();
     Assert.assertTrue("Name validation",name.equalsIgnoreCase("Sagar"));

Above test will create the object of Page - SeleniumTestPage and then use it's methods to interact with elements on the page.
Below code shows the BaseTest class. All tests inherit from this class.

package page_object_model_tests.BaseTests;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;

public class BaseTest {
    protected static WebDriver driver ;
    @BeforeClass    public static void init() {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    @AfterClass    public static void clean() {

Below code shows the BasePage class. All pages inherit from this base page class.

import org.openqa.selenium.WebDriver;

/** * Created by Sagar on 26-03-2016. */public class BasePage {
    protected WebDriver driver;

    public BasePage(WebDriver driver){
        this.driver = driver;

    //Common methods will be put here

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at

No comments:

Post a Comment

Buy Best Selenium Books