Flaky Android Espresso 测试 - Snackbar

2024-05-01

1) 所有正在测试的设备/模拟器都禁用了动画。

2)我有一个 @BeforeClass 来构建我的 Credentials 对象。

3)我有一个IntenServiceIdlingResource和一个EventBusIdlingResource,在@Before中注册。

4) 当点击登录按钮时,IntentService 就会被触发。在这种情况下,服务器(模拟服务器)返回 500 错误。该信息通过 greenrobot 的 EventBus 从 IntentService 发回 UI,并显示带有错误消息的 Snackbar。

这是测试的代码:

@Test
public void a_userNamePasswordTest() throws Exception {
    // email input
    ViewInteraction userNameView = onView(withId(R.id.email));

    // verify it's on screen and enabled
    userNameView.check(matches(isDisplayed())).check(matches(isEnabled()));

    // set the username
    userNameView.perform(scrollTo(), replaceText(credentials.username), closeSoftKeyboard());

    // password input
    ViewInteraction passwordView = onView(withId(R.id.password));

    // verify it's on screen and enabled
    passwordView.check(matches(isDisplayed())).check(matches(isEnabled()));

    // set the password.
    passwordView.perform(scrollTo(), replaceText(credentials.password), closeSoftKeyboard());

    // sign in button
    ViewInteraction signInButton = onView(withId(R.id.email_sign_in_button));

    // verify the button
    signInButton.check(matches(allOf(
            isDisplayed(), isEnabled(), withText("Sign In"), withContentDescription("Sign In")
    )));

    // clickity click the button
    signInButton.perform(scrollTo(), click());

    // verify the snackbar text
    onView(withText(startsWith("Server Error: 500"))).check(matches(isDisplayed()));

}

这是我通常遇到的异常:

SignInExceptionTest > a_userNamePasswordTest[Nexus_6P_API_23(AVD) - 6.0]失败 android.support.test.espresso.NoMatchingViewException:层次结构中没有找到匹配的视图:文本:以以下开头的字符串 “服务器错误:500”

根据我的记录,我的闲置资源正在运行。但查看日志的时间戳,异常发生在空闲资源空闲后大约 5 秒。

资源空闲和尝试查找视图之间似乎存在延迟。

其他可能相关的细节:

  • 最小SDK:20
  • 编译和目标 SDK:25
  • 构建工具:25.0.2
  • 支持库:25.1.1
  • 浓缩咖啡核心:2.2.2
  • gradle插件2.3.0-beta3

除了增加小吃栏的显示时间之外,我该如何修复此测试,使其不不稳定?


Espresso 在 IdlingResource 变为活动状态后强制等待 5 秒,然后再次检查其是否空闲。

就我而言,这有两个负面影响。

首先,它增加了测试运行的时间。每次测试多花 5 秒(或 5 秒的倍数)确实可以加起来。

其次,由于小吃栏会立即显示,并且仅显示大约 3.5 秒,因此几乎在您等待 IdlingResource 的任何时候,小吃栏都会在 Espresso 意识到资源空闲之前消失,这使得测试变得困难。

ConditionWatcher,描述如下:https://medium.com/azimolabs/wait-for-it-idlingresource-and-conditionwatcher-602055f32356#.9rms52osh https://medium.com/azimolabs/wait-for-it-idlingresource-and-conditionwatcher-602055f32356#.9rms52osh

代码在这里:https://github.com/AzimoLabs/ConditionWatcher https://github.com/AzimoLabs/ConditionWatcher

为这两个问题提供了解决方案。它不是 5 秒,而是默认为 250ms,并且可以调整该值(setWatchInterval)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flaky Android Espresso 测试 - Snackbar 的相关文章

随机推荐