Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓浓缩咖啡:选中复选框_Android_Android Layout_Junit4_Android Espresso - Fatal编程技术网

Android 安卓浓缩咖啡:选中复选框

Android 安卓浓缩咖啡:选中复选框,android,android-layout,junit4,android-espresso,Android,Android Layout,Junit4,Android Espresso,在这一点上,我对浓缩咖啡是新手,我有一个例外: android.support.test.espresso.AmbiguousViewMatcherException:'id为: android:id/checkbox'匹配层次结构中的多个视图 当尝试检查未选中的框时会发生这种情况:onView(withId(android.R.id.checkbox)).check(匹配项(not(isChecked())) 我的理解是,我有3个复选框,出于某种原因,它们都符合此描述,我的问题是:如何区分复选

在这一点上,我对浓缩咖啡是新手,我有一个例外:

android.support.test.espresso.AmbiguousViewMatcherException:'id为: android:id/checkbox'匹配层次结构中的多个视图

当尝试检查未选中的框时会发生这种情况:
onView(withId(android.R.id.checkbox)).check(匹配项(not(isChecked()))

我的理解是,我有3个复选框,出于某种原因,它们都符合此描述,我的问题是:如何区分复选框

这是我得到的错误日志:

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: android:id/checkbox' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1440, height=2560, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1440, height=2392, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16909225, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=-1, visibility=VISIBLE, width=1440, height=2308, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=84.0, child-count=1}
|
+--->ActionBarOverlayLayout{id=2131492999, res-name=decor_content_parent, visibility=VISIBLE, width=1440, height=2308, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+---->ContentFrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1440, height=2112, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=196.0, child-count=1}
|
+----->ScrollView{id=-1, visibility=VISIBLE, width=1440, height=2112, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+------>LinearLayout{id=-1, visibility=VISIBLE, width=1440, height=1162, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=5}
|
+------->FontedTextView{id=-1, visibility=VISIBLE, width=1334, height=88, 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, root-is-layout-requested=false, has-input-connection=false, x=53.0, y=105.0, text=Paperless Delivery & Data Sharing, input-type=0, ime-target=false, has-links=false}
|
+------->FontedTextView{id=-1, desc=Check/un-check each box to change your preferences then tap save., visibility=VISIBLE, width=1334, height=138, 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, root-is-layout-requested=false, has-input-connection=false, x=53.0, y=263.0, text=Check/un-check each box to change your preferences then tap save., input-type=0, ime-target=false, has-links=false}
|
+------->View{id=-1, visibility=VISIBLE, width=1440, height=1, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=471.0}
|
+------->LinearLayout{id=2131493744, res-name=settings_prefs_consents_layout, visibility=VISIBLE, width=1440, height=689, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=472.0, child-count=3}
|
+-------->LinearLayout{id=2131493745, res-name=meh, desc=Entitlement 1 checked. Description 1. Read more., visibility=VISIBLE, width=1440, height=191, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=32.0, child-count=2}
|
+--------->AppCompatCheckBox{id=16908289, res-name=checkbox, visibility=VISIBLE, width=103, height=102, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=53.0, y=53.0, text=, input-type=0, ime-target=true, has-links=false, is-checked=true} ****MATCHES****
|
+--------->LinearLayout{id=-1, visibility=VISIBLE, width=1196, height=138, 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, root-is-layout-requested=false, has-input-connection=false, x=191.0, y=53.0, child-count=2}
|
+---------->FontedTextView{id=16908308, res-name=text1, visibility=VISIBLE, width=1196, height=69, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Entitlement 1, input-type=0, ime-target=false, has-links=false}
|
+---------->FontedTextView{id=16908309, res-name=text2, visibility=VISIBLE, width=1196, height=69, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=69.0, text=Description 1 Read more., input-type=0, ime-target=false, has-links=false}
|
+-------->LinearLayout{id=2131493745, res-name=meh, desc=Entitlement 2 unchecked. Description 2. Read more., visibility=VISIBLE, width=1440, height=191, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=223.0, child-count=2}
|
+--------->AppCompatCheckBox{id=16908289, res-name=checkbox, visibility=VISIBLE, width=102, height=102, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=53.0, y=53.0, text=, input-type=0, ime-target=false, has-links=false, is-checked=false} ****MATCHES****
|
+--------->LinearLayout{id=-1, visibility=VISIBLE, width=1197, height=138, 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, root-is-layout-requested=false, has-input-connection=false, x=190.0, y=53.0, child-count=2}
|
+---------->FontedTextView{id=16908308, res-name=text1, visibility=VISIBLE, width=1197, height=69, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Entitlement 2, input-type=0, ime-target=false, has-links=false}
|
+---------->FontedTextView{id=16908309, res-name=text2, visibility=VISIBLE, width=1197, height=69, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=69.0, text=Description 2 Read more., input-type=0, ime-target=false, has-links=false}
|
+-------->LinearLayout{id=2131493745, res-name=meh, desc=Entitlement 3 checked. Description 3. Read more., visibility=VISIBLE, width=1440, height=191, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=414.0, child-count=2}
|
+--------->AppCompatCheckBox{id=16908289, res-name=checkbox, visibility=VISIBLE, width=103, height=102, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=53.0, y=53.0, text=, input-type=0, ime-target=false, has-links=false, is-checked=true} ****MATCHES****
|
+--------->LinearLayout{id=-1, visibility=VISIBLE, width=1196, height=138, 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, root-is-layout-requested=false, has-input-connection=false, x=191.0, y=53.0, child-count=2}
|
+---------->FontedTextView{id=16908308, res-name=text1, visibility=VISIBLE, width=1196, height=69, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Entitlement 3, input-type=0, ime-target=false, has-links=false}

我不熟悉浓缩咖啡,但通过搜索,我在他们的维基上发现:

有时,R.id值在多个视图之间共享。发生这种情况时,尝试使用特定的R.id。给您一个模糊的ViewMatcherException(例如)。异常消息为您提供了当前视图层次结构的文本表示形式,您可以搜索并查找与非唯一R.id匹配的视图:

java.lang.RuntimeException:
com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException:
This matcher matches multiple views in the hierarchy: (withId: is <123456789>)
...

+-----SomeView{id=123456789, res-name=plus_one_standard_ann_button, visibility=VISIBLE, width=523, height=48, has-focus=false, has-focusable=true, window-focus=true,
is-focused=false, is-focusable=false, enabled=true, selected=false, is-layout-requested=false, text=, root-is-layout-requested=false, x=0.0, y=625.0, child-count=1}
****MATCHES****
|
+------>OtherView{id=123456789, res-name=plus_one_standard_ann_button, visibility=VISIBLE, width=523, height=48, has-focus=false, has-focusable=true, window-focus=true,
is-focused=false, is-focusable=true, enabled=true, selected=false, is-layout-requested=false, text=Hello!, root-is-layout-requested=false, x=0.0, y=0.0, child-count=1}
****MATCHES****
也可以使用“不反转任何匹配器”:

onView(allOf(withId(R.id.my_view), withText("Hello!")))
onView(allOf(withId(R.id.my_view), not(withText("Unwanted"))))
有关Espresso提供的视图匹配器,请参见ViewMatchers

因此,你应该:

  • 使用上面的过滤器
  • 分配唯一的ID。从长远来看,这更容易,尤其是在代码变得过于复杂而无法更改之前

在布局xml文件中,您可以为每个视图定义三个有用的测试匹配器:

onView(allOf(withId(R.id.my_view), withText("Hello!")))
onView(allOf(withId(R.id.my_view), not(withText("Unwanted"))))
  • android:id
    -它是众所周知的视图id(每个视图都应该有一个its id,这是非常好的设计模式)
  • android:text
    -在一些视图上显示的文本,如
    按钮
    文本视图
  • android:contentDescription
    -就像ID一样,每个视图都可能有其内容描述
使用这些元素中的至少一个会导致您可以使用:

  • onView(withId(R.id.help))…
    -如果您的视图有id
  • onView(withText(R.string.help_desc))…
    -如果视图中有文本
  • onView(带contentdescription(“帮助”))…
    -如果您的视图有内容描述

您的
复选框
视图没有这些
匹配器
。请修复它。

Android Studio中新的Espresso测试记录器通过检查类类型、父级、添加自定义匹配器等,在完全限定查询方面做得很好。它是学习最佳实践的一个很好的培训辅助工具

以下是AS测试记录器机器生成的浓缩咖啡代码示例。将“allOf”和“withParent”相结合对于解决含糊不清的ViewMatcherException非常有效

    ViewInteraction imageButton3 = onView(
        allOf(withContentDescription("Navigate up"),
            withParent(allOf(withId(R.id.action_bar),
                withParent(withId(R.id.action_bar_container)))),
            isDisplayed()));
    imageButton3.perform(click());

对于以下场景,这不是一个解决方案:您有一个线性布局,可以通过编程方式用许多相同的子视图填充该布局。可能您在该视图中没有要匹配的文本。可能文本是随机生成的,不可能硬编码匹配的字符串。