Skip to content

Page Object Model

Ben Weese edited this page Jun 13, 2019 · 2 revisions

So what is a POM or a Page Object Model. When I first heard of POM I thought I was already using it via Maven with my pom.xml. Well Maven is using a POM but it is to pull in dependancies. With Selenium we use it to define our elements so they are easily accessible and reusable. Below is an example POM with some code taken from our project.

Example

import org.openqa.selenium.By;

public class complicatedPOM {

    By button = By.className("et_pb_button_0");
    By capName = By.id("et_pb_contact_name_0");
    By capAnswer = By.cssSelector("div.et_pb_contact_form_0 div.et-pb-contact-message p");

public complicatedPOM(WebDriver driver){
        this.driver = driver;
    }

What you do is create a bunch of By elements that can be used in your main code to call the element without having to reenter the same selector over and over.

Implementation

public class complicatedPage {

    private WebDriver driver;
    private complicatedPOM comPOM;
   
    @Before
    public void beforeSenario(){
        ...
        comPOM  = new complicatedPOM(driver);
        ...
        WebElement button = driver.findElement(comPOM.button);
        Assert.assertTrue(button.isDisplayed());
    }

So we don't need to enter the By.className("string") we can instead use the By element and if the string changes we can change it in our POM instead of in the page code. This makes everything more manageable.

Page Object Factory(Alternative)

Page Factories are less widely used and are said by their creator to not be that great. So instead of a bunch of By objects we are now passing full WebElements that are initialized when when the page is called. The issue that comes down is if the element can't be found it will not error out. Instead it will create a fake one to test against, then your test will error and not your code. However the Factory has more functionality and used correctly can be very awesome.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class complicatedPOF {
	WebDriver driver;
	
	@FindBy(className="et_pb_button_0")
	WebElement button;
	
	@FindBy(id="et_pb_contact_name_0")
	WebElement capName;

	@FindBy(cssSelector="div.et_pb_contact_form_0 div.et-pb-contact-message p")
	WebElement capAnswer;

        public complicatedPOF(WebDriver driver) {
		this.driver = driver;
		PageFactory.initElements(driver, this);	
	}

So we create the whole element which makes calling it later very easy. You can even now right inside the Factory click functions with the element for navigation, your own version of sendkeys and various other things that can call an element directly just by creating a function instead.

Implementation

import complicatedPOF;

public void beforeScenario() {
		...
		complicatedPOF comPom = new complicatedPOF(driver);
                ...
                Assert.assertTrue(comPom.button.isDisplayed());

Less lines and less code.

Clone this wiki locally