XCTestCase 中自动关闭对话框

Automatically dismissing dialog in XCTestCase

当我的应用程序首次启动时,它会显示一个对话框,要求用户通过单击“不允许”或“确定”来响应是否允许该应用程序使用麦克风。

问题是,当第一次启动单元测试时,会出现同样的对话框,这会停止测试的执行。如果 运行 服务器正在 运行 进行单元测试,则无法手动干预和单击按钮。

有没有办法自动关闭这个对话框?单击哪个按钮进行单元测试无关紧要。

我尝试将以下代码放入 XCTestCase setUp() 方法中:

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

但从未调用处理程序。

我什至不需要应用程序关心单元测试的麦克风,如果有办法从一开始就不显示对话框的话。

在执行测试之前使用 shell 命令 xcrun simctl privacy(例如,在 xcodebuild ... 之前或作为目标设置中的准备步骤)

这是它的信息

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.

中断监视器需要 return 一个布尔值 - 这个想法是当检测到中断时,测试运行器将执行所有已添加到中断处理程序堆栈的中断处理程序,直到其中之一这些处理程序 return 是正确的。

https://developer.apple.com/documentation/xctest/xctestcase/handling_ui_interruptions

https://developer.apple.com/videos/play/wwdc2020/10220/