RecyclerView espresso测试点击()不起作用

时间:2016-01-27 10:05:27

标签: android android-recyclerview ui-testing android-espresso

我遇到了奇怪的浓缩咖啡仪器测试行为。单击回收站视图的项目不起作用。

点击此处未发生

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

但是这种解决方法有效

onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(0, recyclerClick()));

// ...

public static ViewAction recyclerClick() {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return any(View.class);
        }

        @Override
        public String getDescription() {
            return "performing click() on recycler view item";
        }

        @Override
        public void perform(UiController uiController, View view) {
            view.performClick();
        }
    };
}

这是Espresso还是RecyclerView问题?

2 个答案:

答案 0 :(得分:2)

应该与RecyclerView具体无关。 Espresso的ViewActions.click()实施功能将MotionEvent发送到目标视图的中心。确保没有孩子拦截它。

答案 1 :(得分:0)

您可以使用RecyclerViewActions

只需使用:

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

例如:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
  @Rule
  public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);

  @Test
  public void clickItem() {
    onView(withId(R.id.recycler_view))
        .perform(RecyclerViewActions.actionOnItemAtPosition(o, click()));

    onView(withId(R.id.text))
        .check(matches(withText("XX")))
        .check(matches(isDisplayed()));
  }
}