How to let Espresso click a specific RecyclerView item?
Asked Answered
O

2

1

I am trying to write an instrumentation test with Espresso for my Android app which uses a RecyclerView. Here is the main layout:

<?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>

... and the item view layout:

<?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>

This means there are a couple of TextViews on the screen which all have the label id. I am trying to click a specific label. I tried to let Espresso test recorder generate the code:

onView(allOf(withId(R.id.grid), isDisplayed()))
    .perform(actionOnItemAtPosition(5, click()));

I rather want to to something like:

// Not working
onView(allOf(withId(R.id.grid), isDisplayed()))
    .perform(actionOnItem(withText("Foobar"), click()));

Readings

I read quite a bit about the topic but still cannot figure out how to test a RecyclerView item based on its content not based on its position index.

Oona answered 1/11, 2016 at 18:12 Comment(0)
P
3

You can implement your custom RecyclerView matcher. Let's assume you have RecyclerView where each element has subject you want to match:

public static Matcher<RecyclerView.ViewHolder> withItemSubject(final String subject) {
    Checks.checkNotNull(subject);
    return new BoundedMatcher<RecyclerView.ViewHolder, MyCustomViewHolder>(
            MyCustomViewHolder.class) {

        @Override
        protected boolean matchesSafely(MyCustomViewHolder viewHolder) {
            TextView subjectTextView = (TextView)viewHolder.itemView.findViewById(R.id.subject_text_view_id);

            return ((subject.equals(subjectTextView.getText().toString())
                    && (subjectTextView.getVisibility() == View.VISIBLE)));
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("item with subject: " + subject);
        }
    };
}

And usage:

onView(withId(R.id.my_recycler_view_id)
    .perform(RecyclerViewActions.actionOnHolderItem(withItemSubject("My subject"), click()));

Basically you can match anything you want. In this example we used subject TextView but it can be any element inside the RecyclerView item.

One more thing to clarify is check for visibility (subjectTextView.getVisibility() == View.VISIBLE). We need to have it because sometimes other views inside RecyclerView can have the same subject but it would be with View.GONE. This way we avoid multiple matches of our custom matcher and target only item that actually displays our subject.

Pulling answered 3/11, 2016 at 13:46 Comment(2)
Anyone know what the function of that appendText is?Nazarite
In this way you customise your error description in case of failure.Pulling
L
1

actionOnItem() matches against the itemView of the ViewHolder. In this case, the TextView is wrapped in the RelativeLayout, so you need to update the Matcher to account for it.

onView(allOf(withId(R.id.grid), isDisplayed()))
    .perform(actionOnItem(withChild(withText("Foobar")), click()));

You can also use hasDescendant() to wrap your matcher is the case of a more complex nesting.

Legitimacy answered 2/11, 2016 at 11:42 Comment(2)
Does hasDescendant() automatically traverse the hierarchy levels regardless of how deep views are nested? Or do I have to use hasDescendant(hasDescendant(...))?Oona
Yup, hasDescendant() will handle the entire view hierarchy from the root that is in onView() part. In case of recyclerview actions, the root is the itemView.Legitimacy

© 2022 - 2024 — McMap. All rights reserved.