Android Espresso 2 卡在对话框中

标签 android android-espresso

我想用 Espresso 2.2.1 测试登录 Activity 。

当用户输入错误的用户名/密码组合并单击登录按钮时,将出现一个进度对话框并启动一个 http 请求。之后,当服务器回答密码错误时,将出现一个错误对话框。

这是我的测试代码:

@Test
 public void testLoginInvalid() {
    ViewInteraction emailViewInteraction = Espresso.onView(ViewMatchers.withId(R.id.email));
    ViewInteraction passwordViewInteraction = Espresso.onView(ViewMatchers.withId(R.id.password));
    ViewInteraction loginButtonViewInteraction = Espresso.onView(ViewMatchers.withId(R.id.login_button));

    emailViewInteraction.perform(ViewActions.typeText("example_username"));
    passwordViewInteraction.perform(ViewActions.typeText("invalid_password"));
    loginButtonViewInteraction.perform(ViewActions.click());

    Espresso.onView(ViewMatchers.withText(R.string.dialog_exception_title)).inRoot(RootMatchers.isDialog()).check(ViewAssertions.matches(ViewMatchers.isDisplayed())); 
    Espresso.onView(ViewMatchers.withId(android.R.id.button1)).perform(ViewActions.click());

    Intents.assertNoUnverifiedIntents();
}

它工作正常,直到

loginButtonViewInteraction.perform(ViewActions.click());

出现进度对话框,然后是异常对话框。但随后它卡住了。下一行

Espresso.onView(ViewMatchers.withText(R.string.dialog_exception_title)).inRoot(RootMatchers.isDialog()).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

未被调用。我必须手动单击异常对话框中的确定按钮,然后测试才能继续。当然它会失败,因为在手动单击确定后对话框被关闭。

当我不手动点击时,测试在 60 秒后超时:

    android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.uclapp:id/login_button'.
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:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.uclapp.tests.integration.ui.LoginActivityTest.testLoginInvalid(LoginActivityTest.java:113)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 3558 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:477)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:408)
at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:226)
at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:138)
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.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

我进一步调试它,问题似乎是在 UIControllerImpl 第 460 行:

QueueState queueState = queueInterrogator.determineQueueState();

queueState 总是 BARRIER。

我该如何解决这个问题?

最佳答案

明白了。进度对话框在后台运行,直到异常对话框消失后才会隐藏。所以 Espresso 无法执行下一个命令。

关于Android Espresso 2 卡在对话框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33256327/

相关文章:

android-testing - 使用 Spoon 进行 Android 测试分片

java - 此 Android SDK 需要 Android Developer Toolkit -887826。请更新ADT到最新版本

java - DataBinding - boolean 条件未按应有的方式评估

android - 启用使用主机 GPU 时 Espresso 测试卡住

android - 为什么 espressos View 层次结构与 UI automators View 层次结构不匹配

android - isDialog() 与 Espresso 中的对话框 fragment 不匹配

android - 如何使用数据库数据分发 Android 应用程序?

java - 在 Java (Android) 中扩展动态加载的类

android - 根据编译器、Android 的说法,FragmentTransaction 对象的实例化是不可能的

android - 测试列表偏好