我已经开始使用Espresso来自动化我的测试和我遇到的问题,我无法匹配我的xml文件中的确切textview。
示例xml如:
xmlns:app="http://schemas.xx.com/apk/res-auto"
android:id="@+id/filter_sort_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="2dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:animateLayoutChanges="true"
android:shrinkColumns="0">
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/dropdown_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:clickable="true"
android:ellipsize="end"
android:focusable="true"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingBottom="15dp"
android:paddingEnd="5dp"
android:paddingTop="15dp"
android:textColor="@color/colorAccent" />
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="3dp"
app:srcCompat="@drawable/ic_triangle" />
</LinearLayout>
我试图检查id'dropdown_title'是否存在并且必须从中获取文本。我使用的代码:
onView(allOf(
withId(R.id.dropdown_title),
withParent(withId(R.id.filter_sort_layout)))).
check(matches(isDisplayed();
但是,它让我错误地说,
'android.support.test.espresso.NoMatchingViewException:层次结构中找不到匹配的视图:(带有id:xx.debug:id / dropdown_title并且父匹配:id:xx / filter_sort_layout)
查看层次结构:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, 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=3}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1776, 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=16909227, 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=1080, height=1704, 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=72.0, child-count=1}
|
+--->FitWindowsLinearLayout{id=2131820663, res-name=action_bar_root, visibility=VISIBLE, width=1080, height=1704, 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}
|'
我尝试过使用descendantOfA()
和hasSibling()
方法。不行。请帮帮我。
答案 0 :(得分:1)
我认为您当前的代码失败了,因为dropdown_title
不是filter_sort_layout
的直接子代。相反,dropdown_title
是filter_sort_layout
的后代。请改用isDescendantOfA
:
onView(allOf(withId(R.id.dropdown_title),
isDescendantOfA(withId(R.id.filter_sort_layout))))
.check(matches(isDisplayed()));
修改强>
从下面的评论看来,OP实际上在应用程序中具有相同名称的倍数视图。重复链接包含如何处理此方案。