2014-02-18 75 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) 
}