【发布时间】:2016-11-01 18:12:03
【问题描述】:
我正在尝试使用 Espresso 为我使用 RecyclerView 的 Android 应用程序编写仪器测试。这是主要布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
...和项目视图布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
这意味着屏幕上有几个TextViews,它们都有label id。我正在尝试单击特定标签。我试着让 Espresso 测试记录器生成代码:
onView(allOf(withId(R.id.grid), isDisplayed()))
.perform(actionOnItemAtPosition(5, click()));
我宁愿做这样的事情:
// Not working
onView(allOf(withId(R.id.grid), isDisplayed()))
.perform(actionOnItem(withText("Foobar"), click()));
读数
我阅读了很多关于该主题的内容,但仍然无法弄清楚如何根据其内容测试RecyclerView 项目不是基于其位置索引。
- Advanced Android Espresso - Slide on RecyclerViewActions(陈秋琪)
- RecyclerView and espresso, a complicated story(Patrick Bacon,2015 年 7 月 18 日)
- Espresso – Testing RecyclerViews at Specific Positions(Romain Piel,2016 年 4 月 15 日)
【问题讨论】:
标签: android android-recyclerview android-espresso android-espresso-recorder