14

I have a question

I want to test whether after button click the web browser is beign launched using espresso. And the question is: is it even possible to test such thing? If so any ideas how would I do that?

user3274539
  • 687
  • 2
  • 8
  • 18

3 Answers3

36

Although it's an old question but just posting here to help anyone else. I had the same situation where i wanted to verify whether a particular url was launched in browser or not. I got real help from this link

I got it working using this chunk of code:

    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();

So, it tests when browser is opened with correct url and intending() does the magic here by enabling intent stubbing. Using this, we can intercept it so the intent is never sent to the system.

Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41
  • @FalcoWinkler good to know that. It would be nice if you can also accept it as an answer :) – Wahib Ul Haq Feb 14 '16 at 17:48
  • I did not ask the question – Falco Winkler Feb 14 '16 at 20:24
  • Oh, Apologies. I was not paying attention at that time. – Wahib Ul Haq Feb 15 '16 at 08:31
  • @WahibUlHaq why do you need to `init()` and `release()`? – Alan Jan 10 '17 at 21:41
  • 1
    @Alan In such test use case, it's imp to start by making a call to init() from Intents. This will start recording the fired Intents, and is required before each test specially when you want to verify Intent activity. For each time you call init(), you have to call release() at the end of the test in order to clear the Intents state and stop recording intents. I hope it was helpful. – Wahib Ul Haq Jan 11 '17 at 14:17
  • For supporting ***Intents.init();*** and the rest of the objects - apply this gradle directive: `androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'` – Gene Bo Feb 02 '17 at 20:12
  • Thanks, it works. By the way, don't forget to perform Click to open browser. – azwar_akbar Jul 21 '17 at 06:56
  • 2
    now it's `androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'` https://developer.android.com/training/testing/espresso/intents – Mr Heelis Apr 24 '20 at 15:45
6

For convenience I suggest a full example:


Production code:

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)))

Strings 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>

Test code:

@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)
}
JoachimR
  • 5,150
  • 7
  • 45
  • 50
4

Actually no. Espresso will allow you to click in the button and once the browser is fired up, the test will end. The alternative you have is having your class that fires the browser Intent mocked, so that you can test the remaining flow (if any).

HAve a look in this answer: Controlling when an automation test ends - Espresso, where I describe how you could achieve that.

Community
  • 1
  • 1
Bolhoso
  • 895
  • 1
  • 7
  • 14