Cannot find system alert using Xcode Tests

Viewed 59

How do you tap on "OK" on the iOS system alert asking "Photos access" using Xcode Tests?

There are many many such questions on SO, but none of the answers worked for me.

Tests always fail - I.E. they cannot find the alert at all.

Xcode 13.4
Swift 5

1 Answers

Just a simple tap on any alert would be:

XCUIApplication(bundleIdentifier: "com.apple.springboard").buttons["OK"].tap()

If you want to ensure the correct alert is shown, you can build a more complex query like:

XCUIApplication(bundleIdentifier: "com.apple.springboard").alerts.matching(NSPredicate(format: "label CONTAINS[c] %@", "Access Photos")).element.buttons["OK"].tap()
Related