gpt4 book ai didi

安卓 Espresso : checking for a checked checkbox

转载 作者:行者123 更新时间:2023-11-29 20:26:13 31 4
gpt4 key购买 nike

我现在是 Espresso 的新手,我遇到了这个异常:

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: android:id/checkbox' matches multiple views in the hierarchy.

当尝试检查未选中的框时会发生这种情况: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}

最佳答案

我对 Espresso 不熟悉,但是搜索了一下,我在他们的 wiki 上找到了 this:

Sometimes, R.id values are shared between multiple views. When this happens an attempt to use a particular R.id. gives you an AmbiguousViewMatcherException (for example). The exception message provides you with a text representation of the current view hierarchy, which you can search for and find the views that match the non-unique 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****

Looking through the various attributes of the views, you may find uniquely identifiable properties (in the example above, one of the views has the text "Hello!"). You can use this to narrow down your search by using combination matchers:

onView(allOf(withId(R.id.my_view), withText("Hello!")))

You can also use not to reverse any of the matchers:

onView(allOf(withId(R.id.my_view), not(withText("Unwanted"))))

See ViewMatchers for the view matchers provided by Espresso.

因此,您应该:

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

关于安卓 Espresso : checking for a checked checkbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32511514/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com