我知道我们可以使用expresso来检查 View 输出,如下所示:onView((withId(R.id.text_view_name))).check(matches(withText("text_value_here")));
查看输出是否符合预期,但如果我们想确保它在某个值范围内怎么办?例如,不低于 0。
如何使用 espresso 编写测试代码来确保 UI 值低于、高于或位于特定值范围内?
最佳答案
可以使用自定义匹配器轻松完成:
public class TextViewValueMatcher extends TypeSafeMatcher<View> {
@Override
protected boolean matchesSafely(View item) {
TextView textView = (TextView) item;
String value = textView.getText().toString();
boolean matching = <here your condition on the value>;
return matching;
}
@Override
public void describeTo(Description description) {
}
}
然后使用如下:
Espresso.onView(withId(<some_id>)).check(matches(new TextViewValueMatcher()));
关于android - 如何使用 espresso android 测试一定范围内的 View 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50734475/