I have a RecyclerView (R.id.recyclerView) where each row has an image (R.id.row_image) and a TextView. I want to click on the image in the first row.
I\'ve tried to use
Use RecyclerViewActions
onView(withId(R.id.recyclerView))
.perform(actionOnItemAtPosition(0, click()));
Include this in your gradle script:
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
Espresso 2.0 has been released, the changelog includes the following:
New Features
- espresso-contrib
- RecyclerViewActions: handles interactions with RecyclerViews
I haven't yet tested this myself, but Thomas Keller posted this on G+ with a short explanation and a link to a Gist with the necessary view matchers.
Since the new
RecyclerView
API inherits fromViewGroup
and not fromAdapterView
, you cannot use Espresso'sonData()
to test layouts using this component.
Link to Gist.
I'll attach the code, just for completeness sake (note: not mine! All credit goes to Thomas Keller)
ViewMatcher:
public class ViewMatchers {
@SuppressWarnings("unchecked")
public static Matcher<View> withRecyclerView(@IdRes int viewId) {
return allOf(isAssignableFrom(RecyclerView.class), withId(viewId));
}
@SuppressWarnings("unchecked")
public static ViewInteraction onRecyclerItemView(@IdRes int identifyingView, Matcher<View> identifyingMatcher, Matcher<View> childMatcher) {
Matcher<View> itemView = allOf(withParent(withRecyclerView(R.id.start_grid)),
withChild(allOf(withId(identifyingView), identifyingMatcher)));
return Espresso.onView(allOf(isDescendantOfA(itemView), childMatcher));
}
}
And sample usage:
onRecyclerItemView(R.id.item_title, withText("Test"), withId(R.id.item_content))
.matches(check(withText("Test Content")));
Just to add to Gabor's answer (which is the correct and complete answer since Espresso 2.0).
You may encounter problems at the moment when using espresso-contrib
and RecyclerView
s (see android-test-kit ticket).
A workaround is to add this exclusion in the espresso-contrib
dependency Gabor mentioned above:
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'
}
(This is an answer instead of a comment to Gabor's answer because I don't have the right to post comments yet)
You needn't to add "testing-support-lib", neither "espresso:espresso-core". They are transitive added when "espresso:espresso-contrib" is added.
build.grade
dependencies {
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}
Usage:
onView(withId(R.id.recyclerView)).perform(
RecyclerViewActions.actionOnItemAtPosition(0, click()));
I have found two ways:
onView(allOf(withId(R.id.place_description),withText("what"))).perform(click());
`
public Matcher<View> withItemText(final String itemText) {
checkArgument(!TextUtils.isEmpty(itemText),"cannot be null");
return new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View item) {
return allOf(isDescendantOfA(isAssignableFrom(RecyclerView.class)),withText(itemText)).matches(item);
}
@Override
public void describeTo(Description description) {
description.appendText("is descendant of a RecyclerView with text" + itemText);
}
};
}
`
And then, do this:
onView(withItemText("what")).perform(click());
Same answer like above but when some small modification since you have to provide the Viewholder type
With using RecyclerViewActions
fun tapOnRecyclerView(@IdRes resId: Int , position: Int) = onView(withId(resId))
.perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(position, click()));
and to include this libraries in your project , use this
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
Prefer to use the latest espresso version from this link