Espresso - TextView包含String

Sub*_*bby 39 android android-espresso

很简单,我怎么说如果一个给定TextView包含一个特定的字符串Espresso.

相当于: myStrings.contains("Subby");

476*_*ick 58

您可以使用Hamcrest库.它有一个方法containsString.我相信它在Espresso库中.

您可以在类中静态导入它:

import static org.hamcrest.core.StringContains.containsString;
Run Code Online (Sandbox Code Playgroud)

在TextView上的方法中使用containsString:

textView.check(matches(withText(containsString("Test"))));
Run Code Online (Sandbox Code Playgroud)

  • 这是原始问题的唯一合法答案. (2认同)

Peh*_*laj 17

使用 withText

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

onView(withId(R.id.textView)).check(matches(withText("Subby")));
Run Code Online (Sandbox Code Playgroud)

  • 使用`withText(startsWith("Subby"))`来匹配以所需文本开头的TextView.(同样适用于`withText(endsWith(...))` (17认同)

小智 11

使用withSubstring(substring),虽然一样,withText(containsString(substring))但更加简洁