成功点击并阻止 60 秒后,Espresso 测试失败

Posted

技术标签:

【中文标题】成功点击并阻止 60 秒后,Espresso 测试失败【英文标题】:Espresso test fails after successful click and blocking for 60 seconds 【发布时间】:2015-01-21 18:57:08 【问题描述】:

我尝试过使用 Espresso 测试框架并编写了一个简单的测试。

没什么特别的,就是这段代码:

onView(withId(R.id.login_button_stub)).perform(click());

测试在 Nexus 5 设备和搭载 android 5.0 的 Genymotion 上运行良好,但在搭载 Android 4.x 的 Android Emulator 4.x、5.0、Genymotion 2.3、4.x 和 LG G2 上失败。

在所有这些设备/模拟器/模拟器上,单击按钮并执行其操作(在我的情况下转到另一个屏幕)。问题是它在某些设备上阻止perform。这在 60 秒后以一个很好的异常结束:

android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.vielengames.mock:id/login_button_stub'.
    ...
    Caused by: android.support.test.espresso.AppNotIdleException: Looped for 3544 iterations over 60 SECONDS. The following Idle Conditions failed .
    ...

请注意,没有关于哪些条件失败的信息。查看 Espresso 源代码,应该有一个逗号分隔的问题列表。

这里是完整代码:TestCase.java。

和完整的堆栈跟踪:

I/TestRunner﹕ android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.vielengames.mock:id/login_button_stub'.
        at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
        at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
        at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
        at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:185)
        at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
        at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
        at com.vielengames.ui.NotLoggedOnTestCase.testLoggedOnAfterLoginClick(NotLoggedOnTestCase.java:19)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
        at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
        at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
        at junit.framework.TestCase.runBare(TestCase.java:134)
        at junit.framework.TestResult$1.protect(TestResult.java:115)
        at junit.framework.TestResult.runProtected(TestResult.java:133)
        at android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
        at junit.framework.TestResult.run(TestResult.java:118)
        at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
        at junit.framework.TestCase.run(TestCase.java:124)
        at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
        at junit.framework.TestSuite.runTest(TestSuite.java:243)
        at junit.framework.TestSuite.run(TestSuite.java:238)
        at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
        at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
        at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:24)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
        at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
 Caused by: android.support.test.espresso.AppNotIdleException: Looped for 3544 iterations over 60 SECONDS. The following Idle Conditions failed .
        at android.support.test.espresso.IdlingPolicy.handleTimeout(IdlingPolicy.java:61)
        at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:471)
        at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:402)
        at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:226)
        at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:135)
        at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:118)
        at android.support.test.espresso.action.Tap.sendSingleTap(Tap.java:135)
        at android.support.test.espresso.action.Tap.access$100(Tap.java:35)
        at android.support.test.espresso.action.Tap$1.sendTap(Tap.java:40)
        at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:98)
        at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction

【问题讨论】:

【参考方案1】:

这是我的应用程序代码中的一个错误,SwipeRefreshLayout 会无限期地为自己设置动画。由于a bug in this component,刷新状态甚至没有显示。

【讨论】:

非常感谢!这让我发疯了;我在以前的片段中运行了一个旋转动画,该片段从未被清除,因此该应用程序没有空闲并且 Espresso 测试会挂起。实际上,我现在意识到这是一个很棒的功能,因为它迫使开发人员整理动画和背景动作。 非常感谢!!!在我的情况下,AppBarLayout 的 OffsetChangeListener 会不断调用它使用动画自定义 AppBar 我遇到了类似的问题,但在我的情况下,它的按钮附有动画进度条,任何线索 我的情况类似;由 CountDownTimer 引起。

以上是关于成功点击并阻止 60 秒后,Espresso 测试失败的主要内容,如果未能解决你的问题,请参考以下文章

Espresso 测试被后台线程阻塞。应用程序未空闲异常:“AppNotIdleException”。

Android 自动化测试 Espresso篇:异步代码测试

Espresso 在云中的设备上运行时抛出“在视图中执行“单击”时出错”,但相同的测试在本地成功运行

将 AWS Lambda 连接到 Redshift - 60 秒后超时

回到地图意图espresso - UI测试的活动

Espresso 测试无法处理 Pickers