Flaky Android Espresso Test - Snackbar

Mar*_*ark 14 android android-support-library android-espresso android-snackbar

1)所有正在测试的设备/仿真器都禁用了动画.

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

3)我有一个在@Before中注册的IntenServiceIdlingResource和一个EventBusIdlingResource.

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

}
Run Code Online (Sandbox Code Playgroud)

这是我通常得到的例外:

SignInExceptionTest> a_userNamePasswordTest [Nexus_6P_API_23(AVD) - 6.0] FAILED android.support.test.espresso.NoMatchingViewException:找不到层次结构中的视图匹配:with text:以"Server Error:500"开头的字符串

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

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

其他可能相关的细节:

  • minSdk:20
  • compile&targetSdk:25
  • buildTools:25.0.2
  • support-library:25.1.1
  • espresso-core:2.2.2
  • gradle插件2.3.0-beta3

我怎么能修复这个测试所以它不是片状,除了膨胀我的零食栏显示多长时间?

Mar*_*ark 15

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

这对我的情况有两个负面影响.

首先,它会增加测试运行所需的时间.每次测试额外的5秒(或5的倍数)可以真正加起来.

其次,由于快餐栏立即显示,并且只显示约3.5秒,几乎在等待IdlingResource的任何时候,在Espresso意识到资源闲置之前,快餐栏将消失,这使得难以测试.

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

并在此处编写代码:https: //github.com/AzimoLabs/ConditionWatcher

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