iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

[iOS] Granting Photo Library Permissions in UITest

に公開
2025/04/12

When automating iOS app tests using UITest, it is necessary to simulate access permission when the app requests access to the photo album.

However, it is not possible to interact with system permission alerts from the app being tested.

Therefore, we will attempt to allow access to the photo album through the alert displayed by Springboard by interacting with Springboard directly.

Sample code below:

import XCTest

class PhotoAlbumPermissionTest: XCTestCase {

    func testGrantPhotoAlbumPermission() {
        let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
        
        // Wait until the "Allow Full Access" button is displayed
        let allowButton = springboard.buttons["Allow Full Access"]
        _ = allowButton.waitForExistence(timeout: 3)
        
        // Tap the "Allow Full Access" button
        allowButton.tap()
    }
}

Note that the allowButton will not be displayed in subsequent tests.

Discussion