Imagine you are working on a login page test case in an automation project. At first, everything looks simple. You write a test script, identify each web element, and run the test using Selenium WebDriver. It works perfectly. But as your project grows, things start getting messy.
Now you have:
- Multiple test cases
- Repeated code for the same page elements
- Difficulty in updating locators when the web page of the application changes
Every time there is a small change in the login page, you need to update the same web element in many places. This leads to code duplication, confusion, and wasted time. This is where a better structure is needed in automation testing.
Instead of writing everything inside one test script, we can:
- Create a separate class for each web page
- Store all elements on the page in one place
- Build a clean object repository for web elements
This idea is called the Page Object Model (POM), which is a design pattern used in Selenium. But even with POM, we still need to manually initialize web elements using the webdriver driver. This can again make the code lengthy. To solve this problem, Selenium introduced something called Page Factory.
So, what is Page Factory in Selenium? It is a class provided by Selenium WebDriver that helps to:
- Initialize web elements easily
- Reduce code duplication
- Simplify the implementation of the page object model
In this blog, we will clearly understand:
- What is Page Object Model
- What is Page Factory in Selenium
- How both work together in a Selenium framework
- And how to implement them in a real automation project using Java
Let’s start from the basics and build step by step 👍
Introduction to Selenium Test Automation and Design Pattern in Selenium
Before we understand Page Factory, we need to first understand how Selenium test automation works in a real automation project.
In the beginning, most beginners write all their test code inside a single file. They locate every web element, write actions, and run the test script using the webdriver driver. This may work for small projects, but as the application grows, the test suite becomes hard to manage.
For example:
- The same elements on the page are used in multiple test cases
- Updating one locator requires changes in many places
- The automation framework becomes difficult to maintain
This is why we need a proper structure. This structure is called a design pattern in Selenium.
A design pattern helps us:
- Organize test code properly
- Reduce code duplication
- Make the automation framework easy to maintain
- Improve readability of the test script
One of the most popular patterns in Selenium is the Page Object Model pattern, which we will learn in detail next.
What is Selenium WebDriver in an automation project?
In any automation testing project, the main tool we use is Selenium, and its core component is Selenium WebDriver.
Selenium WebDriver is used to:
- Open a web page of the application
- Identify each web element
- Perform actions like click, type, and submit
- Run the test case automatically
In simple terms, the webdriver driver acts like a real user interacting with the browser.
For example, in a login page test:
- It opens the login page
- Finds the username and password fields
- Enters the data
- Clicks the login button
All this is done through code in a Java class, where we create an object of the driver and perform actions on the page elements.
Why Use Design Patterns in Selenium Framework?
Now think about this situation. You have:
- 20 test cases
- Multiple web pages
- Hundreds of web elements
If everything is written in one place, your test suite becomes very hard to manage. This is where a pattern in Selenium helps.
Using a design pattern:
- We create a separate class for each page
- We store all elements inside the corresponding page class
- We create an object repository to store elements on a page
- We reduce code duplication across test scripts
This makes your Selenium framework:
- Clean
- Easy to update
- Scalable for large projects
So, before moving to Page Factory, the first step is to understand the page object model in Selenium, which is the base for everything.
Struggling to understand Selenium concepts like POM and Page Factory? Learn step-by-step with real-time projects and simple explanations. Join Payilagam, the Best Selenium Training Institute in Chennai.
What is Page Object Model (POM) in Selenium?
Now that we understand why structure is important, let’s learn one of the most important concepts in Selenium.
The Page Object Model (POM) is a design pattern used in automation testing. In simple words, pom is a design pattern that helps us organize test code by creating a separate class for each web page of the application.
Instead of writing all actions inside a test script, we:
- Create a page object class for each page
- Store all web elements inside that class
- Write methods to perform actions on those elements
This means each page of the application will have its own corresponding page class.
So, the object model is a design where:
- Every web page becomes a Java class
- Every web element becomes a variable
- Every action becomes a method
This structure is called the page object model pattern.
By using the page object model, we can:
- Reduce code duplication
- Keep test code clean
- Easily update the page when UI changes
Understanding Page Objects and Page Class in Selenium
Let’s break this down with a simple example. Imagine a login page.
Instead of writing everything in the test script, we:
- Create a page class for the login page
- Store all elements on the page like username, password, and login button
- Build methods like login(), enterUsername(), enterPassword()
This class is called a page object class.
So basically:
- Page objects represent a web page
- Each page class contains all elements within page
- It acts like an object repository for web elements
This way, we:
- Create page objects for each web page of the application
- Keep all related elements in one place
- Avoid repeating the same code in multiple test cases
Advantages of Using Page Object Model in Selenium Framework
Using POM gives many benefits in a real automation framework.
✅ 1. Reduce Code Duplication
We store elements only once in the object repository, so we don’t repeat code in every test case.
✅ 2. Easy Maintenance
If there is any change in the UI:
- We just need to update the corresponding page class
- No need to update every test script
✅ 3. Better Structure
Each page object design keeps:
- Elements
- Methods
- Logic inside a clean, separate class
✅ 4. Reusability
We can reuse the same page object class in multiple test cases within the test suite.
✅ 5. Clear Automation Flow
It becomes easy to understand:
- Which class represents which page?
- What actions are performed on that page?
What is Page Factory in Selenium?
Now we already understand that the Page Object Model (POM) helps us organize our automation code by creating a page object class for each web page. But there is still one small problem.
In POM, we need to manually initialize web elements using the webdriver driver. This makes the code longer and sometimes difficult to manage, especially when there are many elements on the page.
To solve this, Selenium introduced Page Factory. So, what is Page Factory in Selenium?
Page Factory is a class provided by Selenium WebDriver that helps to initialize web elements automatically. In simple words, Page Factory is a Selenium class that acts as an extension of the page object model.
It simplifies the implementation of the page object model by:
- Automatically initializing web elements
- Reducing code duplication
- Making the code clean and easy to read
Instead of writing code to find each web element, Page Factory uses annotations and handles it internally.
So, when we say page factory is an extension, it means:
- It supports the page object model pattern
- It improves how we create page objects
- It makes automation faster and simpler
How Page Factory Improves Page Object Model with Page Initialization?
In normal POM:
- We write code to locate each element
- We manually initialize web elements using the driver
But Page Factory simplifies this process.
With Page Factory:
- We use annotations like
@FindBy - Selenium will initialize web elements automatically
- No need to write extra code for each element
This means:
- Less code inside the page class
- Cleaner structure within page classes
- Easy to maintain when the page changes
It also helps when we need to update the page, because all elements are already handled in a structured way.
Key Features of Page Factory in Selenium WebDriver
Let’s look at what makes Page Factory useful in a real automation project.
✅ 1. Uses Annotations
Page Factory uses annotations like:
@FindByto locate elements
This helps to easily define elements on a page.
✅ 2. Automatic Initialization
Using a factory class like PageFactory.initElements(), Selenium will:
- Initialize page
- Connect elements with the webdriver driver
✅ 3. Supports Object Repository
It helps to create an object repository where:
- All web elements are stored inside the page object class
✅ 4. Cleaner Page Object Design
With using the page factory, the code becomes:
- Short
- Easy to read
- Easy to maintain
✅ 5. Works with Selenium WebDriver
Page Factory is a class provided by Selenium WebDriver, so it works smoothly inside any Selenium framework.
Page Object Model with Page Factory in Selenium
Now we clearly understand both concepts:
- Page Object Model (POM) → gives structure
- Page Factory → simplifies implementation
When we combine both, we get a powerful approach for building a clean and scalable automation framework.
In simple terms:
- POM helps us create page objects
- Page Factory helps us initialize web elements inside those page objects
So, this combination is called an object model with a page factory.
Here:
- Each page object class represents a web page
- All elements on the page are stored inside that class
- Page Factory is used to initialize those elements automatically
This means:
- Better structure
- Less code
- Easy maintenance
That’s why many real-time Selenium test automation projects use both POM and page factory together.
What is the difference between Page Object Model and Page Factory in Selenium?
Many beginners get confused between these two. Let’s understand the difference clearly.
🔹 Page Object Model (POM)
- It is a design pattern in Selenium
- It helps to organize test code
- We manually initialize web elements
- Requires more setup inside the page class
🔹 Page Factory
- It is a class provided by Selenium WebDriver
- It is an extension of the page object model
- It uses annotations and a factory class
- Automatically initialize web elements
👉 In short:
- POM = Structure
- Page Factory = Simplification
When to Use Page Object Model and Page Factory Together?
In a real automation project, using both together gives the best result.
You should use them together when:
- Your test suite has multiple pages
- There are many web elements on each page
- You want to reduce code duplication
- You want a clean object repository for web elements
Using pom and page factory helps you:
- Create a separate class for each page
- Keep all elements within the corresponding page class
- Easily update the page of the application when changes happen
It also makes it easier to:
- Maintain test scripts
- Reuse code
- Scale your Selenium framework
How to Implement Page Factory in Selenium Automation Project?
Now let’s move to the practical part. Till now, we understood:
- What is the page object model pattern?
- How is Page Factory an extension of POM?
Now we will see how to use it in a real automation project.
The main idea is simple:
- Create a separate class for each page
- Store all web elements inside that class
- Use Page Factory to initialize web elements automatically
This helps us build a clean automation framework with less code and better structure.
Steps to Create Page Class Using Page Factory
Let’s understand step by step.
✅ Step 1: Create a Java Class for the Page
Create a Java class for the web page.
Example:
- Login page → LoginPage.java
- This becomes your corresponding page class
Each page of the application should have:
- One class for each page
✅ Step 2: Declare Web Elements
Inside the class:
- Identify all elements on the page
- Store them as variables
Example:
- Username field
- Password field
- Login button
These are your page elements.
✅ Step 3: Use Annotations to Locate Elements
Instead of manually finding elements, we use annotations. Page Factory uses annotations like:
@FindBy
This helps to define:
- Each web element
- Its locator is inside the web page of the application
✅ Step 4: Initialize Page Using Page Factory
Now comes the important part. Page Factory uses a factory class to initialize elements. We use:
PageFactory.initElements()
This will:
- Initialize page
- Connect all elements with the private webdriver driver
- Prepare the class to be used in test scripts
So we don’t need to manually initialize web elements anymore.
✅ Step 5: Create Methods for Actions
Inside the same class:
- Write methods for actions?
Example:
enterUsername()enterPassword()clickLogin()
This completes the implementation of the page object.
✅ Step 6: Use the Page Class in Test Script
In your test script:
- Create an object of the page class
- Call methods to perform actions
This helps to:
- Make test code clean
- Reuse the same page object in multiple test cases
Example Code to Implement the Page Object Model with Page Factory (Java)
Let’s look at a simple example of a login page.
🔹 Page Class (LoginPage.java)
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
// page object class
public class LoginPage {
// private webdriver driver
WebDriver driver;
// constructor to initialize page
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this); // initialize web elements
}
// elements on the page using annotations
@FindBy(id = "username")
WebElement username;
@FindBy(id = "password")
WebElement password;
@FindBy(id = "loginBtn")
WebElement loginButton;
// methods to interact with page elements
public void enterUsername(String user) {
username.sendKeys(user);
}
public void enterPassword(String pass) {
password.sendKeys(pass);
}
public void clickLogin() {
loginButton.click();
}
}
🔹 Test Script
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
// create an object of page class
LoginPage page = new LoginPage(driver);
// make test using page object
page.enterUsername("testuser");
page.enterPassword("password123");
page.clickLogin();
}
}
Using POM and Page Factory in Selenium Test Suite
Now that we have implemented Page Factory, let’s understand how it works in a real test suite.
In a real automation project, we don’t have just one test case. We have:
- Multiple test cases
- Multiple web pages
- Many page elements
If everything is not structured properly, the test code becomes difficult to manage. This is where using the page object model with page factory becomes very useful.
In a proper selenium framework:
- Each page of the application has its own corresponding page class
- Each class acts as an object repository for web elements
- Page Factory is used to initialize web elements automatically
So in a test suite:
- Test scripts will only call methods from the page classes
- All logic related to elements stays inside the page object class
This helps us:
- Keep test scripts clean
- Avoid code duplication
- Easily update the page when UI changes
How Page Factory Supports a Scalable Selenium Framework?
Let’s understand how this helps in scaling your automation framework.
✅ 1. Clear Separation of Code
- Test logic → inside test scripts
- Page logic → inside page classes
Each separate class handles its own responsibility.
✅ 2. Centralized Object Repository
All elements on a page are stored in one place:
- Inside the corresponding page class
- Acts as an object repository to store web elements
So when you need to update the page, you only update one class.
✅ 3. Easy Maintenance
In real projects, UI changes are common.
With this approach:
- You update only the corresponding elements on the page
- No need to change every test script
✅ 4. Reusable Page Objects
We can:
- Create page objects once
- Use them in multiple test cases
This reduces code duplication and improves efficiency.
✅ 5. Better Readability of Test Code
Using Page Factory:
- Test scripts look simple
- Easy to understand even for beginners
Example:
- Instead of writing element locators, we just call methods
✅ 6. Supports Large Automation Projects
When your project grows:
- More pages → more page object class
- More test cases → more test scripts
Still, everything stays organized because:
- The object model is a design pattern
- Page Factory supports the implementation of the page object model
👉 In simple words:
POM gives structure, and Page Factory makes it easy to use that structure in large projects.
Advantages and Limitations of Page Factory in Selenium
Now we clearly understand how the page factory in Selenium works and how it supports the page object model pattern. Let’s look at the real benefits and also some limitations you should know before using it in an automation framework.
Advantages of Page Factory in Selenium
🔹 1. Automatic Initialization of Web Elements
One of the biggest advantages is that Page Factory:
- Automatically initialize web elements
- Uses a factory class provided by Selenium WebDriver
So we don’t need to manually write code to find each web element.
🔹 2. Reduces Code Duplication
By using the page factory:
- All elements on the page are stored in one page object class
- We avoid repeating the same code in every test script
This helps to reduce code duplication and keep the code clean.
🔹 3. Cleaner Page Object Design
Page Factory improves the page object design by:
- Using annotations
- Keeping code simple inside page classes
This makes the implementation of the page object easy to understand.
🔹 4. Supports Object Repository
It helps to create an object repository:
- Where all elements on a page are stored
- Inside the corresponding page class
This makes it easy to manage the web page of the application.
🔹 5. Easy Maintenance
When there is any UI change:
- You just need to update the corresponding elements on the page
- No need to update every test case
This is very useful when you need to update the page frequently.
🔹 6. Works Well with Selenium Framework
Page Factory is a class provided by Selenium WebDriver, so it:
- Easily fits into any Selenium framework
- Supports selenium test automation projects
Limitations of Page Factory in Selenium
Even though Page Factory is useful, it also has some limitations.
🔸 1. Not Ideal for Complex Applications
In very large or complex applications:
- Managing many page elements with annotations can become difficult
🔸 2. Less Control Over Element Initialization
Since Page Factory automatically initializes web elements:
- We have less control compared to manual handling
🔸 3. Synchronization Issues
Sometimes:
- Elements may not load properly
- It can cause issues in test execution
So we may still need:
- Wait conditions
- Additional handling in test code
🔸 4. Slight Learning Curve for Beginners
For beginners:
- Understanding annotations
- Understanding how Page Factory works internally may take some time.
Final Take on This Section
Page Factory is not mandatory, but it is very helpful when:
- You are working with multiple pages
- You want a clean object repository for web elements
You want to simplify the implementation of the page object model
Conclusion: Why Use Page Factory in Selenium Test Automation?
Now, let’s quickly connect everything we have learned.
At the beginning, we saw the problem:
- Repeated test code
- Difficult to manage web elements
- Too much code duplication in test scripts
To solve this, we used the page object model pattern, where:
- Each web page of the application has a separate class
- All elements on the page are stored in one place
- We built a clean object repository for web elements
Then we improved it further using Page Factory.
So, what is Page Factory in Selenium? It is a class provided by Selenium WebDriver that simplifies the implementation of the page object model by automatically initializing web elements.
By using the page factory, we:
- Reduce code duplication
- Easily initialize web elements
- Keep the code clean inside page classes
- Improve the overall automation framework
In real projects, this combination:
- Makes test scripts simple
- Helps teams maintain large test suites
- Supports scalable Selenium test automation
👉 In simple words:
- POM organizes your code
- Page Factory simplifies your code
If you are building or working on an automation project, using both together will help you create a clean and efficient structure.
Final Thoughts
Learning automation testing is not just about understanding concepts like Page Object Model or Page Factory. The real value comes when you actually use them in a project and see how they make your test scripts simple and easy to manage. Many learners struggle because they only focus on theory. But in real projects, you need to know how to handle web elements, organize your test code, and work with a proper automation framework.
Payilagam, the Best Software Training Institute in Chennai, the focus is more on practical learning. You will work on real examples like login pages and complete workflows, so you understand how things actually work in a company environment. If you are searching for the Best Selenium Training in Chennai, choosing the right place can make a big difference. With proper guidance, hands-on practice, and placement support, you can confidently move into automation testing.
Keep practicing, keep building projects, and step by step, you will become strong in Selenium 👍
Start your journey with Payilagam, the Best Selenium Training Institute in Chennai.
FAQs on Page Factory in Selenium
What is Page Factory in Selenium?
Page Factory in Selenium is a class provided by Selenium WebDriver that helps to initialize web elements automatically. It is an extension of the Page Object Model and uses annotations like @FindBy to manage elements inside a page object class. This makes the test code simple and reduces manual work.
Explain the difference between POM and Page Factory in Selenium?
The Page Object Model (POM) is a design pattern used to organize test code by creating a separate class for each web page. Page Factory, on the other hand, is a class that helps to implement POM easily by automatically initializing web elements. In simple terms, POM provides structure, while Page Factory simplifies that structure.
Why use Page Object Model in Selenium?
We use the Page Object Model in Selenium to reduce code duplication and improve code structure. It helps to store all web elements in one place, making it easier to maintain test scripts. When there is a change in the UI, we only need to update the corresponding page class instead of multiple test cases.
How to implement Page Factory in Selenium?
To implement Page Factory in Selenium, you need to create a page class for a web page, declare web elements using annotations like @FindBy, and initialize them using PageFactory.initElements(). Then, you create methods to perform actions and use that page class in your test script.
Is Page Factory still used in Selenium automation projects?
Yes, Page Factory is still used in many Selenium automation projects, especially for small to medium applications. However, in some modern frameworks, testers prefer custom approaches for better control. Still, Page Factory is useful for beginners and learning automation concepts.
What are the advantages of using POM and Page Factory?
Using POM and Page Factory together helps to create a clean and organized automation framework. It reduces code duplication, improves readability, and makes maintenance easy. It also allows reusing page objects across multiple test cases, which saves time and effort in large projects.

