iOS UI Testing Design Pattern: Introducing Page Object Pattern
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
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…
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?
 Create 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.
 Use your page object for your UITest
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!
- Create Page Object which maps view’s all XCUIElements.
- Reuse it in each test cases!