With Xcode 9 this is now possible. You can get access to other apps. That includes the springboard which contains the Notification banner.
XCUIApplication
now has a new initializer that takes a bundle identifier as parameter. It gives you a reference to the app that uses that bundle identifier. You can then use the normal queries to access UI elements. Just like you did before with your own app.
This is a test that checks if a Local Notification is being displayed when the app is closed:
import XCTest
class UserNotificationUITests: XCTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
}
func testIfLocalNotificationIsDisplayed() {
let app = XCUIApplication()
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
app.launch()
// close app to trigger local notification
XCUIDevice.shared.press(XCUIDevice.Button.home)
// wait for the notification
let localNotification = springboard.otherElements["USERNOTIFICATION, now, Buy milk!, Remember to buy milk from store!"]
XCTAssertEqual(waiterResultWithExpectation(localNotification), XCTWaiter.Result.completed)
}
}
extension UserNotificationUITests {
func waiterResultWithExpectation(_ element: XCUIElement) -> XCTWaiter.Result {
let myPredicate = NSPredicate(format: "exists == true")
let myExpectation = XCTNSPredicateExpectation(predicate: myPredicate,
object: element)
let result = XCTWaiter().wait(for: [myExpectation], timeout: 6)
return result
}
}
You can checkout the demo app including this test here
You can also test Remote Notifications with UITests. That's needs a bit more work, because you cannot directly schedule Remote Notifications from your code. You can use a service called NWPusher for that. I wrote a blogpost about how to test Remote Notifications with Xcode UITests and there also is a demo project on github.
com.apple.springboard
doesn't seem to exist. I triedcom.apple.noticeboard
too, but it doesn't seem to be recognized either... – Lidia