Espresso hasBackground()匹配项不匹配

问题描述 投票:1回答:1

我有一个RecyclerView和几个CardView。我将背景设置为卡片的RelativeLayout,如下所示:

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

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

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

我得到的是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}"

[当我尝试检查是否有这样背景的any视图时>>

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

我有一个带有几个CardView的RecyclerView。我使用以下代码将背景设置为卡片的RelativeLayout:layout.setBackground(ContextCompat.getDrawable(mContext,R.drawable.item_background_grey))...

android android-espresso android-cardview android-relativelayout
1个回答
0
投票

尝试一下:

© www.soinside.com 2019 - 2024. All rights reserved.