2014-02-18 80 views
8

我有一個問題安卓咖啡 - 網頁瀏覽器

我想測試是否在按鈕單擊web瀏覽器後使用espresso啓動beign。 問題是:是否有可能測試這樣的事情? 如果有的話,我會怎麼做?

回答

3

其實沒有。 Espresso將允許您點擊按鈕,一旦瀏覽器啓動,測試將結束。您的選擇是讓您的課程觸發瀏覽器意圖嘲笑,以便您可以測試剩餘流量(如果有)。

看到這個答案:Controlling when an automation test ends - Espresso,在那裏我描述你如何實現這一點。

+0

請參閱下面的Wahib的真實答案。 – Jamey

18

雖然這是一個老問題,但只是張貼在這裏幫助其他人。我有同樣的情況,我想驗證一個特定的url是否在瀏覽器中啓動。我得到真正的幫助,從這個link

我得到它的工作使用這個代碼塊:

Intents.init(); 
    Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(EXPECTED_URL)); 
    intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null)); 
    onView(withId(R.id.someid)).perform(click()); 
    intended(expectedIntent); 
    Intents.release(); 

所以,瀏覽器中打開與正確的網址時,並intending()通過實現意圖存根這裏確實神奇它測試。使用這個,我們可以攔截它,所以意圖不會被髮送到系統。

+2

作品魅力 –

+0

@FalcoWinkler很好的知道。這將是很好,如果你也可以接受它作爲答案:) –

+0

我沒有問這個問題 –

0

爲了方便起見,我建議一個完整的例子:


生產代碼:

register_terms.text = Html.fromHtml(getString(R.string.register_terms, 
     getString(R.string.privacy_policy_url), 
     getString(R.string.register_terms_privacy_policy), 
     getString(R.string.general_terms_and_conditions_url), 
     getString(R.string.register_terms_general_terms_and_conditions))) 

字符串XML:

<string name="register_terms">By registering you accept our &lt;a href=\"%1$s\">%2$s&lt;/a&gt; and the &lt;a href=\"%3$s\">%4$s&lt;/a&gt;.</string> 
<string name="register_terms_privacy_policy">Privacy Policy</string> 
<string name="register_terms_general_terms_and_conditions">General Terms and Conditions</string> 

<string name="privacy_policy_url" translatable="false">https://www.privacypolicy.com</string> 
<string name="general_terms_and_conditions_url" translatable="false">https://www.generraltermsandconditions.com</string> 

測試代碼:

@Before 
fun setUp() { 
    Intents.init() 
} 

@After 
fun tearDown() { 
    Intents.release() 
} 

@Test 
fun when_clickPrivacyLink_then_openPrivacyUrl() { 
    val expected = allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW), IntentMatchers.hasData(string(privacy_policy_url))) 
    Intents.intending(expected).respondWith(Instrumentation.ActivityResult(0, null)) 

    onView(ViewMatchers.withId(R.id.register_terms)) 
      .perform(openLinkWithText(string(register_terms_privacy_policy))) 

    Intents.intended(expected) 
}