Testing background color espresso Android

前端 未结 7 980
灰色年华
灰色年华 2020-12-11 14:50

Is it possible to check if the background color matches a given color with espresso?

Update:

I made a custom matcher, similar to what @Irfa

7条回答
  •  半阙折子戏
    2020-12-11 15:45

    Here is what i use in my tests

    public static Matcher withTextColor(final int color) {
        Checks.checkNotNull(color);
            return new BoundedMatcher(TextView.class) {
                @Override
                public boolean matchesSafely(TextView textView) {
                    return ContextCompat.getColor(getTargetContext(),color)==textView.getCurrentTextColor();
                }
                @Override
                public void describeTo(Description description) {
                    description.appendText("with text color: ");
                }
            };
    }
    

    and call it as

    onView(withId(R.id.price_value)).check(matches(withTextColor(R.color.black)));

提交回复
热议问题