Exploring the XCUIElement Hierarchy in Xcode UI Tests

Exploring the XCUIElement Hierarchy in Xcode UI Tests

5 April 2024 Stephan Petzl Leave a comment Tech-Help

When conducting UI testing in Xcode, it’s crucial to understand the structure and relationships of the UI elements within your application. This knowledge allows you to effectively select and manipulate these elements during testing. Let’s explore how you can visualize the hierarchy of accessible elements, similar to viewing the DOM tree in web development.

Printing the XCUIElement Tree

To inspect the element tree during a testing session, you’ll need to set a breakpoint at the desired point in your code. Once the breakpoint is hit, you can use the Xcode console to print out the entire view hierarchy that the UI testing framework has access to.

Here’s a step-by-step guide to achieve this:

  1. Set a breakpoint in your UI test where you need to inspect the view hierarchy.
  2. Once the breakpoint is hit, open the console in Xcode.
  3. Type the following command and press enter:
po print(XCUIApplication().debugDescription)

This command outputs the entire hierarchy of XCUIElements that the XCUITesting framework can interact with.

Incorporating Hierarchy Inspection into Test Functions

If you frequently need to check the element hierarchy, consider adding code directly into your test functions to print this information. Below is an example of how you might do this:

func testTreeExample() {

With this implementation, the hierarchy is printed out automatically after certain interactions, making it easier to troubleshoot issues with element selection or manipulation.

Using the Accessibility Inspector

While the method above is useful, Xcode’s Accessibility Inspector offers a more visual approach to inspecting your view hierarchy. This tool provides detailed information about UI elements, such as type, description, and hierarchy—all in terms of accessibility identifiers.

Here’s how you can use the Accessibility Inspector:

  1. Open Xcode and navigate to Xcode -> Open Developer Tool -> Accessibility Inspector.
  2. Start your iOS app from Xcode and hover over any UI element within the simulator.
  3. The Accessibility Inspector will display comprehensive details about the selected element.

This tool is particularly useful when UI test recordings do not behave as expected, allowing you to determine what changes are needed for accessibility descriptions.

Additional Debugging Tools

For a more graphical representation of the view hierarchy, you can also use Xcode’s built-in view debugging features:

  • Select Debug -> View Debugging -> Capture View Hierarchy while your app is running in debug mode.
  • This will present a visual representation of the views, along with a hierarchy view in the debug navigator.

Although this doesn’t provide a direct one-to-one match with the UI Testing framework’s perspective, it can be a valuable tool for understanding the layout and structure of your app’s UI.


Understanding the XCUIElement hierarchy is essential for effective UI testing in Xcode. By using the debugDescription command, incorporating hierarchy inspection into your test functions, and utilizing the Accessibility Inspector, you can gain valuable insights into the structure of your app’s UI. These tools will aid you in creating robust and reliable UI tests that ensure the quality of your application’s user interface.

Like this article? there’s more where that came from!