How can you see the XCUIElement tree? - ios

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.

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.

The way Appium does this is using Facebook WebdriverAgent.
As far as I can tell, the way they do it, essentially, is starting from the root application element and collecting information about each child, then recursing.

What about http://fbidb.io?
With idb ui describe-all command you get the accessibility information of all the elements on screen (not the entire app) https://fbidb.io/docs/commands#accessibility-info

Put a breakpoint in any of your tests then just do: po XCUIApplication() and that will print out the whole app's accessibility hierarchy in easy to read tree format.

Related

Get all elements in the view of iphone app using xcode

I am trying to take screenshot of every screen during iphone app automation on a simulator. Along with the screenshot I also want to extract all strings in the that particular view before taking a screenshot using xcode. Is there a way to do that? Purpose is to send these screenshots and strings for validation to another tool.
This can be done using Web Driver Agent(WDA) provided by facebook. It does provide all the functionalities you need for your app.
Here is link to github repo for it:
https://github.com/facebook/WebDriverAgent
Please have a look at it. It might help you achieving your goal.
If you are using the XCUITest framework for your automation, you can use XCUIScreen.main.screenshot() to get a screenshot of the current state.
To fetch all text currently on screen you can use XCUIApplication().descendants(matching: .textField) or .buttons or .any or whatever you expect to be on the screen, and extract the text from the element
let descendants = XCUIApplication().descendants(matching: .textField)
foreach descendant in descendants { descendant.label /*do something*/ }
You need to set an Accessibility Identifier on the view elements for this to work.

Why xpath Changes for UILabel for UITableViewCell in Appium?

I have UITableViewCell which has Subject,Name,Description,time. When I am testing this on Appium the xpath changed for UIElement for Subject line only.
xpath:-subject line: //UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[3]**/UIAStaticText[2]**
after changes
xpath:-subject line: //UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[3]**/UIAStaticText[3]**
Which is giving another UI element details, which fails the testing. How to handle this from Appium, or do I need to fix it from Xcode?
Basing on xpaths is not a really good idea.
But sometimes it is the only way to develop your scripts if there are no static ids nor accessibility labels available.
Your element that was previously set as: UIAStaticText[2], so as I suppose it was second element available at current screen. Now looks like UIAStaticText[3] - it is marked as third. If you are not able to see changes on the screen that you are automating, that can be caused because of an element that is under in the background it can be hidden.
The best approach will be with using static resource id's in your scripts or accessibility labels which you have mentioned in comment you've them already set and available.

How to print the hierarchy of views in iOS? [duplicate]

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?
I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.
Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.
The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX
Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.
Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()
This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)
The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

How do I inspect the view hierarchy in iOS?

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?
I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.
Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.
The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX
Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.
Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()
This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)
The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

Traversing the View Hierarchy on an iPhone or iPad [duplicate]

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?
I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.
Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.
The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX
Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.
Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()
This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)
The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

Resources