使用"hasBackground"进行浓缩咖啡测试 [英] Espresso test with “hasBackground”

查看:136
本文介绍了使用"hasBackground"进行浓缩咖啡测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用布局背景的颜色进行浓咖啡测试?当前使用 hasBackground():

How can I make a espresso test with the color of the layout background? Currently use hasBackground():

onView(withId(R.id.backgroundColor)).check(matches(hasBackground(Color.parseColor("#FF55ff77"))));

但是会发生错误:

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:'具有可绘制ID为-11141257的背景'与所选视图不匹配.

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has background with drawable ID: -11141257' doesn't match the selected view.

预期:具有可绘制ID:-11141257的背景

Expected: has background with drawable ID: -11141257

Got:"LinearLayout {id = 2130968576,res-name = backgroundColor,可见性= VISIBLE,宽度= 996,高度= 1088,has-focus = false,has-focusable = false,has-window-focus = true, is-clickable = false,is-enabled = true,is-focused = false,is-focusable = false,is-layout-requested = false,is-selected = false,layout-params = android.widget.LinearLayout $ LayoutParams @ e55f6e7,tag = null,root-is-layout-requested = false,has-input-connection = false,x = 42.0,y = 601.0,child-count = 2}"

Got: "LinearLayout{id=2130968576, res-name=backgroundColor, visibility=VISIBLE, width=996, height=1088, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@e55f6e7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=601.0, child-count=2}"

我该如何比较?

推荐答案

我正在借助

您可以为匹配器提供类似的内容:

And you can provide your matcher with something like:

public class CustomTestMatchers {

    public static Matcher<View> withBackgroundColour(@ColorRes int expectedColor) {
        return new BackgroundColourMatcher(expectedColor);
    }
}

最后在您的测试中:

onView(withId(R.id.my_view)).check(matches(withBackgroundColour(R.color.color_to_check)))

您可以轻松地修改BackgroundColourMatcher类,使其直接使用颜色而不是资源.

You can easily modify the BackgroundColourMatcher class to make it use directly a colour and not a resource.

希望有帮助!

这篇关于使用"hasBackground"进行浓缩咖啡测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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