Espresso hasBackground()匹配器不匹配

时间:2019-10-01 09:11:07

标签: android android-espresso android-cardview android-relativelayout

我有一个RecyclerView和几个CardView。我使用以下代码将RelativeLayout张卡片的背景设置为:

layout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.item_background_grey));

具有不同可绘制对象的卡片看起来像
white_and_grey_cards

我想通过浓缩咖啡测试检查可绘制对象

我得到ViewInteraction这样的话:

ViewInteraction relativeLayout2 = onView(
                allOf(withId(R.id.last_interaction_entry_layout),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.tab_last_interaction_recyclerview),
                                        1),
                                0),
                        isDisplayed()));

当我检查里面的文字时,它可以正常工作。但是当我检查背景时
relativeLayout2.check(matches(hasBackground(R.drawable.item_background_grey)));

它给我以下错误:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has background with drawable ID: 2131165305' doesn't match the selected view.
Expected: has background with drawable ID: 2131165305
Got: "RelativeLayout{id=2131230886, res-name=last_interaction_entry_layout, visibility=VISIBLE, width=736, height=113, 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.FrameLayout$LayoutParams@2242239, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}"

当我尝试检查是否有具有这样背景的任何视图

onView(allOf(hasBackground(R.drawable.item_background)))
      .check(matches(isDisplayed()));

我得到NoMatchingViewException

还尝试像建议的hereherethere一样直接检查颜色,得到了

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.ColorDrawable.getColor()' on a null object reference

0 个答案:

没有答案