Android Espresso error on button click
Solution 1
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
orListView
? - 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
Solution 2
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.
Solution 3
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>
![PeachMode](https://i.stack.imgur.com/x8frn.jpg?s=256&g=1)
PeachMode
Updated on June 07, 2022Comments
-
PeachMode about 2 years
I'm trying to write some UI tests for an Android APP with the espresso framework.
For now I'm just checking if all the elements are present on the splash screen and then I'm trying to click the login button.
When the the button is clicked the test fails due to an error that I can't seem to understand why it's happening.
My test code is
@RunWith(AndroidJUnit4.class) @SmallTest public class WelcomeTest { @Rule public ActivityTestRule<Welcome> mActivityRule = new ActivityTestRule<>( Welcome.class); @Test public void elements_present() { // Check login onView(withId(R.id.wv_login)).check(matches(isDisplayed())); // Check signup onView(withId(R.id.wv_signup)).check(matches(isDisplayed())); // Check video onView(withId(R.id.videoView)).check(matches(isDisplayed())); // Check swipe right onView(withId(R.id.videoView)).perform(swipeRight()); // Check swipe left onView(withId(R.id.videoView)).perform(swipeLeft()); } @Test public void tap_login() { // Tap login button onView(withId(R.id.wv_login)).perform(click()); } }
The output I get is:
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.
What does this mean and is this caused by my test approach or is it a bug on the code? The app seems to work just fine on my devices.
PS: I have disabled the animations as the espresso documentation suggests