【发布时间】:2017-11-13 06:51:25
【问题描述】:
我已经开始使用 Espresso 来自动化我的测试,我面临的问题是,我无法匹配我的 xml 文件中的确切文本视图。
示例 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 并且有父匹配: with 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() 方法。不工作。请帮帮我。
【问题讨论】:
标签: android android-layout automation android-espresso