I like the smell of Swift in the morning…

I recently worked on an app for a big retailer that made intensive use of remote notifications (aka push notifications). One use case: Whenever you use the app to pay for your shopping you will receive a remote notification when the payment was successful. Tapping on the remote notification would launch the app and show the receipt for the transaction.

As I used a lot of Xcode UITests on this project I was wandering if it is possible to test the remote notification handling in an UITest. It turns out that it is possible. There are two things that need to happen during the test:

1. Trigger a mock remote notification
2. Tap on the remote notification when it is received

Triggering a remote notification is relative easy thanks to the work of the fine folks at noodlework. They build a framework to play around with the Apple Push Notification service (APNs). Using their framework (NWPusher) makes it possible to trigger a remote notification right from a UITest class.

There is one new feature that comes to the XCUITest framework with Xcode 9: Multi App Testing. It is now possible to interact with other apps (and the Springboard) when running a UITest 🎉. So with Xcode 9 we can wait for the remote notification to appear and tap on it. Yay!

So, let’s do it.

Set up a dummy application (if needed)

I’m assuming that you already have an app that uses remote notifications. Otherwise just setup a simple app like I did. My app has a main view controller (with a gray background) and three child view controllers with different background colors that are presented modally based on the remote notification that was received:

My remote notification’s payload contains a key vcType that can have three values red, blue or green. When you tap on the remote notification the app looks for that key to determine which view controller to present.

This is the remote notification payload that makes the app present a red view controller:

1

2

3

4

{

"aps":{"alert":"Hello Red"},

"vcType":"red"

}

If you are setting up a new app for this remember to activate the PushNotification capabilities in your target.

Trigger a remote notification in a UITest

1. Install NWPusher
Add NWPusher to the UITest target of your app. I use Carthage to do that but you can also use CocoaPods or add their source files directly to your app.

2. Add push notification certificate to your app
To trigger a remote notification you need to add a valid Apple Push Notification service SSL certificate to the UITest target of your app.

There is a detailed description on how to do this in the NWPusher repo, so I keep it short here:
– Create a Development APN Certificate on the Apple developer center (if you do not already have created one).
– Download the certificate and add it to your Keychain
– Export the certificate to a PKCS12 file. Set a password (and remember it ;-))
– Add the p12 file to the UITest target.

Now you are ready to trigger a Remote Notification right from your UITest’s code.

3. Trigger a remote notification from a UITest
I created a helper function to trigger a remote notification because I want to trigger multiple notifications:

The code loads the certificate file into a Data object, connects to NWPusher’s service and triggers the remote notification.

You might have noticed that this function expects a deviceToken that is needed for the NWPusher service. That is a bit of a problem, because the deviceToken changes everytime you install the app or run the test on a different device.

To get around this I use a dirty hack. I do not really like this but I could not think of another way to make the deviceToken available to the UITest. If you have a better idea, please let me know in the comments!

To make the deviceToken accessible to the UITest I add a launch argument “isRunningUITests” when launching the app from a UITest. The app checks if the launch argument is present and if it is, adds a tiny UILabel to its root view controller and sets the label’s text to the device token. The UITest class then can read the deviceToken from that label. Shudder. Really ugly, I know.

The alternative would be to manually set the deviceToken when running the UITests. Not an option.

So now we can trigger a remote notification from a UITest class. Neat!

One more thing: When you run the app for the first time a system dialog will pop up to ask the user’s permission to send remote notifications. This also happens during a UITest, so we have to dismiss this dialog during the test.

I added a helper function that dismisses the system dialog:

1

2

3

4

5

6

7

8

9

10

func allowPushNotificationsIfNeeded(){

addUIInterruptionMonitor(withDescription:"“RemoteNotification” Would Like to Send You Notifications"){(alerts)->Boolin

if(alerts.buttons["Allow"].exists){

alerts.buttons["Allow"].tap()

returntrue

}

returnfalse

}

XCUIApplication().tap()

}

The tap on the XCUIApplication seems a bit strange but you have to interact with the app after setting up the interruption monitor to make it work.

Write the UITest that tests the 3 different remote notifications

Now we can write a UITest that does the following steps:

1. Hold a reference to the app and the springboard
2. Get the deviceToken from the app
3. Close the app
4. Trigger a “red” remote notification
5. Tap on the notification
6. Assert that the red view controller is shown
7. Close the app
8. Trigger a “green” remote notification
9. Tap on the notification
10. Assert that the green view controller is shown
11. Close the app
12. Trigger a “blue” remote notification
13. Tap on the notification
14. Assert that the blue view controller is shown

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

func testPushNotifications(){

let app=XCUIApplication()

app.launchArguments.append("isRunningUITests")

app.launch()

// access to the springboard (to be able to tap the notification later) [1]

I added sleep statements after closing the to make sure that the app is fully in background mode before triggering the remote notifications.

Now we can run the test and Voila! UITests that test the handling of remote notifications! Just don’t try to run the tests in a Simulator as remote notifications only work on real devices.

You can checkout the demo app including the UITest here. Make sure to add the demo app as your own app to your developer account and to import your own APN development certificate (the p12 file) to the project to make it work.

8 Comments

Now your code not call application(_ application:,get deviceToken:) and token didn’t set.
And get error in application(_ application:, didFailToRegisterForRemoteNotificationsWithError error:).
Failed to register: Error Domain=NSCocoaErrorDomain Code=3010 “remote notifications are not supported in the simulator”

Its not working. 🙁

2

Jörnsaid at 1:13 pm on November 5th, 2017:

Hi Navka,
thanks for your comment. According to the error message you posted, you are trying to run the project in the simulator. Push Notifications do not work in the simulator, you have to use a real device.

How would I go about expanding/long pressing the notification first before tapping it?

Thanks!
-Adrian

6

Jörnsaid at 12:55 pm on August 30th, 2018:

Hi Adrian, instead of calling ‘tap’ on the XCUIElement that represents the notification (step [13]) you should be able to call press(forDuration:) to simulate a long press and one of the swipe methods to interact with the notification. Unfortunately XCUITest does not provide any means to simulate a force press, so you are out of luck there.

Hi Adrian,
Since notification pop-up won’t be present for a longer duration. I wanted to test a scenario wherein User receives a notification. User wait for 2-3 mins(notification will be gone). User opens notification window and from there reads notification.
So the query I have is how to reach to the notification window. I have tried using swipe down on springboard but it does not help and I could not find any XCUI method to open that window.
Thanks
Vayuj

8

Jörnsaid at 1:49 pm on October 15th, 2018:

Hi Vayuj,

have a look at this gist. There I implemented some helper methods to open NotificationCenter or ControlCenter during a running UITest. Have a look at the gist’s comment for usage examples.