Android Espresso error on button click

PeachMode picture PeachMode · Dec 29, 2015 · Viewed 14.3k times · Source

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

Answer

piotrek1543 picture piotrek1543 · Dec 29, 2015

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