Android 为什么espressos视图层次结构与UI automators视图层次结构不匹配
这就是espresso转储视图层次结构的方式:Android 为什么espressos视图层次结构与UI automators视图层次结构不匹配,android,android-espresso,Android,Android Espresso,这就是espresso转储视图层次结构的方式: +------->AppBarLayout{id=-1, visibility=VISIBLE, width=1080, height=273, has-focus=false,... | +-------->Toolbar{id=2131296382, res-name=main_toolbar, visibility=VISIBLE, width=1080, height=147, ,... | +--------->AppC
+------->AppBarLayout{id=-1, visibility=VISIBLE, width=1080, height=273, has-focus=false,...
|
+-------->Toolbar{id=2131296382, res-name=main_toolbar, visibility=VISIBLE, width=1080, height=147, ,...
|
+--------->AppCompatTextView{id=-1, visibility=VISIBLE, width=185, height=71, has-focus=false, ,...
|
+--------->AppCompatImageButton{id=-1, desc=Open navigation drawer, visibility=VISIBLE, width=147, height=147, ,...
|
+--------->ActionMenuView{id=-1, visibility=VISIBLE, width=359, height=147, has-focus=false, has-focusable=true, ,...
|
+---------->ActionMenuItemView{id=2131296286, res-name=action_toggle_location_service, desc=Toggle location service, ,...
层次结构查看器:
UI自动机查看器:
令人困惑的是,从视觉上看,汉堡包菜单位于第一位,应用程序标题位于第二位
我努力进行以下测试:
final ViewInteraction textView = onView(
allOf(
withParent(withId(R.id.main_toolbar)),
withParentIndex(1)
)
);
// Test fails!!! with withParentIndex(1)
textView.check(matches(withText("MobiAd")));
上述示例适用于父索引(0)
有人知道UI Automator和espresso之间的区别是一个bug还是有原因的吗?我做了一个小样本应用程序,我想我已经知道了发生了什么。 您正在测试的应用程序可能有一个自定义工具栏,由设置 由于您的应用程序有一个导航抽屉,汉堡按钮将显示在自定义工具栏上,显然(如布局检查器所示)正在修改其层次结构 例如:
<android.support.v7.widget.Toolbar
android:id="@+id/R.id.main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c13030">
<!--This textview exists at index 0-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MobiAd"/>
</android.support.v7.widget.Toolbar>
当显示“导航抽屉”按钮时,它会作为其一部分出现在工具栏上,但您的文本视图仍然存在于索引0处,而不是1处
如果您想查看真正的层次结构,只需查看一下工具栏中的内容(找到声明了R.id.main_toolbar
的布局文件)
建议:为什么要通过查找文本索引来查找文本?如果它将被移动到另一个索引,您将不得不重构您的测试。
您可以使用它的字符串资源键(而不是写几行代码,您可以在一行代码中进行简单检查):onView(withText(R.string.toolbar_title)).check(匹配项(isDisplayed())代码>
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
//here is the magic
setSupportActionBar(toolbar);