UI 测试 - 访问相机

UI Testing - Access the Camera

当我在真实设备上 UI 测试中首次触发应用程序摄像头时,我收到了权限警告框。为了避免崩溃并遵循我的 ui 测试步骤,我尝试了以下步骤。但仍然无法按我的意愿工作。有什么避免崩溃的想法吗?

XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[@"Open Camera"] tap];

XCUIElement *alert = app.alerts[@"\u201cSampleAppObjC\u201d Would Like to Access the Camera"].buttons[@"OK"];
if (alert) {
    [alert tap];
}
...
[app.buttons[@"Flash"] tap];
[app.buttons[@"Help"] tap];

我找到了一种允许首次触发摄像头的权限的方法

//system camera alert for permission
id systemAlertMonitor = [self addUIInterruptionMonitorWithDescription:@"Alert Handler" handler:^BOOL(XCUIElement * _Nonnull interruptingElement) {

if (interruptingElement.buttons[@"OK"].exists) {
        [interruptingElement.buttons[@"OK"] tap];
        //Return YES if handled the alert
        return YES;
    }
    return NO;
}];