0

I'm writing automated tests for my company's app. The tests use a combination of Espresso and Robotium. Several of those tests are supposed to check whether certain ReactImageView elements are displaying a specific image.

I first tried using hasBackground() as follows:

onView(...).check(matches(hasBackground(R.drawable...)));

However, this returns a NoMatchingViewException.

I then tried Daniele Bottillo's matcher as described here: https://medium.com/@dbottillo/android-ui-test-espresso-matcher-for-imageview-1a28c832626f

onView(allOf(..., new DrawableMatcher(R.drawable....), isDisplayed())).check(matches(isDisplayed()));

But this didn't work either:

java.lang.ClassCastException: com.facebook.drawee.generic.RootDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

Lastly, I tried Frankie Sardo's solution: Android Espresso match BitmapDrawables with different tint

onView(allOf(..., withImageDrawable(R.drawable...), isDisplayed())).check(matches(isDisplayed()));

However, this again throws a NoMatchingViewException. I'm running out of ideas. Is it even possible to check what resource is attached to an image view?

Danny
  • 705
  • 1
  • 7
  • 23
  • "Is it even possible to check what resource is attached to an image view?" -- not really, since you can populate an `ImageView` from things other than a resource. In your case, you appear to be using an intermediate library (from Facebook) that is wrapping or otherwise manipulating your resource-based image. – CommonsWare Feb 21 '20 at 23:00
  • Yeah, we're using React Native. I know it's possible to store the resource information using tags, but it's generally not considered a best practice to modify production code for QA purposes. – Danny Feb 21 '20 at 23:02

1 Answers1

0

The easiest way would be to use Barista library and its assertHasDrawable(R.id.<imageview_id>, R.drawable.<drawable_id>) assertion function.

lomza
  • 9,412
  • 15
  • 70
  • 85