/ Test Automation

Android Espresso test fake friends: display and click effective visibility

After a while of doing some Espresso testing in Android, you will face some issues and wonder: why we ended up struggling for task that should be simple, as click an element after checking it was displayed?

We will break this problem down to see what went wrong with something that shouldn't be rocket science.

Problem : Ask if element is displayed and then click it

Let's assume we are trying to click on an element we must swipe until it's displayed and then, we want to click on this element. Let's say, we want to click on Matraca icon, that is around 50 percent visible on the right.


Turns out that the element is now displayed, but we will have an error like the following:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user.

Root Cause of this problem

Click action was defined several year ago to click an element, only if this element is displayed to the user, at least 90 percent. Nevertheless, when we use Espresso isDisplayed() function, it will work regardless the effective visibility of the element.

How to deal with this?

Well, there are some options to deal with this problem, according to the context. Let's see which one is suitable for us.

Scroll to element

If you are lucky enough and you are on a ScrollView, then you can solve this by using

    .perform(scrollTo(), click())

Actually scrollTo() was considered to make an element, at least 90 percent visible. Someone was conscious about this problem.

Use isCompletelyDisplayed

If you are not on a ScrollView, this could be a great solution.

If a validation is made using isCompletelyDisplayed() instad of isDisplayed(), validation will check that element is, at least 90 percent visible. Again, someone realized this could be a problem.

Use isDisplayingAtLeast in Matcher

If you already have a lot of logic with isDisplayed() and you are not on a ScrollView, an option is to define a Matcher for the element considering the percentage of the area displayed to the user.

val myElement: Matcher<View> = allOf(ViewMatchers.withId(R.id.my_element), ViewMatchers.isDisplayingAtLeast(90))

Now, this will force isDisplayed() to check for an element at least 90 percent visible.


Something this simple could take some of your time solving it. Please be aware of the possibilities and choose the one that fits to your needs. Sometimes you have friend. But sometimes you have fake friends as isDisplayed() and click().