iOS Automation Testing: Getting Started with Xcode UI Testing

Maksim Akifev
4 min readMay 6, 2018

--

How to write iOS Automation tests? How to use Xcode UI Testing Framework to write reliable and maintainable tests with Screen Object Pattern? Read this article to find out.

Introduction to XCUITest Framework

As a first step we need to choose Automation Framework. In this tutorial we’ll be taking advantage of XCUITest Framework. A framework that allows testing actual user interactions with the screen. It’s developed by Apple and provides great functionality for User Interface and Integration Testing.

Setting up Xcode Project for UI Testing

Xcode UI Testing requires access to Application Source Code, and Xcode installed on your machine. To get started we need to create UI Test Target:

  • Open Xcode Project
  • File > New > Target
  • Select iOS UI Testing Bundle
  • Enter your organization information and select a Programming Language (We’ll be using Swift in this tutorial)

Writing the first test using XCUI Test Recorder

XCUI Test Recorder allows us to record user interactions with the screen and use generated code in our tests:

  • Navigate to Test Navigator
  • Select a test function to record
  • Press the UI Recorder button
  • Execute your test on the device

To complete our test we should add assertion to verify, that expected element is displayed on Home Screen

// Element expected to be displayedlet player = app.collectionViews.staticTexts["Test"]
// Assert that element is displayedXCTAssertTrue(player.exists)

Using Accessibility Inspector to inspect Elements

XCUI Test Recorder is great for simple tests, but the code generated by the recorder needs modifications and sometimes it will not provide the correct locator for the Element. A better approach is to use Accessibility Inspector:

  • Open Accessibility Inspector
  • Click Start Inspection button
  • Inspect Element to define the locator

When we know the Element debug description we can write Locator according to the value:

let playerBalance = app.staticTexts["$99.999"]

Another approach to get the screen debug description is to print accessibility hierarchy in Xcode console:

print(app.debugDescription)

Introduction to Screen Objects Pattern

When writing User Interface and Integration tests, we would like to create maintainable tests with readable code. Inspired by Page Objects Pattern for WEB Applications Testing we’ll be following Screen Objects Pattern in this tutorial:

  • Create Screen Objects for each Screen
  • Define Elements Locators
  • Implement the screen test methods that we’ll be using in our tests

Once we have created Screen Objects for each screen we need to initialize them in Test Case class:

// MARK: Screen Objects initializerslet settingsScreen = SettingsScreen()let addPlayerScreen = AddPlayerScreen()let homeScreen = HomeScreen()

Using Screen Objects Pattern will allow us to create new tests quicker with clearer and easier to follow code:

func testAddPlayerWithDefaultBalance() {/**
Add new Player with default balance and assert that added player is displayed on Home Screen
*/homeScreen.addPlayer()addPlayerScreen.setPlayerNameWith(testPlayerName1)addPlayerScreen.addPlayer()XCTAssertTrue(homeScreen.isPlayerDisplayed(name: testPlayerName1, balance: defaultPlayerBalance, currency: defaultCurrency))}

Next Steps

  • Execute tests with Fastlane tools to run on CI server
  • Generating tests reports and screenshots
  • Parallel test execution on real devices and simulators

Conclusion

XCUITest is a powerful Test Automation Framework for writing reliable tests for native iOS Applications. It’s easy to set it up with existing Xcode Project and it doesn’t require advanced coding skills to start writing tests.

Screen Objects Pattern improves the test code readability and makes is easier to maintain existing tests and create new ones. Complete Test Framework setup example with Application Source Code is available on GitHub, I highly recommend checking it out to learn more details in context.

Stay tuned for more tutorials and Happy Testing :)

About the Author

Maksim Akifev is a highly skilled Quality Assurance Engineer with wide experience in Test Automation, Security and Penetration Testing who helps Companies in various industries such as Finance, Healthcare, and Cyber Security to build reliable and secure Products.

--

--

Maksim Akifev

Helping companies to build reliable and secure products