iOS Automation Testing with Xcode UI Framework
โก Smart Summary
iOS automation testing with Xcode records and replays user interface actions against an application under test, following a test-driven cycle of design, test, implement and test again until every case passes.
iOS Automation Testing using Xcode
To guarantee the quality of your iOS application, you should follow the test-driven development process shown in the figure below.
Test-Driven Development (TDD) is a testing model which is applied to iOS application testing, and it sits inside the wider practice of mobile testing. In this model, a tester has to follow the 4 phases below:
- Design: Figure out what you want to test and design your test cases.
- Test: Run all tests and see if any test cases fail.
- Implement: Revise your code and fix the bugs that caused the test to fail.
- Test again: If a test fails, roll back to the design. If all test cases pass, the code meets the entire tested requirement.
Setting up Xcode Project for UI Testing
To create an iOS test program you need a Mac. Your Mac must already have the following installed:
- OS X โ the operating system for a Mac.
- Xcode IDE โ the development tool for iOS.
- An automated testing framework โ UI Automation, OCUnit, and so on.
- iOS SDK 4 or higher.
โ ๏ธ Version note: The prerequisites above describe the toolchain of the era in which the two walkthroughs that follow were written. On a current Mac, Xcode ships the XCTest and XCUITest frameworks in the box, and the separate UI Automation instrument is no longer part of the installation. The original steps are preserved below because they document how the framework worked, and the modern equivalent is described further down.
How to Create iOS Automation using UI Automation Framework
The eight steps below record a script with the Automation instrument and replay it against the application under test.
Step 1) Launch Instruments
Open XCode -> Open Developer Tool -> Instrument
Step 2) Add Automation Instrument
In the Instruments window, select the Automation instrument.
To create a test script, you either record a test scenario or you program it manually.
Step 3) Press Red button
An instrument is launching โ stop the recording immediately. If you want to start the recording, press the red button.
Step 4) Create a new script
In the Scripts window, click Add > Create to create a new script.
Step 5) Choose the target
You are now in the Trace window. Use the Choose Target pull-down to navigate to the debugging version of your app.
In this case, Apple’s sample SimpleDrillDown app is used as the application under test. It has the GUI shown below.
Step 6) Start record your script
Record your script by hitting the record button at the top or bottom of the tool.
Now you can perform some UI actions on your application under test, and your script is recorded.
Step 7) See your script
To see your script, hit the Trace Log / Editor Log drop-down and switch to the script log view.
You will see your recorded script.
Step 8) Play your script
Press the play button. The script runs, and you can stop it after the logs appear.
โ ๏ธ Historical note: The Automation instrument used in these eight steps was deprecated in Xcode 8 and later removed, so it is no longer present in current Xcode installations. The steps are retained here as a record of how the framework worked; for new work, use the XCUITest walkthrough further down this page.
How to Create iOS Automation using OCUnit framework
The second route places the tests inside the Xcode project itself rather than inside Instruments.
Step 1) Start Xcode IDE, Add Unit Test Bundle target
Step 2) Write the name of the new Unit Test Bundle as shown in the figure above, then click Finish.
Step 3) Make Unit Test the active target
Step 4) Add a group for test classes
Step 5) Add a Unit test class
Step 6) Now start your implementation
OCUnit uses the Objective-C language to create the test program, so the developer must know that language. Modern Xcode versions ship unit testing through XCTest instead, which supports both Objective-C and Swift, but the target-and-class structure shown in these six steps is unchanged.
UIAutomation vs XCUITest: What Changed
Because both frameworks used above belong to an earlier generation of the Apple toolchain, it is worth being precise about what replaced them and why.
| Aspect | UI Automation (Instruments) | XCUITest |
| Status | Deprecated in Xcode 8 and removed from later releases | Apple’s supported UI testing framework |
| Where tests live | Scripts inside an Instruments trace document | A UI Testing Bundle target inside the Xcode project |
| Language | JavaScript | Swift or Objective-C |
| Test runner | The Automation instrument | XCTest, the same runner as unit tests |
| Continuous integration | Awkward โ driven through Instruments | Runs from the command line alongside unit tests |
| Recording | Record button in the Instruments toolbar | Record button in the Xcode editor, which emits Swift |
The practical consequence is that an XCUITest suite is ordinary project source code. It is reviewed, versioned and executed like the rest of the codebase, which is the main reason the recorded-trace model disappeared.
How to Write an iOS UI Test with XCUITest
The modern equivalent of the eight Instruments steps is short. The structure below mirrors the same record-then-replay idea, but the output is a source file rather than a trace.
- Add the target. In Xcode choose File > New > Target and pick the UI Testing Bundle template, or tick the option to include tests when creating a new project.
- Open the generated test class. Xcode creates an XCTestCase subclass with empty setup and test methods.
- Launch the app under test. Create an XCUIApplication instance and call launch on it, which starts the app in a separate process.
- Query and act. Reach elements through the element queries โ buttons, tables, static texts โ and call tap, typeText or swipe on them.
- Assert. Use XCTAssert to verify that the expected element exists after the action.
- Run. Execute the test from the Xcode test navigator, or from the command line so the same suite runs in continuous integration.
A minimal test follows this shape:
import XCTest final class AppUITests: XCTestCase { func testTappingFirstRowShowsDetail() { let app = XCUIApplication() app.launch() // act on the first row of the list app.tables.cells.element(boundBy: 0).tap() // verify that the next screen appeared XCTAssertTrue(app.staticTexts.firstMatch.waitForExistence(timeout: 5)) } }
The recorder still exists: placing the cursor inside a test method and pressing the record button in the editor generates these queries automatically, which is the direct descendant of Step 6 above. The wider principles of automation testing apply unchanged.
UI Automation Sample Code
This article includes some source code examples. They help you understand the tutorial more clearly and quickly.
UI Automation Sample โ test script for the UI Automation demo.















