Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you see the XCUIElement tree?

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.

like image 654
Joe Susnick Avatar asked Sep 25 '15 22:09

Joe Susnick


3 Answers

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.

like image 157
h.w.powers Avatar answered Nov 14 '22 05:11

h.w.powers


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!

like image 12
Debo Avatar answered Nov 14 '22 05:11

Debo


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.

like image 3
Nick McConnell Avatar answered Nov 14 '22 06:11

Nick McConnell