Android Espresso error on button click

﹥>﹥吖頭↗ 提交于 2019-12-23 07:47:30

问题


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


回答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 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




回答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.



来源:https://stackoverflow.com/questions/34510696/android-espresso-error-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!