测试背景色ES preSSO的Andr​​oid [英] Testing background color espresso Android

查看:127
本文介绍了测试背景色ES preSSO的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能检查的背景颜色与ES preSSO给定的颜色相匹配?

我做了一个自定义的匹配,类似于@Irfan建议,谢谢!

 公共静态匹配器<对象> backgroundShouldHaveColor(INT expectedColor){
    返回buttondShouldHaveBackgroundColor(equalTo(expectedColor));
}
私有静态匹配器<对象> buttonShouldHaveBackgroundColor(最终匹配器<整数GT; expectedObject){
    最终诠释[] =色新INT [1];
    返回新BoundedMatcher<对象,按钮和GT;(Button.class){
        @覆盖
        公共布尔matchesSafely(最​​终按钮actualObject){            颜色[0] =((ColorDrawable)actualObject.getBackground())的getColor();
            如果(expectedObject.matches(彩色[0])){
                返回true;
            }其他{
                返回false;
            }
        }
        @覆盖
        公共无效describeTo(最终说明描述){
            //应该加以改进!
            description.appendText(颜色不匹配+颜色[0]);
        }
    };
}


解决方案

我不知道这一点,但我们可以获取一些像按钮和文本视图`

元素的颜色

 按钮按钮=(按钮)findViewById(R.id.my_button);
可绘制buttonBackground = button.getBackground();

和你可以尝试这样的

  Col​​orDrawable b_color =(ColorDrawable)button.getBackground();

然后

  INT颜色= b_color.getColor();
如果(colorID == R.color.green){
    日志(色为绿色);
}

希望这将让你开始。

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 `

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 (colorID == R.color.green) {
    log("color is green");
}

Hope this will get you started.

这篇关于测试背景色ES preSSO的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆