Select Your Favourite
Category And Start Learning.

Espresso: Asserting all views with the same id are not displayed.

If you use Espresso for Android at some point you may need to check that all views that share the same id are not displayed in the view hierarchy. If your target views don’t belong to RecyclerView or ListView then the usual approach would be to locate each view separately and check its visibility using Espresso isDisplayed() ViewMatcher.

This may be not really efficient when you have numerous similar views and can lead to duplicated code. As a more complicated in implementation but easy in use alternative you may go for this custom ViewAssertion that acts on an ancestor view where all views that share the same id are its descendants:

And the usage will look like this – locate ancestor view which should be a view of ViewGroup type, like Linearlayout or similar, and apply descendantsWithIdInvisible() ViewAssertion:

The sample source code with the usage example locates here.

Subscribe to be notified about new posts or follow me on Twitter.