Using Espresso to click view inside RecyclerView item

后端 未结 9 1916
醉酒成梦
醉酒成梦 2020-12-02 05:36

How can I use Espresso to click a specific view inside a RecyclerView item? I know I can click the item at position 0 using:

onView(withId(R.i

9条回答
  •  春和景丽
    2020-12-02 06:11

    Now with android.support.test.espresso.contrib it has become easier:

    1)Add test dependency

    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
    

    *exclude 3 modules, because very likely you already have it

    2) Then do something like

    onView(withId(R.id.recycler_grid))
                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
    

    Or

    onView(withId(R.id.recyclerView))
      .perform(RecyclerViewActions.actionOnItem(
                hasDescendant(withText("whatever")), click()));
    

    Or

    onView(withId(R.id.recycler_linear))
                .check(matches(hasDescendant(withText("whatever"))));
    

提交回复
热议问题