Search code examples
objective-cxcodexctestxctestcase

Automatically dismissing dialog in XCTestCase


When my app first starts up, it displays a dialog asking the user to respond by clicking either "Don't Allow" or "OK" if they would allow the app to use the microphone.

Problem is, when the unit tests are first started this same dialog appears, which stops the execution of the tests. If the unit tests are being run by Xcode Server, there is no way to manually intervene and click a button.

Is there a way to automate the dismissal of this dialog? Doesn't matter which button is clicked for the unit tests.

I've tried putting the following code in the XCTestCase setUp() method:

[self addUIInterruptionMonitorWithDescription:@"Interruption Monitor" handler:^{BOOL(XCUIElement * element) {
    XCUIElement *button = [[element buttons] elementAtIndex:0];
    if (button) {
        [button tap];
    }
}];

but the handler is never called.

I don't even need the app to care about the microphone for the unit tests, if there's a way to never display the dialog in the first place.


Solution

  • Use shell command xcrun simctl privacy before executing the tests (e.g. before xcodebuild ... or as a preparation step in target's settings)

    Here its info

    Grant, revoke, or reset privacy and permissions
    Usage: simctl privacy <device> <action> <service> [<bundle identifier>]
    
        action
             The action to take:
                 grant - Grant access without prompting. Requires bundle identifier.
                 revoke - Revoke access, denying all use of the service. Requires bundle identifier.
                 reset - Reset access, prompting on next use. Bundle identifier optional.
             Some permission changes will terminate the application if running.
        service
             The service:
                 all - Apply the action to all services.
                 calendar - Allow access to calendar.
                 contacts-limited - Allow access to basic contact info.
                 contacts - Allow access to full contact details.
                 location - Allow access to location services when app is in use.
                 location-always - Allow access to location services at all times.
                 photos-add - Allow adding photos to the photo library.
                 photos - Allow full access to the photo library.
                 media-library - Allow access to the media library.
                 microphone - Allow access to audio input.
                 motion - Allow access to motion and fitness data.
                 reminders - Allow access to reminders.
                 siri - Allow use of the app with Siri.
        bundle identifier
             The bundle identifier of the target application.
    
    Examples:
        reset all permissions: privacy <device> reset all
        grant test host photo permissions: privacy <device> grant photos com.example.app.test-host
    
    Warning:
    Normally applications must have valid Info.plist usage description keys and follow the API guidelines to request access to services. Using this command to bypass those requirements can mask bugs.