Testing background color espresso Android

2019-06-15 01:24发布

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]);
        }
    };
}

6条回答
混吃等死
2楼-- · 2019-06-15 01:40

I am not sure about that but we can retrieve the color of some of elements like button and text views `

Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();

and you can try like this

ColorDrawable b_color = (ColorDrawable) button.getBackground();

and then

int color = b_color.getColor();
if (color == R.color.green) {
    log("color is green");
}

Hope this will get you started.

查看更多
走好不送
3楼-- · 2019-06-15 01:47
private fun hasBackgroundColor(colorRes: Int): Matcher<View> {
    Checks.checkNotNull(colorRes)

    return object : TypeSafeMatcher<View>() {
        override fun describeTo(description: Description?) {
            description?.appendText("background color: $colorRes")
        }

        override fun matchesSafely(item: View?): Boolean {

            val context = item?.context
            val textViewColor = (item?.background as ColorDrawable).color
            val expectedColor: Int?

            if (Build.VERSION.SDK_INT <= 22) {
                expectedColor = context?.resources?.getColor(colorRes)
            } else {
                expectedColor = context?.getColor(colorRes)
            }

            return textViewColor == expectedColor
        }
    }
}
查看更多
地球回转人心会变
4楼-- · 2019-06-15 01:47

Here is what i use in my tests

public static Matcher<View> withTextColor(final int color) {
    Checks.checkNotNull(color);
        return new BoundedMatcher<View, TextView>(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)));

查看更多
We Are One
5楼-- · 2019-06-15 01:51

Another approach to check TextView text's color could be via hasTextColor(int color) as it comes directly from Espresso.

import static android.support.test.espresso.matcher.ViewMatchers.hasTextColor;

onView(withId(R.id.anyTextView)).check(matches(hasTextColor(R.color.red)));

Be aware that this method is currently in Beta for Espresso 3.0.1

查看更多
贼婆χ
6楼-- · 2019-06-15 01:55

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.

 private fun withBackgroundColor(@ColorInt color: Int): Matcher<View> {
        Checks.checkNotNull(color)
        return object : BoundedMatcher<View, TextView>(TextView::class.java) {
            override fun describeTo(description: Description?) {
                description?.appendText("TextView background color to be $color")
            }

            override fun matchesSafely(item: TextView?): Boolean {
                val backgroundColor = item?.background as ColorDrawable
                val colorDrawable = ColorDrawable(ContextCompat.getColor(item.context, color))
                return colorDrawable.color == backgroundColor.color
            }

        }
    }

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.

查看更多
乱世女痞
7楼-- · 2019-06-15 01:59

In my tests I have the following matcher for testing EditText color:

public static Matcher<View> withTextColor(final int color) {
    Checks.checkNotNull(color);
    return new BoundedMatcher<View, EditText>(EditText.class) {
        @Override
        public boolean matchesSafely(EditText warning) {
            return color == warning.getCurrentTextColor();
        }
        @Override
        public void describeTo(Description description) {
            description.appendText("with text color: ");
        }
    };
}

And usage is :

onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED)));
查看更多
登录 后发表回答