RecyclerView
を使用しているAndroidアプリのエスプレッソで計測テストを書こうとしています。ここでは主なレイアウトは次のとおりです。Espressoが特定のRecyclerViewアイテムをクリックする方法を教えてください。
<?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>
これは全label
IDを持って、画面上のTextViews
のカップルがあることを意味します。特定のラベルをクリックしようとしています。私はエスプレッソテストレコーダーを聞かせしようとしたコードを生成します。
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 itemViewに対して(パトリック・ベーコン、2015年7月18日)
- Espresso – Testing RecyclerViews at Specific Positions(ロマンPIEL、2016年4月15日)
'hasDescendant()'は、深度ビューがネストされているかどうかにかかわらず、階層レベルを自動的に走査しますか?あるいは、 'hasDescendant(hasDescendant(...))'を使う必要がありますか? – JJD
うん、 'hasDescendant()'は 'onView()'部分にあるルートからビュー階層全体を扱います。 recyclerviewアクションの場合、ルートは 'itemView'です。 –