用“hasBackground”进行浓咖啡测试

时间:2017-11-21 15:53:52

标签: android android-layout background-color android-espresso

如何使用布局背景的颜色进行espresso测试?目前使用hasBackground()

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

但是错误发生了:

  

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

     

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

     

得到:“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}“

我如何比较这个?

1 个答案:

答案 0 :(得分:1)

另一种方法是直接从View:

中检索颜色值
val bar = activityRule.activity.findViewById<View>(R.id.backgroundColor)
val actualColor = (bar.background as ColorDrawable).color
val expectedColor = Color.parseColor("#FF55ff77")
assertEquals(actualColor, expectedColor)
相关问题