iOS UI test helpers
AppeliumTestsKit includes two sets of helpers for your XCUITests. SnapshotQuery checks many elements of a screen at once, much faster than XCUIElementQuery. Permission helpers read the authorization status of the app under test, such as photo library or notification access, so a test can check the state the app is in. Both are available once AppeliumTestsKit is added to your UI test target, as described in Integration.
SnapshotQuery
Every XCUIElementQuery takes its own snapshot of the app's view hierarchy, which typically takes 50–100 ms. SnapshotQuery takes one snapshot and runs all queries against it in memory. It pays off as soon as a test checks more than one element on the same screen. The SnapshotQuery article explains how it works.
Take a snapshot with XCTest's snapshot() and query it with the same element-type properties and identifier subscripts as XCUIElementQuery:
@MainActor
func testWelcomeScreen() throws {
let app = XCUIApplication()
app.launch()
let screen = try app.snapshot()
XCTAssertTrue(screen.staticTexts["Welcome"].exists)
XCTAssertTrue(screen.buttons["Login"].exists)
XCTAssertEqual(screen.cells.count, 3)
XCTAssertFalse(screen.buttons["Submit"].first.isEnabled)
}
A snapshot shows the screen at the moment it was taken. After an action changes the screen, take a new snapshot:
var screen = try app.snapshot()
screen.buttons["Register"].first?.tap()
screen = try app.snapshot()
XCTAssertTrue(screen.textFields["email"].exists)
Querying
| API | Returns |
|---|---|
buttons, staticTexts, cells, textFields and every other element type |
A SnapshotQuery of the matching descendants. Queries can be chained, for example screen.cells.buttons. |
query["identifier"] |
The elements of the query whose accessibility identifier, label or placeholder matches. Line breaks count as spaces, and only the first 128 characters are compared, as in XCTest. Use it directly on a snapshot to search all element types, for example screen["Safari"]. |
query.matching(NSPredicate) |
The elements of the query that match the predicate. |
Results
| API | Returns |
|---|---|
exists |
Whether at least one element matches. |
count |
The number of matching elements. |
first |
The first matching element snapshot, or nil. |
results |
All matching element snapshots. |
first.exists, first.isEnabled, first.isSelected, first.isHittable, first.label, first.value |
The state of the first matching element. Each is false or nil when nothing matches. |
element.tap() |
Taps the center of the element's visible area. |
SnapshotQuery runs on the main actor, like XCUITests.
Permission helpers
To check how the app under test is authorized, read the status straight from the app instead of navigating to the Settings app:
import XCTest
import Photos
import AppeliumTestsKit
@MainActor
func testPhotoAccessIsRequestedOnFirstUpload() throws {
let app = XCUIApplication()
app.launch()
XCTAssertEqual(app.photoLibraryAuthorizationStatus(for: .readWrite), .notDetermined)
}
API on XCUIApplication |
Returns |
|---|---|
photoLibraryAuthorizationStatus(for: PHAccessLevel) |
PHAuthorizationStatus |
mediaAuthorizationStatus(for: AVMediaType) |
AVAuthorizationStatus for .video or .audio |
userNotificationSettingsAuthorizationStatus |
UNAuthorizationStatus |
locationAuthorizationStatus |
CLAuthorizationStatus |
contactsAuthorizationStatus |
CNAuthorizationStatus |
- The app under test must be running and must start AppeliumFeedback. It does not need to enable any feature.
- Import the framework that defines the returned type, such as
Photos,AVFoundation,UserNotifications,CoreLocationorContacts. - If the status cannot be read, the test fails with a message, and the call returns
.notDetermined. - Each check is recorded in the test's activity log, so the report shows what the test saw.