Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Frameworks and Page Objects

The Page Object Model for Mobile Tests

Organize Appium tests with screen objects: one class per screen, locators in one place, actions as methods, cross-platform locators and fluent navigation.

A first Appium script is a list of find_element calls. It works, and then the app changes: a button gets a new ID, and fifty tests break because each one repeated the same locator. The Page Object Model solves this by wrapping each screen in a class. Locators live in one place, user actions are methods with meaningful names, and tests read like a description of behavior instead of a list of taps.

In mobile testing the pattern is often called screen objects, because apps have screens rather than pages. The idea is identical. This lesson builds screen objects in Python, handles Android and iOS from one class, and shows what belongs in a screen object and what does not.

Appium cannot run on this site, so the real classes are read-only. The runnable examples use a fake driver so you can see how screen objects, locators and tests fit together.

Without a screen object

Python
def test_login_fails_with_wrong_password(driver):
    driver.find_element(AppiumBy.ACCESSIBILITY_ID, "email").send_keys("[email protected]")
    driver.find_element(AppiumBy.ACCESSIBILITY_ID, "password").send_keys("wrong")
    driver.find_element(AppiumBy.ACCESSIBILITY_ID, "login-button").click()
    error = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "error-label").text
    assert error == "Wrong email or password"

The test is correct, but the locators are repeated in every login test, and a reader has to decode low-level calls to understand the intent.

With a screen object

Python
from appium.webdriver.common.appiumby import AppiumBy


class LoginScreen:
    EMAIL = (AppiumBy.ACCESSIBILITY_ID, "email")
    PASSWORD = (AppiumBy.ACCESSIBILITY_ID, "password")
    SUBMIT = (AppiumBy.ACCESSIBILITY_ID, "login-button")
    ERROR = (AppiumBy.ACCESSIBILITY_ID, "error-label")

    def __init__(self, driver):
        self.driver = driver

    def log_in(self, email, password):
        self.driver.find_element(*self.EMAIL).send_keys(email)
        self.driver.find_element(*self.PASSWORD).send_keys(password)
        self.driver.find_element(*self.SUBMIT).click()
        return self

    def error_message(self):
        return self.driver.find_element(*self.ERROR).text

The test becomes:

Python
def test_login_fails_with_wrong_password(driver):
    screen = LoginScreen(driver).log_in("[email protected]", "wrong")
    assert screen.error_message() == "Wrong email or password"

Now a new locator is a one-line change, and the test describes what the user does.

Stand-in: screen objects and a fake driver

This runnable example mirrors the structure above with a fake driver that stores text and clicks, so you can try the pattern without a device.

Python Screen objects with a fake driver (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

An action that moves the user to another screen should return the object for that screen. The test then flows naturally and a wrong assumption fails early with an attribute error rather than a missing element.

Python
class HomeScreen:
    TITLE = (AppiumBy.ACCESSIBILITY_ID, "home-title")

    def __init__(self, driver):
        self.driver = driver

    def is_displayed(self):
        return self.driver.find_element(*self.TITLE).is_displayed()


class LoginScreen:
    ...
    def log_in_successfully(self, email, password):
        self.log_in(email, password)
        return HomeScreen(self.driver)
Python
home = LoginScreen(driver).log_in_successfully("[email protected]", "secret")
assert home.is_displayed()
flowchart LR
    L["LoginScreen"] -->|log_in_successfully| H["HomeScreen"]
    H -->|open_product| P["ProductScreen"]
    P -->|add_to_cart| C["CartScreen"]
    C -->|checkout| O["OrderScreen"]

Each method returns the screen the user lands on.

One class, two platforms

Android and iOS usually share accessibility IDs, so one class can serve both. When locators differ, pick them by platform in one place:

Python
class CartScreen:
    def __init__(self, driver):
        self.driver = driver
        if driver.capabilities["platformName"].lower() == "android":
            self.TOTAL = (AppiumBy.ID, "com.example.shop:id/total")
        else:
            self.TOTAL = (AppiumBy.IOS_PREDICATE, 'name == "cart-total"')

An alternative is a base class with a subclass per platform (CartScreen, AndroidCartScreen, IosCartScreen) and a small factory. Choose the subclass design when behavior, not just locators, differs.

Stand-in: pick locators by platform

Python Platform locator table (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What belongs where

Belongs in a screen object Belongs in the test
Locators Assertions about expected behavior
User actions (log_in, add_to_cart) Test data selection
Reading values (error_message, total) Setup and teardown decisions
Waiting for the screen to be ready Which scenario is being checked

Keep assertions out of screen objects, with one exception: a small is_displayed() style check that confirms the screen loaded. A screen object that asserts business rules becomes hard to reuse.

Good habits

  • Wait in the screen object. The constructor or an is_loaded() method can wait for a signature element, so tests never need sleeps (see Waits and Synchronization).
  • Make components reusable. A bottom tab bar or a product card appears on many screens. Write a TabBar component class and compose it instead of copying locators.
  • Keep driver details private. Tests should call login.log_in(...), not login.driver.find_element(...).
  • Name actions after intent, not mechanics: add_to_cart() rather than tap_button_3().

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main benefit of a screen object?

    Centralizing locators and actions reduces duplication and makes tests readable.
  2. 2. What should a method like log_in_successfully return?

    Returning the next screen keeps the test fluent and catches wrong navigation early.
  3. 3. Where should business assertions normally live?

    Screen objects describe what can be done; tests decide what must be true.
  4. 4. Android and iOS use different locators for the same control. What is a good approach?

    Keep the difference hidden inside the screen object so tests stay platform neutral.

Summary

  • One class per screen holds locators and user-level actions; tests call methods that read like user steps.
  • Navigation methods return the next screen object.
  • Hide platform differences inside the screen object, either with a locator table or subclasses.
  • Keep sleeps, driver calls and business assertions out of tests and screen objects respectively; waiting belongs in the screen object.
  • Reuse components such as tab bars and product cards.

Next: Test Runners and Fixtures: pytest, JUnit and TestNG