The Page Object Model (POM) is a powerful design pattern widely adopted in test automation. It helps improve code maintainability, reduce duplication, and make test scripts more readable.
What is the Page Object Design Pattern?
A Page Object is a class that serves as an interface to a page within your Application Under Test (AUT). Instead of interacting directly with the UI, your test scripts use the methods defined in the page class.
As a result, if the UI changes, you only need to update the code inside the page object, not the test scripts. This creates a single point of maintenance and improves overall stability.
Key Benefits of Using the Page Object Model:
- Improved maintainability: You can manage UI changes in one place.
- High code reusability: Common actions and locators are centralized.
- Better separation of concerns: Test logic stays separate from UI interaction logic.
Page Object Model vs Non-Page Object Model
Let’s quickly compare POM with a non-POM approach:
Feature | Page Object Model | Non-POM |
---|---|---|
Locator Management | Centralized | Scattered across test cases |
Maintenance Effort | Low | High |
Readability | High | Low |
Code Duplication | Minimal | Significant |
As shown above, POM provides a cleaner and more structured approach for scaling your test automation.
How to Get Started with Page Object Model (Appium + Android)
Now that you understand the concept, let’s implement the Page Object Model using Appium, Selenium, and TestNG for a basic Android login app.
1. Prerequisites
Before you start, make sure the following tools are installed:
- Java JDK
- Appium
- Maven
- Android Studio or Emulator
2. Application Overview
We’ll test a simple login app that includes the following behavior:
- Login is successful when
Username = Osanda
andPassword = MaxSoft123
. - After login, the app redirects to a home page that displays greeting labels.
- Each failed login attempt reduces the attempt counter by one.
- After five failed attempts, the login button becomes disabled.
3. Add Dependencies to pom.xml
Add these dependencies to your Maven project:
<dependencies>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.3.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>
</dependencies>
These libraries provide everything needed for Appium-based mobile testing.
4. Create the Page Classes
Let’s begin by defining the Page Object classes.
LoginPage.java
UI Elements (Locators):
- Username textbox
- Password textbox
- Login button
- Attempts counter label
Action Methods:
enterUsername(String username)
enterPassword(String password)
clickLogin()
login(String username, String password)
– performs the full login flowgetAttemptsLeftText()
These methods encapsulate all user interactions on the login screen.
HomePage.java
Locators:
- Greeting label
- Welcome label
Action Methods:
getGreetingText()
getWelcomeText()
Both methods return the welcome text shown after a successful login.
5. Write the Test Class: LoginTest.java
In this class, you should test:
- Successful login with valid credentials
- Failed login attempts and decrement of attempt counter
- Disabled login button after five failed attempts
By keeping the test logic separate, you ensure clean and easy-to-update test scripts.
6. Explore the Full Example
You can clone a complete sample project from GitHub that includes all the above:
📦 Appium Java Mobile Automation Demo
It includes the page classes, test cases, and configuration files to get started immediately.
Final Thoughts
The Page Object Model is essential for developing robust and scalable automation frameworks. It separates concerns, reduces repetition, and simplifies updates when your application’s UI changes.
Therefore, if you want to create a maintainable test suite for your mobile or web applications, start using the Page Object Design Pattern today.