Creating custom ViewMatcher for espresso

Thiago Lopes Silva
Aug 18, 2017 · 4 min read

Hello my friends. It’s been a while since I’ve been writing here, right ? let’s go further and learn how to create a custom ViewMatcher for espresso on specific scenarios of testing.

Before going further I recommend you to take a look at this post to learn a few about espresso and this one to learn about the Hamcrest library. It’s used a lot by espresso to match views and do tests that will be our focus on this post.

Let´s start

Sometimes when you are doing e2e tests you need to create custom Matcher that attend your business rules. I will teach you how to solve this particular problem using a great feature named ViewMatcher.


Showing the code of dummy custom button that will dispatch an event to enter into his account:

Before going on we need to check some points:

As shown above. It will create a dummy custom button with a property named dummyLoginStatus that will be used to change its content according to status of login. We will use it to find our custom view on screen but first let’s check it out some important class to create a custom matcher.


Basically we need to return a View matcher to use it on onView() or onCheck method of espresso. As being shown bellow I will show you the easiest way to create a custom matcher.


This class will check for you if the received element on screen attend the typed provided class on signature checking if the element found is not null.

It is very useful if you want to find out all the elements of screen by one specific type of class.

The structure of class:

There are two methods that you need to implement:

The first one is used to implement your custom rules of matcher using the found class on screen.

The second one is used to describe what the matcher will try to do. It is only used for verbose logs.


This class was introduced on package that allows you to create a custom matchers attending the minimum requirement of typed provided class and cast it automatically to the object found.

The structure of class:

There are two methods that you need to implement:

The big difference will be noticed in the method matchesSafely. It will return a class already cast for you, so you won´t need to check all the time if the View found is instance of expected result before do your business rule.

Which of them I should use?

Ok. I will try to answer your question doing some question as shown bellow.

Does the matcher need to find any custom property ?

Does the matcher need to find only property of View’s class?

If your first answers were yes, you probably should use the Boundedmather´s class instead of TypeSafeMatcher because it will try to find the specific class and cast the result for you do your custom matcher rule.

Let’s go back to our sample to match a custom property on screen:

Basically our test will try to find if the custom property dummyLoginStatus has the content ERROR after the user has logged into his account.

Some basics rules:

Following some good strategies adopted in ViewMatchers class, we should do some points as shown bellow:

  1. Create a final class that contain all custom matchers
  2. Create static methods returning a Matcher<View>
  3. Create an anonymous class of BoundedMatcher using the View as the first typed class and the second one with the expected class. In our case will be the class LoginButton
  4. You can use it on onView, check etc… on espresso’s methods

Putting all together:

As shown above it will try to match if the custom property named dummyLoginStatus has the expected status. Very easy, right?

Using the custom View Matcher on espresso:

To use it we will create a test that will fail the login into their account checking if the custom property dummyLoginStatus was changed to ERROR.

This dummy property will be used only for our test. Probably you shouldn’t use it for your business rule.

That’s it! I hope helped you understanding how to create custom view matcher on espresso. I ‘d love to know If you have some topics that would like to see here. Keeping in touch I’m writing another great topics for you.

Thanks Felipe Vasconcelos, Olavo Vitor and Jamal for your support

Thanks to Olavo Vitor and Felipe Vasconcelos

Thiago Lopes Silva

Written by

System engineer with focus on mobile development & blockchain research.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade