Article

Handling UI Interruptions

Improve your UI test’s stability by handling interface changes that block the UI elements under test.

Overview

Use XCTestCase UI interruption monitors to handle situations in which unrelated UI elements might appear and block the test’s interaction with elements in the workflow under test. The following situations could result in a blocked test:

  • Your app presents a modal view that takes focus away from the UI under test, as can happen, for example, when a background task fails and you notify the user of the failure.

  • Your app performs an action that causes the operating system to present a modal UI. An example is an action that presents a photo picker, which may make the system request access to photos if the user hasn’t already granted it.

Add a UI Interruption Monitor to Your Test

Call addUIInterruptionMonitor(withDescription:handler:) with a block that handles the interrupting UI and gets the test back on its expected path. Your block should return true if it clears the interruption, and false otherwise. UI interruption monitors are stored in a stack and their blocks are tried in last-in, first-out order, so returning false gives other interruption monitors the chance to run.

XCTest removes added UI interruption monitors when the test completes, so it’s not necessary to explicitly remove them. However, if you need to remove a monitor during test execution, call removeUIInterruptionMonitor(_:).

See Also

Monitoring UI Interruptions