How can you see the XCUIElement tree? How can you see the XCUIElement tree? xcode xcode

How can you see the XCUIElement tree?


Set a break point where you would like to see the tree... in the debugger type:

po print(XCUIApplication().debugDescription)

That prints out everything XCUITesting has access to. You can also just throw that in to your test:

func testTreeExample() {  XCUIApplication().buttons["login"].tap()  print(XCUIApplication().debugDescription)  XCUIApplication().buttons["next"].tap()  print(XCUIApplication().debugDescription)}

Thta way if you are having trouble finding something you can have it automatically print out what the app sees right after you do something.


This isn't exactly what you're asking for, but Xcode’s Accessibility Inspector makes it much easier to look at your view hierarchy in terms of what elements are accessible via Identifiers. (N.B. It's not the "Label" in IB's Accessibility panel that matters, it's the "Identifier" field.):

In Xcode 7.2, open Xcode->Open Developer Tool->Accessibility Inspector. (You may need to give the app permission to run in System Preferences.) Then launch your iOS app from Xcode and hover over any UI element in the SIMULATOR. You’ll see comprehensive information about the element type, description, hierarchy, etc.

Anytime you record UI actions and the output doesn't look right, use the tool to figure out what accessibility descriptions need to be added, changed, or removed. (I spent a couple days trying to get a deeply embedded UISegmentedControl to change via the UI Test harness, and the problem became obvious once I figured out how to use the Accessibility Inspector tool.)

Thanks to the folks at shinobicontrols.com for the great tip!


I would suggest choosing from the menu bar: Debug > View Debugging > Capture View Hierarchy when running in debug. Not only do you a way of visually representing the views but also the left-side debug navigator shows the hierarchy. This may not be one-for-one with UI Testing's perspective but it can be very helpful. Hope that helps.