Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get view hierarchy in Android or Android Espresso

I am using Android Espresso and when it cannot find a match, it will throw an exception with printing the view hierarchy. Is there a way to get this kind of view hierarchy dynamically when you are running Android testing or Espresso

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=480, height=800,     has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=480, height=800, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16909225, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=-1, visibility=VISIBLE, width=480, height=764, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=36.0, child-count=1}
|
+--->ActionBarOverlayLayout{id=2131427395, res-name=decor_content_parent, visibility=VISIBLE, width=480, height=764, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
| 
like image 940
Pezh Avatar asked Mar 13 '17 21:03

Pezh


People also ask

How do I get espresso view?

Espresso uses onView (Matcher<View> viewMatcher) method to find a particular view among the View hierarchy. onView() method takes a Matcher as argument. Espresso provides a number of these ViewMatchers which can be found in the Espresso Cheat sheet.

What do you mean by view hierarchy in android?

Hierarchy Viewer is a tool built into Android Device Monitor that allows you to measure the layout speed for each view in your layout hierarchy. It can help you find performance bottlenecks caused by the structure of your view hierarchy.

What is espresso used for android?

Stay organized with collections Save and categorize content based on your preferences. Use Espresso to write concise, beautiful, and reliable Android UI tests. The core API is small, predictable, and easy to learn and yet remains open for customization.


1 Answers

You can cause a exception and View Hierarchy will appear. For example put this in your test file:

onView(withText("XYZ")).perform(click())

and since text XYZ does not exists, result will be:

...espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "XYZ"

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1024, height=600, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1024, height=600, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
...
like image 183
ivan.panasiuk Avatar answered Sep 28 '22 04:09

ivan.panasiuk