Android Espresso - Web Browser

I have a question

I want to check if the web browser starts up after pressing the button using espresso. And the question is, is it even possible to experience such a thing? If so, what ideas can I make?

+8
android android-testing android-espresso
source share
3 answers

Not really. Espresso will allow you to click on the button, and as soon as the browser is launched, the test will end. The alternative you have is that your class incites the Intent browser to mock so you can test the remaining stream (if any).

HAve take a look in this answer: Control at the end of the automation test - Espresso , where I describe how you could achieve this.

+3
source share

Although this is an old question, it's just posting here to help someone else. I had the same situation when I wanted to check if a specific URL was running in a browser or not. I got real help from this link

I worked using this piece 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(); 

Thus, it checks to see if the browser opens with the correct URL and intends () to do the magic here, allowing it to bite the intent. Using this, we can intercept it so that the intent is never sent to the system.

+18
source share

For convenience, I offer a complete 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))) 

XML strings:

 <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) } 
0
source share

All Articles