Page Object Model (POM)

Page Object Model (POM) in Mobile Application Testing

The Page Object Model (POM) is a design pattern that improves test code maintainability and readability by separating the test logic from the application's UI elements. In mobile application testing, POM helps organize tests for complex apps, ensuring scalability and reusability.

Key Concepts of POM

  • Page Class: A dedicated class representing a single screen or functionality of the mobile app. It contains locators and methods to interact with the elements on that screen.
  • Test Class: Contains the test logic, utilizing methods from the corresponding page class for element interactions.
  • Encapsulation: Encapsulates UI locators and their interaction logic, making it easier to update locators when the UI changes without modifying the test scripts.

Advantages of POM

  • Improved test code readability and maintainability.
  • Reusability of page methods across multiple test scripts.
  • Reduces redundancy by centralizing UI element locators.
  • Enhances scalability for large applications with multiple screens.

Steps to Implement POM

  1. Create Page Classes:
    • Each class represents a screen or module of the app.
    • Define locators and interaction methods within these classes.
  2. Create a Base Class:
    • Include common functionalities like driver initialization, tear-down, and utilities.
  3. Write Test Classes:
    • Use methods from the page classes to create test cases.
    • Separate test data using external files like Excel, JSON, or property files.

Sample POM Implementation

Below is an example of implementing POM for a login functionality in a mobile application:

1. Base Class


              import io.appium.java_client.android.AndroidDriver;
              import org.openqa.selenium.WebElement;
              import org.openqa.selenium.remote.DesiredCapabilities;
              import java.net.URL;
              
              public class BaseClass {
                  protected static AndroidDriver driver;
              
                  public void initializeDriver() {
                      try {
                          DesiredCapabilities caps = new DesiredCapabilities();
                          caps.setCapability("platformName", "Android");
                          caps.setCapability("deviceName", "Pixel_4_Emulator");
                          caps.setCapability("app", "/path/to/your/app.apk");
                          driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), caps);
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              
                  public void tearDown() {
                      if (driver != null) {
                          driver.quit();
                      }
                  }
              }
                  

2. Page Class for Login Screen


              import io.appium.java_client.android.AndroidDriver;
              import org.openqa.selenium.WebElement;
              import org.openqa.selenium.support.FindBy;
              import org.openqa.selenium.support.PageFactory;
              
              public class LoginPage {
                  private AndroidDriver driver;
              
                  // Locators
                  @FindBy(id = "com.example:id/username")
                  private WebElement usernameField;
              
                  @FindBy(id = "com.example:id/password")
                  private WebElement passwordField;
              
                  @FindBy(id = "com.example:id/loginButton")
                  private WebElement loginButton;
              
                  // Constructor
                  public LoginPage(AndroidDriver driver) {
                      this.driver = driver;
                      PageFactory.initElements(driver, this);
                  }
              
                  // Actions
                  public void enterUsername(String username) {
                      usernameField.sendKeys(username);
                  }
              
                  public void enterPassword(String password) {
                      passwordField.sendKeys(password);
                  }
              
                  public void clickLoginButton() {
                      loginButton.click();
                  }
              }
                  

3. Test Class


              import org.testng.annotations.AfterClass;
              import org.testng.annotations.BeforeClass;
              import org.testng.annotations.Test;
              
              public class LoginTest extends BaseClass {
                  private LoginPage loginPage;
              
                  @BeforeClass
                  public void setup() {
                      initializeDriver();
                      loginPage = new LoginPage(driver);
                  }
              
                  @Test
                  public void testValidLogin() {
                      loginPage.enterUsername("testuser");
                      loginPage.enterPassword("password123");
                      loginPage.clickLoginButton();
                      // Add assertions to validate successful login
                  }
              
                  @AfterClass
                  public void teardown() {
                      tearDown();
                  }
              }
                  

Real-Time Scenarios

  1. Login Validation: Create a LoginPage class and test multiple login scenarios using the same page methods.
  2. Shopping App: Develop page classes for screens like Home, Product Details, and Cart to test navigation and functionality.
  3. Multi-Language Testing: Use POM to organize locale-based test cases by modifying page element locators for different languages.

Best Practices for POM

  • Keep page classes focused on one screen or module for simplicity.
  • Use descriptive method names for better readability.
  • Centralize locator definitions to make updates easier when the UI changes.
  • Combine POM with data-driven testing for maximum flexibility.

Challenges with POM

  • Initial setup can be time-consuming for large applications.
  • Frequent UI changes require updates to multiple page classes.
  • Managing dependencies between test classes and page classes can be complex.