Page Object Model vs Page Factory: What Should You Use?

             Page Object Model vs Page Factory             What Should You Use?

pomPagefactory


Should You Prefer Page Object Model or Page Factory?

As you begin your learning in automation testing, you may have heard the familiar words Page Object Model and Page Factory. Don’t feel confused if these concepts are technical at the start. I’ll make these concepts easier to understand in this article. If you are going to interviews or starting to build your first framework, this guide will explain the difference and suggest the correct approach.
Let’s get started, just like we would over a cup of chai and a conversation about automation!

🌟 What is Page Object Model (POM)?

Think of a website. Every page you visit — login, signup, dashboard — is a separate screen. Now imagine you're writing test scripts to interact with these pages. Instead of writing the same code again and again, you create a model (or class) for each page. That’s what Page Object Model is all about.
You should create classes in your code for every web page. All the page features (including buttons, text boxes and so on) are referred to as variables. Anything you can do on that page (such as clicking login or supplying your username) is called a method.

📄 Simple Example in Java:

public class LoginPage {
private WebDriver driver;
    
private By username = By.id("username");
private By password = By.id("password");
private By loginButton = By.id("loginButton");

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

public void enterUsername(String user) {
driver.findElement(username).sendKeys(user);
}

public void enterPassword(String pass) {
driver.findElement(password).sendKeys(pass);
}

public void clickLoginButton() {
driver.findElement(loginButton).click();
}
}


Advantages of Using POM

  • Makes your code clean and easy to read.
  • Reduces repetition — reuse the same code in multiple tests.
  • If your website changes (like the login button ID), you only need to update it in one place.

When I first started using Selenium, I didn’t know about POM. I used to copy-paste the same login code  in every test case. It was tiring and messy! Once I switched to POM, everything became so much more organized.

🔧 What is Page Factory?

Page Factory is like an upgrade to Page Object Model. It’s still based on the same concept, but it adds some smart features to make things even easier.
In Page Factory, we don’t need to write By.id or findElement() again and again. Instead, we use annotations like @FindBy. These annotations tell Selenium where to find the element, and Selenium takes care of the rest.

📄 Same LoginPage using Page Factory:

public class LoginPage {
private WebDriver driver;

@FindBy(id = "username")
private WebElement username;

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

@FindBy(id = "loginButton")
private WebElement loginButton;

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

public void enterUsername(String user) {
username.sendKeys(user);
}

public void enterPassword(String pass) {
password.sendKeys(pass);
}

public void clickLoginButton() {
loginButton.click();
}
}

🚀 Advantages of Page Factory

  • Code looks cleaner and more compact.
  • You don’t need to manually locate elements — it happens automatically.
  • It supports something called lazy initialization — meaning it waits until the element is needed.

🤔 POM vs Page Factory: What’s the Difference?

Feature

Page Object Model (POM)

Page Factory

Element Declaration

Manual using By locators

Annotations using @FindBy

Initialization

You find elements inside methods

Automatically done by PageFactory

Readability

Moderate

High

Performance

Normal

Slightly better due to lazy init

Learning Curve

Easier for beginners

Slightly more advanced


🚫 Common Mistakes to Avoid

  • Not separating logic: Don’t put test logic inside page classes. Keep your tests and page objects separate.
  • Hardcoding data: Always try to use external test data if possible.
  • Skipping waits: Use proper waits if needed. Page Factory helps, but some dynamic elements may still need manual handling.

😊 Personal Tip

Start with plain POM when you're learning. It's simpler to understand. Once you’re comfortable, move on to Page Factory for a cleaner and more professional framework.
When I introduced Page Factory in one of my projects, my team was amazed at how short and clear the code became. We spent less time fixing locator bugs and more time improving our tests.

💼 Which One Should You Choose?

  • If you're a beginner, go for POM to understand the basics.
  • If you're building a real-world project, or joining a company that uses Selenium, you’ll likely need Page Factory.
In the end, both are great options. They’re like two paths leading to the same destination — clean, maintainable, and readable test code.

📢 Share and Learn Together

Did you find the article useful? Did it help you? If you are studying test automation with others, share the tips with them. Send it to your LinkedIn network, Friends on WhatsApp or use Slack to share it with your community.

Cheers to happy testing!

Previous Post Next Post