swift - Xcode UI 测试 如何处理 UNUserNotificationCenter 生成的通知权限

标签 swift xcode uitest

在我的应用中,我请求这样的通知权限:

  UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in

        if granted {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }

现在在测试时,我需要处理这个权限弹出窗口,但它不起作用,我尝试了这些代码:

XCUIApplication().alerts["“AppName” Would Like to Send You Notifications"].buttons["Allow"].tap() //didn't work.

addUIInterruptionMonitorWithDescription("Notification Dialog") { (alert) -> Bool in  
    alert.buttons["Allow"].tap()
    return true
}


addUIInterruptionMonitorWithDescription("“AppName” Would Like to Send You Notifications") { (alert) -> Bool in  
    alert.buttons["Allow"].tap()
    return true
}


addUIInterruptionMonitorWithDescription("Notifications may include alerts, sounds, and icon badges. These can be configured in Settings.") { (alert) -> Bool in  
    alert.buttons["Allow"].tap()
    return true
}

但没有任何作用。

最佳答案

试试这个

    let app2 = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    let button = app2.alerts.firstMatch.buttons["Allow"]
    button.waitForExistence(timeout: 10)
    button.tap()

关于swift - Xcode UI 测试 如何处理 UNUserNotificationCenter 生成的通知权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52161036/

相关文章:

xcode-ui-testing - 如何获取与标识符匹配的元素数量,例如 XCUIQuery 返回的 "fooImage"?

ios - 当键盘出现在 SwiftUI 中时向上移动 TextField

iphone - 使用静态库打包 Bundle

xcode - 在 yosemite 上使用 npm 安装 bcrypt 时出错

ios - Xcode 8.1 dyld : Library not loaded: @rpath/XCTest. 框架/XCTest 错误

ios - 无法加载 bundle “CheckoutTests”,因为它已损坏或缺少必要的资源。尝试重新安装 bundle

ios - 组合选项卡 View 和分割 View

ios - 按日期解析的有组织的组合数组

ios - didSelect 执行不等待 didDeSelect 动画结束

ios - 如何用swift 2.0播放背景音乐?