使用Espresso在RecyclerView项目中单击视图

如何使用Espresso来点击RecyclerView项目中的特定视图? 我知道我可以点击位置0的项目使用:

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

但是我需要点击该项目内的特定视图,而不是项目本身。

提前致谢。

– 编辑 –

更确切地说:我有一个RecyclerViewR.id.recycler_view ),其中的项目是CardViewR.id.card_view )。 在每个CardView内部,我有四个button(其中包括),我想单击一个特定的button( R.id.bt_deliver )。

我想使用Espresso 2.0的新function,但我不确定这是可能的。

如果不行的话,我想用这样的东西(使用Thomas Keller代码):

 onRecyclerItemView(R.id.card_view, ???, withId(R.id.bt_deliver)).perform(click()); 

但是我不知道要把什么放在问号上。

您可以使用自定义视图操作。

 public class MyViewAction { public static ViewAction clickChildViewWithId(final int id) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return null; } @Override public String getDescription() { return "Click on a child view with specified id."; } @Override public void perform(UiController uiController, View view) { View v = view.findViewById(id); v.performClick(); } }; } } 

然后你可以点击它

 onView(withId(R.id.rv_conference_list)).perform( RecyclerViewActions.actionOnItemAtPosition(0, MyViewAction.clickChildViewWithId(R.id. bt_deliver))); 

现在使用android.support.test.espresso.contrib变得更简单了:

1)添加testing依赖项

 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' } 

*排除3个模块,因为很可能你已经有了它

2)然后做类似的事情

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

要么

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

要么

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

尝试下一个方法:

  onView(withRecyclerView(R.id.recyclerView) .atPositionOnView(position, R.id.bt_deliver)) .perform(click()); public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) { return new RecyclerViewMatcher(recyclerViewId); } public class RecyclerViewMatcher { final int mRecyclerViewId; public RecyclerViewMatcher(int recyclerViewId) { this.mRecyclerViewId = recyclerViewId; } public Matcher<View> atPosition(final int position) { return atPositionOnView(position, -1); } public Matcher<View> atPositionOnView(final int position, final int targetViewId) { return new TypeSafeMatcher<View>() { Resources resources = null; View childView; public void describeTo(Description description) { int id = targetViewId == -1 ? mRecyclerViewId : targetViewId; String idDescription = Integer.toString(id); if (this.resources != null) { try { idDescription = this.resources.getResourceName(id); } catch (Resources.NotFoundException var4) { idDescription = String.format("%s (resource name not found)", id); } } description.appendText("with id: " + idDescription); } public boolean matchesSafely(View view) { this.resources = view.getResources(); if (childView == null) { RecyclerView recyclerView = (RecyclerView) view.getRootView().findViewById(mRecyclerViewId); if (recyclerView != null) { childView = recyclerView.findViewHolderForAdapterPosition(position).itemView; } else { return false; } } if (targetViewId == -1) { return view == childView; } else { View targetView = childView.findViewById(targetViewId); return view == targetView; } } }; } } 

首先给你的button独特的contentDescriptions,即“传递button行5”。

<button android:contentDescription=".." />

然后滚动到行:

onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition(5));

然后根据contentDescriptionselect视图。

onView(withContentDescription("delivery button row 5")).perform(click());

内容描述是使用Espresso的onView并使您的应用程序更易于访问的好方法。