linkifyui-testingandroid-espresso

Android Espresso and Linkify


I am writing some tests for my Android app based on Espresso. After clicking on a link inside a TextView (created with the Linkify class) I need to assert I am seeing the correct screen.

I tried performing a click on the TextView that contains the link, but the link won't open.

Is there a proper way to test this using Espresso (other than modifying the code to have a separate TextView for the link)?


Solution

  • I found out a simpler way of doing this, there is a method in ViewActions called openLinkWithText, it uses linkTextMatcher as matcher to match link, using this is it very easy to click a textview with multiple links like this :

    Espresso.onView(ViewMatchers.withId(R.id.legal_privacy_tv)).perform(ViewActions.openLinkWithText("Privacy Statement")); 
    

    In my case i have 1 single text view which is linkify with 2 links, privacy statement and legal statement, using above method i am able to click on them individually without using bound xy or other methods suggested above.