How to get access into push notification with UI tests (Xcode 12, iOS14)?

Viewed 737

I'm during migration to work with new Xcode 12, but I have a problem with UI tests. Code

let springBoard = XCUIApplication(bundleIdentifier: appleBundleIdentifier) let notification = springBoard.otherElements["NotificationShortLookView"]

not working anymore and I can't find how to indicate notification view. How was it changed?

3 Answers

It seems like notification elements have a slightly different accessibility hierarchy in iOS 14. This should work:

let springBoard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let notification = springBoard.otherElements["Notification"].descendants(matching: .any)["NotificationShortLookView"]

It's interesting to observe that the actual XCUIElement that represents the push notification has type "BannerNotification" (an XCUIElementType with a rawValue representation 83) which I couldn't find in the public headers. Might be a private type at the moment. Hence the workaround with decendants(matching: .any).

Also accessing/asserting inside the notification is possible:

let notification = springBoard.otherElements["Notification"].descendants(matching: .any)["APPNAME, now, TITLE, BODY"]
    if notification.waitForExistence(timeout: 10) {
        notification.tap()
    }

If anyone is looking for testing if a notification has an attachment, add "Attachment" after notification Body

let notification = springBoard.otherElements["Notification"].descendants(matching: .any)["APPNAME, now, TITLE, BODY, Attachment"]

Related