单击按钮时出现Android Espresso错误 [英] Android Espresso error on button click

查看:81
本文介绍了单击按钮时出现Android Espresso错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用espresso框架为Android APP编写一些UI测试.

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.

我的测试代码是

@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());
  }

}

我得到的输出是:

由于:java.lang.RuntimeException:将不会执行操作,因为目标视图与以下一个或多个约束不匹配: 至少有90%的视图区域显示给用户.

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:我已按照espresso文档的建议禁用了动画

PS: I have disabled the animations as the espresso documentation suggests

推荐答案

由于:java.lang.RuntimeException:将不执行操作 因为目标视图与以下一项或多项不匹配 约束:视图的至少90%区域显示为 用户.

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.

发生任何预期的View都不完全可见时,Espresso默认会在设备屏幕上寻找完全可见的元素.

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

似乎您的实际设备屏幕未显示登录活动xml文件的全部内容.

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

  • 您使用ScrollView还是ListView?
  • 您的布局如何?
  • 它是否已完全显示在经过测试的设备上?
  • Do you use ScrollView or ListView?
  • How your layout looks like?
  • Is it completely displayed on tested device?

这可能会有所帮助:

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

我仍然会出现问题,请添加到您的问题xml或屏幕截图中.我会解决它;-)

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

注意:要检查某些东西是否完全可见(意味着更多,然后 90%)可以使用

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()));

代替:

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

这篇关于单击按钮时出现Android Espresso错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆