Page Object Model Pattern in Swift

While Writing XCUITest

Tolga Taner
1 min readJun 9, 2021
Photo by Tron Le on Unsplash

In mobile development, Page Object Model Pattern is applies during Selenium, Appium test cases by Test Developers. With XCUITest framework that is introduced by Apple, iOS Developers can also apply this practice. It reduces code duplication, increase code readability, scalability when more than one screens require to same feature. Technically, each pages contains user interface elements which is corresponding to its located screen. Let’s begin with refactoring my old UITest example to Page Object Model Pattern. It is from here.

This code isn’t reusable. We might need use this login logic for other screens. If we use this code block for other screens, we meet a classic code duplication problem.

Page Object Model Pattern

First, create LoginScreen.swift file inside the UITest folder and define XCUIElements corresponding to related screen after that define user interaction functions that returns itself.

Now, go to UITests.swift class and create login screen instance, call its method.

That’s all, we can easily see that we refactored login logic as a functionality to use in other screens. If you read to enjoy, follow me 👋

--

--