如何使用 espresso android 测试一定范围内的视图文本

how to test view text within a range using espresso android

我知道我们可以使用 expresso 来检查视图输出: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()));