XCTest - addUIInterruptionMonitor not working on iOS 17

For my test automation project I use addUIInterruptionMonitor to keep the tests running smoothly. On iOS 17 this function seems to never get triggered. This code snippet contains a test that passes on iOS 15 and 16, but not on iOS 17 (tested on iPhone 12, iOS 15.5, iPhone 13 Mini, iOS 16.4.1 and iPhone 14 Pro, iOS 17.0).

To run the test, disable wifi and cell data on the test device first.

import XCTest

class Interruptions: XCTestCase {
    func testCatchInterruption() {
        // Run test on device with wifi and cell data disabled
        var caughtInterruption = false
        addUIInterruptionMonitor(withDescription: "Generic Alert Handler") { element -> Bool in
            element.buttons["OK"].tap()
            caughtInterruption = true
            return true
        }
        let safariApp = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
        safariApp.launch()
        // Expect to see pop-up saying "Cellular Data is turned off"
        safariApp.tap()
        sleep(5)
        // Pop-up should be gone
        XCTAssert(caughtInterruption)
    }
}
Post not yet marked as solved Up vote post of danielrtlv Down vote post of danielrtlv
1.9k views
  • A small clarification - to get the pop-up so that it appears in the middle of the screen, disable cell data only for the Safari app. Otherwise the pop-up is shown at the top of the screen and doesn't interfere with the tap on Safari.

Add a Comment

Replies

Maybe safariApp.tap() works around the interruption by finding a spot to tap that is hittable in iOS 17- so in the mind of the test, the interruption didn't block the test from executing.

It depends what your test output looks like, to determine what could be the issue here.

  • Thanks for you response, but that doesn't seem to match my experience. Xcode shows the test as tapping directly on the pop-up, and I have had other cases where the pop-up definitely interfered with the test but was not dealt with. Have you seen this function working in iOS 17.0?

Add a Comment

I'm having the same issue. The code works with previous iOS Versions (iOS 16, iOS 15 and iOS 14). The code is pretty simple, only allows the app to use Network:

        addUIInterruptionMonitor(withDescription: "Generic Alert Handler") { element in
            let button = element.buttons.element(boundBy: 1)
            if button.exists {
                button.tap()
            }
            return true
        }

When I run the XCUITest using iOS 17 (17.0.3) and Xcode Version 15.0 (15A240d), I'm getting the next:

Find: Descendants matching predicate identifier == "NotificationShortLookView" OR elementType == 7

    t =    33.05s Failed to construct element query matching interruption. Interrupting element Alert, foreground application Application 'com.apple.springboard', element application: (null). If your test failed after this unhandled interruption, please file a bug and attach your .xcresult bundle.

The interruption does not allow to continue with the test and it fails. Also, there is a clear null value in element application: (null).

I'm having the same issue too. Hope Apple team can do something to fix this, and i have raised a Feedback ticket FB13190617.

  • Thanks for filing FB13190617.

Add a Comment

Not seeing this repeat after running this code locally. Using the UNIX sleep() method in UI testing code is not recommended, so maybe that is part of the issue here? sleep() blocks the main thread, which is also used by UI testing.

Update:

It seems like this IS a bug in iOS 17. Alerts generated by SpringBoard do not get automatically handled by the XCTest interruption handler. However, Alerts generated by your application are still handled properly.

There is a workaround, which is to manually call into SpringBoard and tap a button to close the Alert. This workaround would need to be executed outside the context of an interruption handler.

Here is a code example that does that:

let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
springboard.buttons["Close"].tap()

Apologies for this. We are investigating a longer-term fix.

  • Thank you for looking in to this. I'll see if I can apply the workaround in my project.

  • Is this fixed?

Add a Comment

@Developer Tools Engineer Hello, currently I'm using the workaround, is there any update regarding the addUIInterruptionMonitor for iOS 17? Thanks in advance.

Thanks for this thread. I also was working on UI tests the other day, and today when I came back the addUIInterruptionMonitor() thing didn't work anymore. Seems like I was using an iOS 16 device that other day, and today an iOS 17. It seems the workaround also worked.

Please fix this. And possibly ping this thread when it is?

Thanks everyone.

I am facing a similar issue with contacts permission on iOS 17 and iOS 16.4

@Developer Tools Engineer @eskimo FB13635680