How to catch a View with Tag by Espresso in Android?

After setting the tag in your view somewhere in the app, for those confused about the syntax in Kotlin:

withTagValue(`is`(EXPECTED_TAG))

The complete syntax to assert a tag on a specific view:

onView(
   allOf(
       withId(R.id.some_id),
       withTagValue(`is`(EXPECTED_TAG))
   )
)

Simple :)


Since withTagValue needs an instance of org.hamcrest.Matcher as an argument, we can create a simple one using the Matcher.is method to find views with a certain tag in your expresso test:

String tagValue = "lorem impsum";
ViewInteraction viewWithTagVI = onView(withTagValue(is((Object) tagValue)));