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

3

5

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?

Vulvitis answered 17/9, 2020 at 8:38 Comment(2)
Please add more details in order to explain your question clearly to other users.Hf
I just want to tap on this notification - notification.tap()Vulvitis
J
4

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).

Jauregui answered 30/9, 2020 at 7:2 Comment(0)
M
2

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()
    }
Measles answered 31/3, 2021 at 20:5 Comment(1)
For anyone trying this, it's important to note that APPNAME is what shows up in the "title header" of the notification and it is in ALL CAPS. Also, it is an exact match on title and body, so even whitespace can make a difference in your assertions.Dukedom
B
1

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"]

Bonkers answered 2/9, 2021 at 0:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.