UI Tests + postNotificationName + never reaches observer + Xcode 7
Asked Answered
P

3

9

I have UI Tests target for testing MyApp. To test specific MyApp conditions I need to post notifications from UI Test target to MyApp target. To post notification from UI Test target function I am using this:

NSNotificationCenter.defaultCenter().postNotificationName(name, object: nil, userInfo: aUserInfo)

It looks that this notification never reaches observer from UI Test target, but it works fine when posting this notification from MyApp target.

How to post notification from UI Target to MyApp target?

Using Xcode 7.

Pederasty answered 10/11, 2015 at 15:18 Comment(5)
Are you calling addObserver where you want to listen? Are you sure the addObserver is being called before postNotification?Basement
Posting notification from MyApp target works fine, but when posting from UI Tests target dose't. I updated question as well.Pederasty
Off topic, but it is good practice (or convention?) to send self as the object argument.Bellwort
The opposite is also true (for my case at least): observer from UI Test target cannot see notifications posted from main app, but see notifications posted from inside test.Tranquilizer
Quick question, would we be able to use sockets to send / receive data here without entitlements?Theatricals
T
8

Have similar problem (trying to ensure NSNotification is being posted after certain UI action). Did small research on this.

NSNotification not being received because UI test and app are running in different processes. NSNotification cannot go through process bounds, and NSDistributedNotificationServer is not available on iOS. So, currently there is no default and easy way to post NSNotifications between UI test suite and app instance.

However, there is some ways to communicate between processes, and maybe write small wrapper or even NSDistributedNotificationCenter iOS surrogate for testing purposes. Check out this great article from Realm: https://academy.realm.io/posts/thomas-goyne-fast-inter-process-communication/

Tranquilizer answered 12/1, 2016 at 10:29 Comment(0)
J
3

I wanted to use UI Testing to test for memory leaks and to do this I wanted to get informed in the UI test case when ever a view controller's deinit is called. So I came up with this to provide the IPC mechanism:

/**
  Provides simple IPC messaging. To use this class you need to include     
  #include <notify.h>
  in your bridging header. (Ab)uses UIPasteboard for message delivery, ie. 
  should not be used in production code.
*/
public class SimpleIPC {
  /// Event notification name for libnotify.
  private static let notifyEventName = "com.foo.SimpleIPC"

  /// libnotify token.
  private var token: Int32 = 0

  /// Starts listening to the events
  public func listen(callback: (String? -> Void)) {
      notify_register_dispatch(SimpleIPC.notifyEventName, &token, dispatch_get_main_queue()) { token in
          callback(UIPasteboard.generalPasteboard().string)
      }
  }

  public class func send(message: String) {
      UIPasteboard.generalPasteboard().string = message
      notify_post(SimpleIPC.notifyEventName)
  }

  deinit {
      notify_cancel(token)
  }
}

It uses a combination of libnotify and UIPasteboard for the notification + data delivery. Usable for 1-way communication as is, for 2-way either make the payload include a sender token or use 2 instances with parametrized libnotify event names.

  • Matti
Jeneejenei answered 2/7, 2016 at 16:38 Comment(1)
How would the 2way work over here? I've been having problems setting it up. What exactly the sender token here?Theatricals
A
0

Is this for a Mac app or an iOS app? For a Mac app you can use NSDistributedNotificationCenter as such.

In your subscriber:

NSDistributedNotificationCenter.defaultCenter().addObserver(object, selector: #selector(ObjectsClass.myFuncWithoutParentheses), name: "uniqueString", object: nil)

In your publisher:

NSNotificationCenter.defaultCenter().postNotificationName("uniqueString" object:nil)
Autoclave answered 17/8, 2016 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.