As automation projects grow, maintaining test scripts becomes increasingly difficult. One of the most effective ways to improve maintainability and readability is by using the Page Object Model (POM) design pattern.
In this tutorial, you'll learn how to build a Playwright Page Object Model framework using Java.
What is Page Object Model?
Page Object Model is a design pattern where each application page is represented by a separate Java class.
Instead of writing locators and actions directly in test cases, they are stored inside page classes.
This provides several benefits:
- Better code organization
- Improved reusability
- Easier maintenance
- Reduced code duplication
- Cleaner test scripts
Project Structure
A typical Playwright POM framework structure looks like this:
src
├── main
│ └── java
│ ├── pages
│ │ ├── LoginPage.java
│ │ └── HomePage.java
│ └── utils
│
└── test
└── java
└── tests
└── LoginTest.javaStep 1: Create the Login Page Class
Create a new class called LoginPage.java.
package pages;
import com.microsoft.playwright.Page;
public class LoginPage {
private final Page page;
public LoginPage(Page page) {
this.page = page;
}
private final String usernameTextbox = "#username";
private final String passwordTextbox = "#password";
private final String loginButton = "#loginBtn";
public void enterUsername(String username) {
page.fill(usernameTextbox, username);
}
public void enterPassword(String password) {
page.fill(passwordTextbox, password);
}
public void clickLogin() {
page.click(loginButton);
}
public void login(String username, String password) {
enterUsername(username);
enterPassword(password);
clickLogin();
}
}Step 2: Create the Home Page Class
Create a new file named HomePage.java.
package pages;
import com.microsoft.playwright.Page;
public class HomePage {
private final Page page;
public HomePage(Page page) {
this.page = page;
}
private final String welcomeMessage = ".welcome-text";
public String getWelcomeMessage() {
return page.textContent(welcomeMessage);
}
}Step 3: Create the Test Class
Now create your test class.
package tests;
import com.microsoft.playwright.*;
import pages.HomePage;
import pages.LoginPage;
public class LoginTest {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(
new BrowserType.LaunchOptions()
.setHeadless(false));
Page page = browser.newPage();
page.navigate("https://example.com/login");
LoginPage loginPage = new LoginPage(page);
loginPage.login(
"testuser",
"password123"
);
HomePage homePage = new HomePage(page);
System.out.println(
homePage.getWelcomeMessage()
);
browser.close();
}
}
}Step 4: Add a Base Test Class
As your framework grows, browser initialization should be moved into a separate class.
Example:
public class BaseTest {
protected Playwright playwright;
protected Browser browser;
protected Page page;
public void setup() {
playwright = Playwright.create();
browser = playwright.chromium().launch(
new BrowserType.LaunchOptions()
.setHeadless(false));
page = browser.newPage();
}
public void tearDown() {
browser.close();
playwright.close();
}
}This prevents duplication across test classes.
Step 5: Benefits of Using POM
Using Page Object Model provides:
- Better maintainability
- Reusable methods
- Cleaner test cases
- Easier debugging
- Improved scalability
For example, if the login button locator changes, you only update it in one place.
Best Practices for Playwright POM Framework
Use meaningful method names
Good:
loginPage.login(username, password);Avoid:
loginPage.clickButton();Keep locators inside page classes
Never place locators inside test classes.
Create reusable utility methods
Examples include:
- Screenshot utilities
- Wait utilities
- Configuration readers
- Test data readers
Use separate page classes for each page
Examples:
- LoginPage
- HomePage
- ProductPage
- CartPage
- CheckoutPage
When Should You Use POM?
POM is recommended when:
- Your project has more than a few pages
- Multiple testers contribute to automation
- The application changes frequently
- Test maintenance becomes difficult
For very small projects, a simple framework may be sufficient.
Conclusion
Page Object Model is one of the most important design patterns in automation testing.
By separating page logic from test logic, your Playwright framework becomes easier to maintain, scale, and understand.
In this tutorial, you learned:
- What POM is
- How to create page classes
- How to write reusable methods
- How to organize your framework structure
- Best practices for enterprise automation projects
In the next article, we will see about assertions.
No comments:
Post a Comment