Swift Xcode UI测试如何处理UNUserNotificationCenter生成的通知权限
在我的应用程序中,我请求如下通知权限:Swift Xcode UI测试如何处理UNUserNotificationCenter生成的通知权限,swift,xcode,uitest,Swift,Xcode,Uitest,在我的应用程序中,我请求如下通知权限: UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in if granted { DispatchQueue.main.async { UIApplication.shared.registerForRemoteN
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
}
但什么都不管用 1/尝试使用XCode test recorder to alert按钮记录路径,并确保您拥有正确的路径
XCUIApplication().alerts["“AppName” Would Like to Send You Notifications"].buttons["Allow"].tap() //didn't work.
可能不正确
注意:使用测试记录器做一些事情,比如找到元素的路径是可以的,而且对新手非常友好
2/您不必使用AddUI InterruptionMonitor with Description,因为您有一个系统警报,每次都会发生,或者您知道它会发生。
只要使用waitForExistance并点击它(如果它存在)
注意:我发现等待和点击系统通知比添加UIInterruptionMonitorWithDescription要好,后者有时不稳定/复杂
3/alert.buttons[Allow]。我觉得点击不正确,是不是应该是XUIApplication.alerts.buttons[Allow]?或者,您可以使用XUIApplication.alerts.buttons.elementboundBy:0点击第一个按钮 1/尝试使用XCode test recorder to alert按钮记录路径,并确保您拥有正确的路径
XCUIApplication().alerts["“AppName” Would Like to Send You Notifications"].buttons["Allow"].tap() //didn't work.
import XCTest
// For multiple permission requests
var isPhotoPermissionAvailable: Bool?
var isLocationPermissionAvailable: Bool?
var isNotificationsPermissionAvailable: Bool?
extension XCTestCase {
func allowPhotos(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Photo Permissions") { (alert) -> Bool in
isPhotoPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["OK"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isPhotoPermissionAvailable == nil {
XCUIApplication().swipeUp() //with this code, alert.buttons["OK"] exsists
}
}
func allowLocation(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
isLocationPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["Always Allow"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isLocationPermissionAvailable == nil {
XCUIApplication().swipeUp()
}
}
func allowNotifications(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Notification Dialog") { (alert) -> Bool in
isNotificationsPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["Allow"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isNotificationsPermissionAvailable == nil {
XCUIApplication().swipeUp()
}
}
}
可能不正确
注意:使用测试记录器做一些事情,比如找到元素的路径是可以的,而且对新手非常友好
2/您不必使用AddUI InterruptionMonitor with Description,因为您有一个系统警报,每次都会发生,或者您知道它会发生。
只要使用waitForExistance并点击它(如果它存在)
注意:我发现等待和点击系统通知比添加UIInterruptionMonitorWithDescription要好,后者有时不稳定/复杂
3/alert.buttons[Allow]。我觉得点击不正确,是不是应该是XUIApplication.alerts.buttons[Allow]?或者,您可以使用XUIApplication.alerts.buttons.elementboundBy:0点击第一个按钮 试试这个
import XCTest
// For multiple permission requests
var isPhotoPermissionAvailable: Bool?
var isLocationPermissionAvailable: Bool?
var isNotificationsPermissionAvailable: Bool?
extension XCTestCase {
func allowPhotos(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Photo Permissions") { (alert) -> Bool in
isPhotoPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["OK"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isPhotoPermissionAvailable == nil {
XCUIApplication().swipeUp() //with this code, alert.buttons["OK"] exsists
}
}
func allowLocation(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
isLocationPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["Always Allow"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isLocationPermissionAvailable == nil {
XCUIApplication().swipeUp()
}
}
func allowNotifications(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Notification Dialog") { (alert) -> Bool in
isNotificationsPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["Allow"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isNotificationsPermissionAvailable == nil {
XCUIApplication().swipeUp()
}
}
}
let app2 = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let button = app2.alerts.firstMatch.buttons["Allow"]
button.waitForExistence(timeout: 10)
button.tap()
试试这个
let app2 = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let button = app2.alerts.firstMatch.buttons["Allow"]
button.waitForExistence(timeout: 10)
button.tap()
非常有用。谢谢出于某种原因,系统对话不再是应用程序的一部分?iOS 14.4、Xcode 12.4UIInterruptionHandler也可以工作。您必须在中断处理程序块之后添加app.tap才能使其正常工作。但正如苹果公司所建议的,它应该只用于那些你不知道何时会发生的警报。非常有用。谢谢出于某种原因,系统对话不再是应用程序的一部分?iOS 14.4、Xcode 12.4UIInterruptionHandler也可以工作。您必须在中断处理程序块之后添加app.tap才能使其正常工作。但正如苹果所建议的,它应该只用于那些你不知道何时会发生的警报。