We have some apps where we need to check push notifications in regular interval. Can we check push notifications via Xcode UI test?
-
I made a few edits to my answer, please take a look – mfaani Apr 11 '18 at 13:30
1 Answers
Disclaimer: I haven't done this...
You can't generate a token...so I guess the answer would be No! What can do is a mock your notificationManager with stub data.
With the stub data you test:
- Does your callback function download what want it to?
- Or maybe assuming you have alerts when app is in foreground: are you showing an alert?
Otherwise if you're purely up to testing to see if the app throws the callback when it receives a notification then you're testing the OS and that's wrong. You should only test your own code.
By testing your notificationManager I roughly mean something like:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NotificationManager.shared.handle(userInfo)
}
Your 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
}
}
}
Derived from here
Now what you do is, you mock calling handleAPNSMessage
with a JSON object that looks same as the one your server would send you,
If you want to go crazy and really duplicate the remote notifications then you could use your own stub data and create local notifications... and see if the app works as you expect...
EDIT:
Make sure you see How can I test Apple Push Notification Service without an iPhone?

- 33,269
- 19
- 164
- 293
-
I can trigger push notification from a tool which we developed. Thats not a problem. I'm asking whether can I get elements and its identifiers via XCTestCase from push notifications? – Confused Jun 14 '17 at 09:09
-
@Confused not sure what you mean by 'elements' and 'identifiers'. I mean you're sending a object through payload right? You can get all its keys/values —assuming you can trigger it – mfaani Jun 14 '17 at 09:40
-
I asked this question about XCTest UI testing, not with notification related mechanism. – Confused Jun 14 '17 at 11:06
-
-
@Confused, how did you trigger the push notifications with uitests? – ScottyBlades Oct 30 '17 at 07:09
-