我现在是Espresso的新手,我得到了这个例外:
android.support.test.espresso.AmbiguousViewMatcherException:'with id: android:id / checkbox'匹配层次结构中的多个视图。
尝试检查未选中的复选框时会发生这种情况:onView(withId(android.R.id.checkbox)).check(matches(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}
答案 0 :(得分:1)
在布局xml文件中,您可以为每个视图定义三个有用的测试匹配器:
android:id
- 您知道的视图ID(每个视图都应该有一个ID,这是非常好的设计模式)android:text
- 在Button
或TextView
android:contentDescription
- 与ID一样,每个视图都可能包含其内容说明。使用这些元素中的至少一个导致您可以使用:
onView(withId(R.id.help))...
- 如果您的观点有ID onView(withText(R.string.help_desc))...
- 如果您的观点有文字onView(withContentDescription("Help"))...
- 如果您的观点有内容说明。您的CheckBox
次观看中没有matchers
次观看。请修理它。
答案 1 :(得分:0)
Android Studio中新的Espresso测试记录器可以通过检查班级类型,父级,添加自定义匹配器等来完全验证您的查询。这是学习最佳实践的一个很好的培训辅助工具。
以下是来自AS测试记录器的机器生成的Espresso代码的示例。结合“allOf”和“withParent”对于解析AmbiguousViewMatcherExceptions非常有用。
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());