片状Android浓缩咖啡测试-小吃店 [英] Flaky Android Espresso Test - Snackbar

查看:104
本文介绍了片状Android浓缩咖啡测试-小吃店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

1) All devices/emulators being tested have animations disabled.

2)我有一个@BeforeClass可以构建我的凭据对象.

2) I have an @BeforeClass which builds my Credentials object.

3)我有一个IntenServiceIdlingResource和一个EventBusIdlingResource,它们已在@Before中注册.

3) I have an IntenServiceIdlingResource and an EventBusIdlingResource, registered in @Before.

4)单击登录"按钮后,IntentService将触发.在这种情况下,服务器(模拟服务器)将返回500错误.该信息通过greenrobot的EventBus从IntentService发布回UI,然后显示Snackbar和错误消息.

4) When the sign in button is clicked, the IntentService fires off. In this case, the server (a mocked server) is returning a 500 error. That information is posted back to the UI from the IntentService, via greenrobot's EventBus, and a Snackbar is shown with the error message.

这是测试的代码:

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

}

这是我通常会遇到的异常:

This is the exception I usually get:

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

SignInExceptionTest > a_userNamePasswordTest[Nexus_6P_API_23(AVD) - 6.0] FAILED android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: a string starting with "Server Error: 500"

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

According to my logging, my idling resources are working. But looking at the timestamps for the logs, the exception is occurring about 5 seconds after the idling resources have gone idle.

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

It seems like there's a delay between when the resources go idle, and when it attempts to look for the view.

其他可能相关的详细信息:

Other possibly pertinent details:

  • minSdk:20
  • 编译和编辑targetSdk:25
  • buildTools:25.0.2
  • support-library:25.1.1
  • 浓缩咖啡核:2.2.2
  • gradle插件2.3.0-beta3

除了膨胀显示我的小吃店多长时间外,我该如何修复该测试以使其不易碎?

How can I fix this test so it's not flaky, besides inflating how long my snackbars are displayed?

推荐答案

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

Espresso enforces a 5 second wait after an IdlingResource becomes active before it is checked again for being idle.

这对我来说有2个负面影响.

This has 2 negative impacts in my case.

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

First, it increases how long it takes the test to run. An extra 5 seconds (or multiple of 5) on every test can really add up.

其次,由于小吃栏会立即显示,并且仅显示约3.5秒,几乎在您等待IdlingResource的任何时间,小吃栏将在Espresso意识到资源处于空闲状态之前就消失了,因此很难测试.

Second, since the snackbar is displayed immediately, and only shows for about 3.5 seconds, pretty much any time you're waiting for an IdlingResource, the snackbar will be gone before Espresso realizes that the resource is idle, making it difficult to test.

ConditionWatcher,在这里描述: https://medium.com/azimolabs/wait-for-it-idlingresource -and-condition-watcher-602055f32356#.9rms52osh

ConditionWatcher, described here: https://medium.com/azimolabs/wait-for-it-idlingresource-and-conditionwatcher-602055f32356#.9rms52osh

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

And code here: https://github.com/AzimoLabs/ConditionWatcher

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

Provides a solution to both these issues. Rather than 5 seconds, it defaults to 250ms, and this value can be adjusted (setWatchInterval).

这篇关于片状Android浓缩咖啡测试-小吃店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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