gpt4 book ai didi

单击按钮时出现 Android Espresso 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:08 24 4
gpt4 key购买 nike

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

现在我只是检查启动画面上是否存在所有元素,然后我尝试单击登录按钮。

当按钮被点击时,由于一个我似乎无法理解为什么会发生的错误,测试失败了。

我的测试代码是

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

}

我得到的输出是:

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.

这是什么意思,这是由我的测试方法引起的还是代码中的错误?该应用似乎在我的设备上运行良好。

PS:我已经按照 espresso 文档的建议禁用了动画

最佳答案

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 默认情况下正在寻找在设备屏幕上完全可见的元素。

您的实际设备屏幕似乎没有显示您的登录 Activity xml 文件的全部内容。

  • 您使用 ScrollView 还是 ListView
  • 您的布局如何?
  • 是否在测试设备上完整显示?

这可能有帮助:

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

我的问题仍然会发生,请您添加到您的问题 xml 或屏幕截图。我会修复它 ;-)


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

在更大屏幕的设备上运行你的测试,检查它是否仍然发生。

如有疑问请随时提问

关于单击按钮时出现 Android Espresso 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34510696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com