如何在Android或Android Espresso中获取视图层次结构

13

我正在使用Android Espresso,当它找不到匹配项时,会抛出异常并打印视图层次结构。在运行Android测试或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}
| 
3个回答

10

您可以引发异常,这样将会出现视图层次结构。例如,将以下内容放置在您的测试文件中:

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

由于文本XYZ不存在,结果将是:

...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}
|
...

1

我可以问一个额外的问题,如何获取当前活动以获取根视图并将其传递给函数? - Pezh

1

HumanReadables.getViewHierarchyErrorMessage是Espresso在视图匹配失败时用于打印图形的函数,因此不需要抛出虚假的匹配并捕获它,只需:

\\ import androidx.test.espresso.util.HumanReadables
val theRootView = fragment.view
val viewHierarchy: String = HumanReadables.getViewHierarchyErrorMessage(theRootView, null, "", null)

你需要传递一个视图。例如,如果你正在使用一个Fragment,fragment.view将是图形的根。

4
你能否专门编辑你的答案,添加更多细节并解释为什么你的答案可以解决这个问题? - Elikill58

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接