Espresso - TextView contains String

AndroidAndroid Espresso

Android Problem Overview


Quite simply, how do I say if a given TextView contains a specific string in Espresso.

The equivalent of: myStrings.contains("Subby");

Android Solutions


Solution 1 - Android

You can use the Hamcrest library. It has a method containsString. I believe it is in the Espresso library.

You can static import it in your class:

import static org.hamcrest.core.StringContains.containsString;

Use containsString in your method on a TextView:

textView.check(matches(withText(containsString("Test"))));

Solution 2 - Android

Use withText

onView(...).check(matches(withText("Subby")));

onView(withId(R.id.textView)).check(matches(withText("Subby")));

Solution 3 - Android

Use withSubstring(substring), it is same as withText(containsString(substring)) but more concise

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSubbyView Question on Stackoverflow
Solution 1 - Android476rickView Answer on Stackoverflow
Solution 2 - AndroidPEHLAJView Answer on Stackoverflow
Solution 3 - Androidluxin.chenView Answer on Stackoverflow