Skip to main content

Writing ViewHolder Matcher with Espresso for Android.

Recently I had a need to adapt my Espresso tests to operate on RecyclerView after migration from ListViews. The current actions that are available for RecyclerView based on item position working fine but I don't like to be dependent on position since data in my tests is created dynamically.

I've googled the ViewHolder matchers and found only this link without any practical examples - RecyclerViewActions.

Then based on already created Matcher<Object> used in onData(...) I've created Matcher<VH> which was not so difficult.

Let's assume each item in RecyclerView adapter has subject, represented by TextView. The below matcher will search for item in RecylerView with unique subject which I provide into matcher. Feel free to use it:

public static Matcher<RecyclerView.ViewHolder> withItemSubjectInViewHolder(final String itemSubject) {
    Checks.checkNotNull(itemSubject);
    return new BoundedMatcher(RecyclerView.ViewHolder, MyListRecyclerViewItemAdapter.MyViewHolder.class) {
        @Override
        public boolean matchesSafely(MyListRecyclerViewItemAdapter.MyViewHolder holder) {
            boolean isMatches = false;

            if (!(holder.subject == null)) {
                isMatches = ((itemSubject.equals(holder.subject.getText().toString()))
                        && (holder.subject.getVisibility() == View.VISIBLE));
            }
            return isMatches;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with item subject: " + itemSubject.toString());
        }
    };
}
And the possible usage is:
onView(withId(R.id.adapter_list)).perform(scrollToHolder(withItemSubjectInViewHolder(itemSubject)));
onView(withId(R.id.adapter_list)).perform(actionOnHolderItem(withItemSubjectInViewHolder(itemSubject), click()));

Comments

Popular posts from this blog

Discovering Espresso for Android: matching and asserting view with text.

After more than a month of using great test tool from Google - Espresso for Android, I'd like to share with you some of my experience. I assume that you've already added espresso jar into your project, spent some time playing with Espresso samples and have basic understanding how this tool works. In this post I'll show how to match particular view with text or assert that it contains (or not) specified Strings. Before we start, you have to take a look at Hamcrest matchers - Hamcrest tutorial and API Reference Documentation , which are used together with Espresso's ViewAssertions and ViewMatchers and included into Espresso standalone library. Pay more attention to Matcher<java.lang.String> matchers. So, here we go. For simplicity following String "XXYYZZ" will be used as a expected text pattern. Espresso ViewMatchers class implements two String matcher methods withText() and withContentDescription() which will match a view which text is equal...

Preparing android emulator for UI test automation.

This post is about setting up android emulator for UI test automation. Properly configured emulator is the basis for reliable tests. Hundreds or thousands of professionally written test cases is great but if they become flaky because of the environment they are running on, their value reduces a lot. I will give you a couple of advices I'm following in my test automation projects. In general we will go through below topics: Managing emulator system animations Controlling soft keyboard appearance Changing emulator system locale Tweaking first and second points will reduce to minimum flakiness in our automated tests which can be caused by emulator. For those who are lazy to read the whole article at the bottom of the post I shared youtube video where I describe the same points with one more additional hint on top :) 1. There are three types of system animation we may control: window animation scale transition animation scale animator duration scale Emulator ...

Discovering Espresso for Android: swiping.

Hi, for today I have some tips about swiping ViewActions in Espresso for Android. As you may know the latest Espresso release contains new swipeLeft and swipeRight ViewActions. They both are really useful when you'd like to swipe between activity fragments, tab layouts or any other UI elements. You can use it as any other view action: onView(withId(R.id.viewId)).perform(swipeRight()); But be aware that doing this you will operate on a view, in our case R.id.viewId, but not on the screen size. That means that to swipe right or left, for example between fragments you have to deal with some parent layout or maybe list view. If you take a look inside Espresso's ViewActions.java class you will see below code for swipeRight() method:   public static ViewAction swipeRight() {     return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.CENTER_LEFT,         GeneralLocation.CENTER_RIGHT, Press.FINGER);   } As you may g...