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

标签 android android-studio range android-espresso android-testing

我知道我们可以使用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/

相关文章:

android - 找出 Android 应用被卸载的原因

android - Gradle - 高 CPU 使用率

java - 如何设置具有 IDE 支持的 Cordova 插件项目?

c# - 遍历数组时 C# 中的索引超出范围异常

python - 尝试将列表中的整数插入日期时间对象

android - 用于后台任务的最佳做法是什么?

android - 如何使用android :layout_width ="wrap_content"?获取以像素为单位的宽度

android - 是否有 “Could not determine artifacts for (library from Jitpack)”的gradle修复程序?

范围构建模式

Android Looper 和 Thread 似乎不起作用