2016-04-21 20 views
1

我有一個帶有登錄屏幕的Android應用程序,該應用程序還包含一個忘記密碼按鈕,可讓您訪問網站以獲取進一步幫助。我使用勺子和咖啡測試它,用下面簡單的測試功能:應用選擇器 - Android Espresso在打開外部URL後打破單元測試

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

    intended(allOf(
      hasAction(Intent.ACTION_VIEW), 
      hasData(BuildConfig.FORGOT_PW_URL))); 
} 

該測試通過罰款,並顯示在屏幕上,這是正確的行爲的「使用瀏覽器/鉻對話框完成行動」爲這個設備。迄今爲止都很好。 但是,只要該對話框保留在那裏,任何後續測試都無法打開該應用程序,並在長時間暫停並且未通過測試後返回異常。

如何更新測試以主動擺脫對話框,或者確保我可以繼續進行剩餘的單元測試?

,供大家參考完整例外:

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) 

回答

6

你必須存根所有外部意圖能夠與您的測試用例進行。把這個和平代碼放在你的測試類中:

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

更多這裏 - IntentsBasicSample

+0

該代碼段還打破了涉及應用程序內導航的所有其他測試用例。然而,由於指針我最終修改它使用意圖(hasAction(Intent.ACTION_VIEW)),而不是我的工作。 – Combuster

+0

你是絕對的救星!,在這個例子中很深的jeez haha​​ –

+0

Thanks for this –

相關問題