13

1)所有正在测试的设备/模拟器都禁用了动画。片状安卓咖啡测试 - 小吃店

2)我有一个@BeforeClass构建我的凭证对象。

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

4)当点击登录按钮时,IntentService会关闭。在这种情况下,服务器(模拟服务器)返回500错误。该信息通过绿色机器人的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] FAILED android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: a string starting with "Server Error: 500"

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

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

其他可能的相关细节:

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

我该如何修复这个测试,使它不是片状,除了膨胀多长时间我的小吃店显示?

回答

12

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

这对我而言有2个负面影响。

首先,它增加了运行测试需要多长时间。每个测试中额外的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秒,它默认为250ms,并且可以调整该值(setWatchInterval)。

+0

谢谢。 Espresso对IdlingResource的依赖正在推动着我的香蕉。没有很好的理由将测试框架代码投入到我的类中,而这正是应该如何正确完成的。 我仍然不能相信Espresso已经没有这样的东西了! –