Is it possible to check if the background color matches a given color with espresso?
I made a custom matcher, similar to what @Irfan suggested, thanks!
public static Matcher<Object> backgroundShouldHaveColor(int expectedColor) {
return buttondShouldHaveBackgroundColor(equalTo(expectedColor));
}
private static Matcher<Object> buttonShouldHaveBackgroundColor(final Matcher<Integer> expectedObject) {
final int[] color = new int[1];
return new BoundedMatcher<Object, Button>( Button.class) {
@Override
public boolean matchesSafely(final Button actualObject) {
color[0] =((ColorDrawable) actualObject.getBackground()).getColor();
if( expectedObject.matches(color[0])) {
return true;
} else {
return false;
}
}
@Override
public void describeTo(final Description description) {
// Should be improved!
description.appendText("Color did not match "+color[0]);
}
};
}
I am not sure about that but we can retrieve the color of some of elements like button and text views `
and you can try like this
and then
Hope this will get you started.
Here is what i use in my tests
and call it as
onView(withId(R.id.price_value)).check(matches(withTextColor(R.color.black)));
Another approach to check TextView text's color could be via
hasTextColor(int color)
as it comes directly from Espresso.Be aware that this method is currently in Beta for Espresso 3.0.1
It is possible to to test on color. I created a method to test on
TextView
background color as below. Also notice that am passing color resource.I create
ColorDrawable
object from color resource and get color from the object.Comparing color from resource without the
ColorDrawable
does not pass. Hope this helps you.In my tests I have the following matcher for testing
EditText
color:And usage is :