Selenium: Diff between POM (page object model) and page factory?

0 votes

Seems like Page object model & page factory are doing similar things. 

IpmObjectInitializer initialize = new IpmObjectInitializer(driver.getWebDriver());

By using page factory: Initialize elements in BatchCreationPageFactory class

batchCreationPageFactory = initialize.getBatchCreationPageFactoryObj();
Apr 17, 2018 in Selenium by kappa3010
• 2,020 points
2,960 views

2 answers to this question.

0 votes

Page Object Model is a design pattern to create an Object Repository for web UI elements.
However, Page Factory is a built-in class in Selenium for maintaining object repository. For that, we can import the package: Page Factory.

public class LogInPage
{
    private WebElement usrnm;
    private WebElement pwd;

    public LogInPage() {
    }

    public void locateElements() {
        usrnm = driver.findElement(By.id("userName"));
        pwd = driver.findElement(By.id("password"));
    }

    public void doLogIn() {
        usrnm.sendKeys("qwe");
        pwd.sendKeys("123");
    }
}

With Page Factory, initElement() statement can be used for easily looking up elements in page class.
Page Factory allows storing of page elements in cache memory using @CacheLookup annotation
So, which ever methods we have defined in a diff class, those can be imported by using the page factory library.

public class LogInPage
{
    @FindBy(id="userName")
    private WebElement usrnm;

    @FindBy(id="password")
    private WebElement pwd;

    public LogInPage() {
        PageFactory.initElements(driver, this); // initialize the members like driver.findElement()
    }

    public void doLogIn() {
        usrnm.sendKeys("qwe");
        pwd.sendKeys("123");
    }
}
answered Apr 18, 2018 by king_kenny
• 3,650 points
0 votes

Hi,

In POM (Page Object Model) you create new class for every new page/URL and put all your element identification methods in it.

Eg.

If you navigate to my Github - OrangeHRM automation sample project, you will notice that I have two .java files there. Dashboard and Login. So whatever element I identify on Login page, I put in Login.java and whatever elements I identify on Dashboard, I put it in Dashboard.java. This is really helpful in terms of maintenance and usability. So, if I want to work with any element present on Dashboard, I just have to say Dashboard.element. If case of change of element, I just have to change it at one place.

Page Factory is annotation inside selenium which you give it to class. So, when you define any class as page factory, all your elements inside the class will get loaded while any of element in the class is called. This is really a bad practice if you have 100s elements inside a class and you just have to make use of only one.

So, I would suggest you to go with POM. I do not have C# example, but I have similar one in Java which I have already shared.

Click here to read more

answered Mar 14 by Ellen Dares

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

What's the diff between Selenium RC and Selenium WebDriver?

RC works by injecting the JavaScript functions ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
217 views
0 votes
1 answer

What is Page Object Model in Selenium WebDriver?

If you want to learn about Selenium ...READ MORE

answered Jun 14, 2018 in Selenium by Meci Matt
• 9,420 points
155 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
916 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,102 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
835 views
0 votes
1 answer

What is the difference between thoughtworks.selenium and openqa.selenium selenium?

thoughtworks.selenium is the original Selenium (aka Selenium 1, ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
253 views
0 votes
1 answer

Diff between WebDriver listeners and TestNG listeners?

This is not a very important concept ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
1,762 views