应用程序选择器 - Android Espresso单元在打开外部URL后测试中断

Com*_*ter 3 android unit-testing spoon android-espresso

我有一个带有登录屏幕的Android应用程序,其中还包含一个忘记密码按钮,可以将您带到网站以获得进一步的帮助.我正在使用Spoon和Espresso进行测试,具有以下简单的测试功能:

@Test
public void testForgotPassword()
{
    onView(withId(R.id.login_forgot_password)).perform(click());

    intended(allOf(
            hasAction(Intent.ACTION_VIEW),
            hasData(BuildConfig.FORGOT_PW_URL)));
}
Run Code Online (Sandbox Code Playgroud)

此测试通过正常,并在屏幕上显示"使用浏览器/ chrome对话框完成操作",这是此设备的正确行为.到目前为止都很好.但是,只要该对话框保留在那里,任何后续测试都无法打开应用程序,在长时间停顿并且测试失败后返回异常.

如何更新测试以主动摆脱对话框,或者确保我可以继续进行剩余的单元测试?

完整的例外情况供您参考:

2016-04-21 17:37:04 [STRL.testFailed] failed java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=nl.test.example/.ui.activity.login.LoginActivity_ } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1461252979050 and now the last time the queue went idle was: 1461252979050. If these numbers are the same your activity might be hogging the event queue.
  at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:360)
  at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:219)
  at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:268)
  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:59)
  at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1667)
Run Code Online (Sandbox Code Playgroud)

den*_*nys 8

您必须存根所有外部意图才能继续测试用例.将这段代码放在测试类中:

@Before
public void stubAllExternalIntents() {
    // By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before
    // every test run. In this case all external Intents will be blocked.
    intending(not(isInternal())).respondWith(new ActivityResult(Activity.RESULT_OK, null));
}
Run Code Online (Sandbox Code Playgroud)

更多信息 - IntentsBasicSample.