Android Espresso error on button click

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.

I happens when any expected View is not full visible, Espresso by default is looking for elements which are completely visible on device screen.

It seems to be that your actual device screen don't show whole content of your login activity xml file.

  • Do you use ScrollView or ListView?
  • How your layout looks like?
  • Is it completely displayed on tested device?

It might help:

onView(withId(R.id.wv_login))
    .perform(scrollTo(), click());

I problem still would happen, please you add to your question xml or screenshot. I would fix it ;-)


NOTE: To check if something is completely visible (it means more then 90%) you can use

onView(withId(R.id.wv_login)).check(matches(isCompletelyDisplayed()));

instead of:

onView(withId(R.id.wv_login)).check(matches(isDisplayed()));

Run your test on bigger screen device, to check if it still happens.

If you have question please free to ask


The RuntimeException seen here 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. Does, in fact come from a failure of the View to be fully displayed. However, the answer given by @piotrek1543 doesn't really get to a solution, though it is full of really good inforamation.

The issue is caused by a race condition. You are attempting to open the view, and during it's opening you are attempting to click it. If the timing isn't right, then less than 90% of the View will be available for the Espresso framework to click(). You can resolve the issue by disabling animations, as recommended in The Espresso Setup Instructions

  • Navigate to you phone's Developer Options
  • Set the following
    • Window animation scale = 0.0x
    • Transition animation scale = 0.0x
    • Animator duration scale = 0.0x

My own testing indicates that you can get away with just setting the Transition Animation Scale to 0.0x. As you can imagine, this is a perfectly consistent solution to the race condition that you're experiencing.


If button is not placed in scrollview and animations turned off, this error can occur when button is set a height and within a parent layout with a height set to wrap_content.

Fail

<LinearLayout
        ...
        android:layout_width="match_parent"
        **android:layout_height="wrap_content">**

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_attendance"
            app:backgroundTint="@color/surveyColor"
            android:textAllCaps="false"
            android:text="View Attendance"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
            android:layout_width="0dp"
            **android:layout_height="60dp"/>**
</LinearLayout>

Pass : The value of height should rather be passed to the parent layout

    <LinearLayout
        ...
        android:layout_width="match_parent"
        **android:layout_height="60dp">**

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_attendance"
            app:backgroundTint="@color/surveyColor"
            android:textAllCaps="false"
            android:text="View Attendance"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
            android:layout_width="0dp"
            **android:layout_height="60dp"/>**
</LinearLayout>