其他分享
首页 > 其他分享> > 如何在Android或Android Espresso中获取视图层次结构

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

作者:互联网

我正在使用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}
| 

解决方法:

鉴于Android View层次结构是树形结构,使用某种算法更容易遍历每个树形节点,您可以在https://developer.android.com/reference/android/support/test/espresso/util/TreeIterables.html处查看这些方法

它打印出异常跟踪的方式使用了一个名为广度FirstViewTraversaland的方法,它在类ViewFinderImpl中打印出视图树
https://developer.android.com/reference/android/support/test/espresso/base/ViewFinderImpl.html

标签:android-espresso,android-testing,android
来源: https://codeday.me/bug/20191026/1934364.html