2

In my test code I am registering a method to listen for a particular event. My intention is when this method is called, I can continue test execution between screen transitions. Couple of approaches I am trying to solve this with.

Approach 1: (Notification Center for listening to events)

App code:
     NotificationCenter.default.post(name: Notification.Name.PageLoadCompleteTestReady, object: nil)

Test Code:
// Click to load a new screen
     NotificationCenter.default.addObserver(self, selector: #selector(self.printState), name: NSNotification.Name.PageLoadCompleteTestReady, object: nil)
// Continue execution on new screen.

Approach 2: (Implement the delegates under App module, by importing the Module in UI test code)

But none of the above methods seem to work. With approach 1: the notification observer on the test side is not being called. With approach 2: The class that implemented the delegate under test code base is not being called but on the Module is being called.

avi rao
  • 31
  • 6

1 Answers1

1

UI tests run outside your app and interact with at like a regular user would do. Thus, they don't have access to the app code flow, including notifications sent by the notification center.

What you can do is to assert the app state after triggering the conditions for the notifications to be sent. After all, that notification should result in some UI changes, otherwise testing it should be part of the unit tests.

For example if upon receiving the notification you present an alert, you assert that alert exists. Or if you change a visual indicator, assert on that. Or maybe you present a new screen, assert the new screen. Basically it depends on how your app visually reacts to the assert, since UI tests validate, after all, the UI

Cristik
  • 30,989
  • 25
  • 91
  • 127
  • Can you share an example for how to "assert the app state"? – avi rao Jan 21 '20 at 18:31
  • @avirao for example if upon receiving the notification you present an alert, you assert that alert exists. Basically it depends on how your app visually reacts to the assert, since UI tests validate, after all, the UI. – Cristik Jan 21 '20 at 18:33
  • Thanks for your answer. That's very helpful in confirming my observation. Will leave this thread open for a while, and mark your answer as accepted if no response. – avi rao Jan 22 '20 at 02:28