77

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

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

476rick
  • 2,764
  • 4
  • 29
  • 49
Subby
  • 5,370
  • 15
  • 70
  • 125

3 Answers3

121

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"))));
476rick
  • 2,764
  • 4
  • 29
  • 49
33

Use withText

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

onView(withId(R.id.textView)).check(matches(withText("Subby")));
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • 22
    Use `withText(startsWith("Subby"))` to match a TextView which starts with your desired text. (Same goes for `withText(endsWith(...))` – chrjs Jul 25 '17 at 12:27
26

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

luxin.chen
  • 381
  • 3
  • 7