Can I test push notifications using the Xcode UI Test?

We have apps in which we need to check push notifications regularly. Can we test push notifications through the XI-UI test?

+3


source to share


1 answer


Disclaimer: I didn't do it ...

You cannot create a token ... so I think the answer is No! What can do is mock your notificationManager with stub data.

With the stub data you are testing:

  • Does your callback function load what it wants?
  • Or maybe if you have warnings when the app is in the foreground: are you showing an alert?

Otherwise, if you are just testing before testing to see if the application is calling a callback when it receives a notification, then you are testing the OS and what is wrong. You only have to test your own code.

While checking your notification manager, I am something like:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        NotificationManager.shared.handle(userInfo)
}

      



NotificationManager class

func handleAPNSMessage(_ message:[AnyHashable: Any]) {

if let aps = message["aps"] as? NSDictionary {
    if let alert = aps["alert"] as? NSDictionary {
        if let message = alert["message"] as? NSString {
           //Do stuff
        }
    } else if let alert = aps["alert"] as? NSString {
        //Do stuff
        }
    }
}

      

Retrieved from here

Now what you do is you mock a call handleAPNSMessage

with a JSON object that looks just like the one your server will send you,

If you want to go crazy and really duplicate remote notifications, you can use your own stub data and create local notifications ... and see if the app works as you expect ...

EDIT:

Make sure you see How to check Apple Push Notification app without iPhone?

-2


source







All Articles