iOS app communication
XCUITests run in their own process and can only tap, type and read what is on screen. With XPCService, a test can talk to the app under test directly: tell it to log in a test user or turn on a feature flag, or ask it for a value, such as the current user's ID. Tests get shorter and faster, because they skip UI steps that only prepare state, and they no longer need launch arguments that require relaunching the app.
sequenceDiagram
participant Test as UI test (AppeliumTests)
participant App as App under test (AppeliumFeedback)
App->>App: register handler "currentUserId"
Test->>App: send("currentUserId")
App-->>Test: "user-42"
Requirements
- The UI test target starts AppeliumTests.
- The app under test starts AppeliumFeedback. It does not need to enable any feature.
- On a device,
AppeliumFeedback.xpcServiceisnilunless the app runs under AppeliumTests, so your handlers are never registered in the app on your users' devices. In the Simulator, it is always available, so that a test can also talk to an app that was launched before it.
Handle messages in the app
Register a handler for each message right after AppeliumFeedback.start, before the tests send it. A message is identified by its name, a String; its content and its response can be any Codable type:
AppeliumFeedback.start(apiKey: "YOUR_API_KEY")
// A message with content and no response
AppeliumFeedback.xpcService?.on("setFeatureFlag", decoding: FeatureFlag.self) { featureFlag in
FeatureFlags.shared.set(featureFlag.name, isEnabled: featureFlag.isEnabled)
}
// A message with a response
AppeliumFeedback.xpcService?.on("currentUserId", encoding: String.self) {
UserSession.shared.userId
}
// A response that is ready only later
AppeliumFeedback.xpcService?.onAsync("unreadMessagesCount", encoding: Int.self) { respond in
Inbox.shared.loadUnreadCount { count in
respond(count)
}
}
- Handlers run on a background queue, not on the main thread. Move work with UI or main-actor state to the main actor.
- Never block a handler while you wait for a result, for example with a semaphore: it delays every other message. Use
onAsyncand callrespondwhen the result is ready. - If an
onAsynchandler does not respond within itstimeout(3 seconds by default), the test receives an error. - If a handler throws, the test's
sendthrows too. - Keep test-only handlers out of release builds, for example with
#if DEBUG.
Send messages from the test
Use the xpcService of the XCUIApplication:
@MainActor
func testInboxShowsUnreadCount() throws {
let app = XCUIApplication()
app.launch()
try app.xpcService.send("setFeatureFlag", content: FeatureFlag(name: "newInbox", isEnabled: true))
let unreadMessagesCount: Int = try app.xpcService.send("unreadMessagesCount")
XCTAssertTrue(app.staticTexts["\(unreadMessagesCount) unread"].exists)
}
sendwaits for the app's answer and returns the decoded response. It throws anXPCErrorwhen the app does not answer withintimeout(5 seconds by default), or when the handler fails.- Each
sendhas asendAsynccounterpart that returns at once and calls a completion handler with aResult. - Share the
Codabletypes and the message names between the app and the UI test target, for example in a file that belongs to both, so both sides encode and decode the same type.
API
In the app (AppeliumFeedback.xpcService) |
Use it for |
|---|---|
on(_:handler:) |
A message without content or response |
on(_:decoding:handler:) |
A message with content |
on(_:encoding:handler:) |
A message with a response |
on(_:decoding:encoding:handler:) |
A message with content and a response |
onAsync(_:encoding:timeout:handler:) |
A response that is produced asynchronously |
In the test (XCUIApplication().xpcService) |
Use it for |
|---|---|
send(_:timeout:) |
Waits until the app has handled the message |
send(_:content:timeout:) |
The same, with content |
send(_:timeout:) -> D |
Waits for a response of type D |
send(_:content:timeout:) -> D |
The same, with content |
sendAsync(…) |
The same four, without waiting, with a completion handler |
The permission helpers of AppeliumTestsKit are built on the same messages, so the app under test answers them without any code of yours.