How can you see the XCUIElement tree?

Joe Susnick picture Joe Susnick · Sep 26, 2015 · Viewed 12k times · Source

Background:

I'm experimenting with ui level testing in iOS 9.0 with XCode GM.

Question:

Is there a command in XCode GM that will allow you to see a 'tree' of accessible elements and their relationships? Something similar to the 'page' command in Appium?

Ideally I would be able to run a command in the debugger that would give me a list of elements available for selection/manipulation. Currently you can use debugDescription on a single XCUIElement but that only gives you info for that element.

Answer

h.w.powers picture h.w.powers · Jan 16, 2017

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.