<!--
{
  "documentType" : "article",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/handling-ui-interruptions",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Handling UI Interruptions"
}
-->

# Handling UI Interruptions

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

## Discussion

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.

> Important:
> When an alert or other modal UI is an expected part of the test workflow, don’t write a UI interruption monitor. The test won’t use the monitor because the modal UI isn’t blocking the test. A UI test only tries its UI interruption monitors if the elements it needs to interact with to complete the test are blocked by an interruption from an unrelated UI.

### Add a UI Interruption Monitor to Your Test

Call [`addUIInterruptionMonitor(withDescription:handler:)`](/documentation/XCTest/XCTestCase/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(_:)`](/documentation/XCTest/XCTestCase/removeUIInterruptionMonitor(_:)).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)