在我的应用中,我请求这样的通知权限:
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/