【问题标题】:Flaky Android Espresso Test - SnackbarFlaky Android Espresso 测试 - Snackbar
【发布时间】:2017-06-21 06:47:39
【问题描述】:

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 秒后。

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

其他可能相关的细节:

  • minSdk: 20
  • 编译 & targetSdk: 25
  • 构建工具:25.0.2
  • 支持库:25.1.1
  • 浓缩咖啡芯:2.2.2
  • gradle 插件 2.3.0-beta3

除了夸大我的小吃店的显示时间之外,我如何修复此测试以使其不至于不稳定?

【问题讨论】:

    标签: android android-support-library android-espresso android-snackbar


    【解决方案1】:

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

    在我的情况下,这有 2 个负面影响。

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

    其次,由于snackbar 会立即显示,并且只显示大约3.5 秒,几乎在您等待 IdlingResource 的任何时候,snackbar 都会在 Espresso 意识到资源空闲之前消失,因此很难测试。

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

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

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

    【讨论】:

    • 谢谢。 Espresso 对 IdlingResource 的依赖让我发疯了。没有充分的理由将测试框架代码洒在我的类中,这就是应该正确完成的方式。我仍然不敢相信 Espresso 还没有这样的东西!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多