iOS UI Testing Design Pattern: Introducing Page Object Pattern

KD Knowledge Diet
2 min readJul 3, 2022

When UI Testing is in progress, the code looks messy because of the huge number of mapping functions. In order to prevent this problem, I introduce the Page Object Pattern, which is easy for anyone to understand.

How to map XCUIElement

Code Snippet

This is what you are most likely to see when you make your UI Testing Code. You map XCUIElement with .accessibilityIdentifier(). This might seem trivial at first if you are writing a simple test. But as your code base grows this will be a disaster. Because you have to map XCUIElement again and again for each UITesting methods…

Before refactoring

Before Refactoring

The code is pure by itself. But you can see redundant mapping with hard-coded values such as “usernameTextField” and “passwordTextField”. Remember that when you are writing unit tests or ui tests, test cases can be hundreds or more. So how do you solve this problem?

Refactoring Guide

[1] Create Page Object

Page Object

Instead of mapping each XCUIElement inside test case methods, I created an Object which represents Login Page. This class has as many properties of XCUIElements as it has in LoginView. As a result, it increases reusability.

[2] Use your page object for your UITest

Refactored Code

Instead of hard coded values, you can now use pre-mapped XCUIElements through LoginPageObject. You can understand the power of this approach when you are writing more than one test cases!


  1. Create Page Object which maps view’s all XCUIElements.
  2. Reuse it in each test cases!



KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!