【问题标题】:Android how to check content of Image View using EspressoAndroid 如何使用 Espresso 检查图像视图的内容
【发布时间】:2016-01-19 06:16:00
【问题描述】:
我正在使用 Glide 从 URL 将图像加载到图像视图中。
如何对此图像视图执行比较操作以检查是否确实使用 Espresso 加载了正确的 URL?
我在回收站视图中有许多这样的图像,并且正在滚动到它的每个项目,如下所示:
for(int i=0;i<length;i++){
onView(withId(R.id.people_recycler_view)).perform(RecyclerViewActions.scrollToPosition(i));
}
但是在这个循环中,我该如何检查图像视图内容呢?
谢谢!
【问题讨论】:
标签:
android
imageview
android-espresso
【解决方案1】:
一种简单(但不完全)的方法是在应用程序源代码中设置图像的任何位置添加适当的内容描述,然后使用onView(withContentDescription("Cute Picture Of Cat"));
如果还是不行,请在您的测试项目中添加预期的图像,并使用图像比较库将其与您在屏幕元素范围内看到的任何内容进行比较。
eg - 创建和使用如下的自定义匹配器。您还必须编写自己的图像比较方法。
onView(withId(R.id.myElementThatHasImage)).
CustomMatcher.compareImages(test.R.id.myExpectedImageID));
public static Matcher<Object> compareImages(Matcher<View> expectedImageView) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("comparingImages");
}
@Override
protected boolean matchesSafely(View actualImageView) {
return MyCompareLibrary.compare(expectedImageView,(ImageView)actualImageView.getDrawable());
}
};
}