Xcode 9 GM , UITests , addUIInterruptionMonitorWithDescription:handler: does not fire

I've downloaded the latest xCode 9 GM and I face an issue with :

- (id <NSObject>)addUIInterruptionMonitorWithDescription:(NSString *)handlerDescription handler:(BOOL (^)(XCUIElement *interruptingElement))handler; - method . I use it in a test case , where I have to dissmiss a System Notification . If I place the breakpoin inside the block - it appears that the block is never called , and the Notification is never dissmissed , so my test do not pass . Here is some code I use :


[self addUIInterruptionMonitorWithDescription:@"MyApp Would Like to Send You Notifications" handler:^BOOL(XCUIElement * _Nonnull interruptingElement) {


XCUIElement *element = interruptingElement;

XCUIElement *allow = element.buttons[@"Allow"];


if ([allow exists]) {

[allow tap];

return YES;

}

return NO;

}];

// tap somewhere in the app , workaround for a known issue

[self.app tap];


The code above work just fine in the previous xCode 8.3.3 , so if there are any ideas how to solve that , it will be great . Thanks in advance .


EDIT :


It appear that it is connected with the simulator that I am using , if I use the iPhone 8 Simulator , the method works just fine , in all other cases : iPhone 5, iPhone 5s, iPhone 6, iPhone 6 Plus, iPhone 7, iPhone 7 Plus - it behave just like I wrote above , Simulator versions are 10.3.1 , exept the 8th iPhone line - version 11 .

Replies

I see this happening too with the new Xcode 9 GM build with iOS versions less than 11. This works perfectly with iOS 11 though.

So this happens anytime you use a device/simulator with less than iOS11?

I just tries on an iphone 7 with iOS 10.3.3 and it does not work since i made the upgrade to Xcode 9.

As the prevoius comments have mentioned, I am seeing the same thing that XCode 9 GM works great with System Alerts / Dialgos when iOS 11 only regardless if the test is run on a simulator or device.


Does anyone know of a workaround for this issue? It would be great to run the same test on iOS 10 & 9 as well.


Thanks,

Paul

It appears that the interuption handler can only fire after user interaction with the app. If you add a `app.tap()` then you should see your handler being called. It appears that the handler is being called as a closure to the `tap` event.


Hope this helps (:

I have the same problem. addUIInterruptionMonitor seems to work just fine for custom alerts but is not working for notification alerts.

The solution that worked for me was to interact with the app in a way that doesn't accidentally trigger anything such as swiping down then up.


XCUIApplication().swipeUp()

XCUIApplication().swipeDown()


You want to do this in the places you *think* the system alert should appear.


In practice your UITest will present the system alert. At this point the swipeUp and swipeDown must be done. The interrupt handler *should* get triggered.


Some solutions may suggest tapping, but they might accidentally tap a button when you didn't mean to.


tl;dr;

wake up the UITest by swiping it until it notices the system alert.

Exactly the same problem with XCode 9: In my case the interruption monitor works on iOS 11, but it doesnt on iOS 9.3 which I need to run my tests on iPhone 4s. The following workaround works for me in the case of system notifications:


let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let allowBtn = springboard.buttons["Allow"]
let okBtn = springboard.buttons["OK"]
let exists = NSPredicate(format:"enabled == true")
let expectBtns = [
     expectation(for: exists, evaluatedWith: allowBtn, handler: nil),
     expectation(for: exists, evaluatedWith: okBtn, handler: nil)
]
_ = XCTWaiter.wait(for: expectBtns, timeout: 3.0)
allowBtn.exists ? allowBtn.tap() : okBtn.tap()
_ = expectBtns.map { $0.fulfill() }


Note that I check for either "Allow" or "OK" because the permission dialog on iPhone 4s is different, and I mark both expectations as fulfilled to avoid waitForExpectations further down in my test to break

This did not work for me. I tried on Xcode 9.2 and iOS 10.3.

This did not work for me either. I tried on Xcode 9.2 and iOS 10.3.

Hmm, seems to be working for me on XCode 9.2 with iOS 10.3.1