androidx.test.espresso.AmbiguousViewMatcherException: как вы можете выполнять действия с общим представлением карты внутри макета фрейма в тестах espresso?

#android #android-espresso

#Android #android-espresso

Вопрос:

Я пытаюсь обойти выбор представления карты внутри макета фрейма в тестах Espresso, но я получаю androidx.test.espresso.AmbiguousViewMatcherException , хотя я указал элементы-потомки.

Дело в том, что я получаю следующую ошибку после запроса элемента в иерархии представлений:

 androidx.test.espresso.AmbiguousViewMatcherException: '(with id is <com.example.app:id/cv_display_item> and view has effective visibility=VISIBLE and Child at position 1 in parent Child at position 1 in parent with class name: is "android.widget.FrameLayout" and is displayed on the screen to the user)' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.
  

Вот иерархия представлений фрагмента кода, зарегистрированная при сбое теста:

  >DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, 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, layout-params={(0,0)(fillxfill) sim={state=hidden adjust=resize} ty=BASE_APPLICATION
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
 ->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1794, 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, layout-params=android.widget.FrameLayout$LayoutParams@b4c5ca5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
 -->ViewStub{id=16908692, 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, layout-params=android.widget.LinearLayout$LayoutParams@5be87a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
 -->FrameLayout{id=-1, visibility=VISIBLE, width=1080, height=1731, 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, layout-params=android.widget.LinearLayout$LayoutParams@53582b, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=63.0, child-count=1}
|
 --->FitWindowsLinearLayout{id=2131296377, res-name=action_bar_root, visibility=VISIBLE, width=1080, height=1731, 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, layout-params=android.widget.FrameLayout$LayoutParams@a4b1e21, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
 ---->ViewStubCompat{id=2131296393, 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, layout-params=android.widget.LinearLayout$LayoutParams@3b01246, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
 ---->ContentFrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1080, height=1731, 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, layout-params=android.widget.LinearLayout$LayoutParams@fad4c07, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
 ----->ConstraintLayout{id=2131296543, res-name=container, visibility=VISIBLE, width=1080, height=1731, 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, layout-params=android.widget.FrameLayout$LayoutParams@e5db134, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
 ------>AppBarLayout{id=2131296417, res-name=appBarMain, visibility=VISIBLE, width=1080, height=147, 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, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@c1a7b5d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
 ------->Toolbar{id=2131297265, res-name=toolbar_main, visibility=VISIBLE, width=1080, height=147, 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, layout-params=com.google.android.material.appbar.AppBarLayout$LayoutParams@fcb0cd2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
 -------->AppCompatTextView{id=-1, visibility=VISIBLE, width=261, height=98, 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=androidx.appcompat.widget.Toolbar$LayoutParams@3c8a1a3, tag=null, root-is-layout-requested=false, has-input-connection=false, x=630.0, y=24.0, text=Example MP, input-type=0, ime-target=false, has-links=false}
|
 -------->AppCompatImageButton{id=-1, desc=go-up, visibility=VISIBLE, width=147, height=147, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.appcompat.widget.Toolbar$LayoutParams@4beaca0, tag=null, root-is-layout-requested=false, has-input-connection=false, x=933.0, y=0.0}
|
 -------->ActionMenuView{id=-1, visibility=VISIBLE, width=127, height=147, 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, layout-params=androidx.appcompat.widget.Toolbar$LayoutParams@1887059, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
 --------->FrameLayout{id=2131296691, res-name=filter_layout, visibility=VISIBLE, width=127, height=126, 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, layout-params=androidx.appcompat.widget.ActionMenuView$LayoutParams@2b9b4ff, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=10.0, child-count=2}
|
 ---------->AppCompatImageView{id=-1, visibility=VISIBLE, width=63, height=63, 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@a71f2cc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=32.0, y=31.0}
|
 ---------->MaterialTextView{id=2131297286, res-name=tv_badge, 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, layout-params=android.widget.FrameLayout$LayoutParams@46b915, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false}
|
 ------>FragmentContainerView{id=2131296974, res-name=nav_host_container, visibility=VISIBLE, width=1080, height=1437, 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, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@5bf642a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=147.0, child-count=1}
|
 ------->CoordinatorLayout{id=2131296917, res-name=main_content, visibility=VISIBLE, width=1080, height=1437, 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, layout-params=android.widget.FrameLayout$LayoutParams@57ea21b, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
 -------->AppBarLayout{id=2131296418, res-name=appbar, visibility=VISIBLE, width=1080, height=154, 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, layout-params=androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams@8316fb8, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
 --------->HorizontalScrollView{id=2131296508, res-name=chip_scroll, visibility=VISIBLE, width=1080, height=154, has-focus=false, 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, layout-params=com.google.android.material.appbar.AppBarLayout$LayoutParams@d66d191, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
 ---------->ChipGroup{id=2131296505, res-name=chip_group, visibility=VISIBLE, width=1387, height=126, 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@fc798f6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=14.0, y=14.0, child-count=5}
|
 ----------->Chip{id=2131296502, res-name=chip_all, visibility=VISIBLE, width=216, height=126, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=com.google.android.material.chip.ChipGroup$LayoutParams@cdb44f7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1075.0, y=0.0, text=all, input-type=0, ime-target=false, has-links=false, is-checked=true}
|
 ----------->Chip{id=2131296507, res-name=chip_recommended, visibility=VISIBLE, width=262, height=126, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=com.google.android.material.chip.ChipGroup$LayoutParams@669cf64, tag=null, root-is-layout-requested=false, has-input-connection=false, x=773.0, y=0.0, text=recommended, input-type=0, ime-target=false, has-links=false, is-checked=false}
|
 ----------->Chip{id=2131296504, res-name=chip_domestic, visibility=VISIBLE, width=227, height=126, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=com.google.android.material.chip.ChipGroup$LayoutParams@395f5cd, tag=null, root-is-layout-requested=false, has-input-connection=false, x=506.0, y=0.0, text=dom, input-type=0, ime-target=false, has-links=false, is-checked=false}
|
 ----------->Chip{id=2131296506, res-name=chip_ports, visibility=VISIBLE, width=228, height=126, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=com.google.android.material.chip.ChipGroup$LayoutParams@14c4e82, tag=null, root-is-layout-requested=false, has-input-connection=false, x=238.0, y=0.0, text=por, input-type=0, ime-target=false, has-links=false, is-checked=false}
|
 ----------->Chip{id=2131296503, res-name=chip_cross_borders, visibility=VISIBLE, width=198, height=126, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=com.google.android.material.chip.ChipGroup$LayoutParams@7f53993, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=xb, input-type=0, ime-target=false, has-links=false, is-checked=false}
|
 -------->LockableViewPager{id=2131296543, res-name=container, visibility=VISIBLE, width=1080, height=1283, has-focus=false, 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, layout-params=androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams@5a67dd0, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=154.0, child-count=2}
|
 --------->FrameLayout{id=-1, visibility=VISIBLE, width=1080, height=1283, 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, layout-params=androidx.viewpager.widget.ViewPager$LayoutParams@8afdbef, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=4}
|
 ---------->SwipeRefreshLayout{id=2131297188, res-name=srl_root, visibility=VISIBLE, width=1080, height=1283, 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, layout-params=android.widget.FrameLayout$LayoutParams@1bda6fc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
 ----------->NestedScrollView{id=-1, visibility=VISIBLE, width=1080, height=1283, has-focus=false, 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, layout-params=android.view.ViewGroup$LayoutParams@4912bda, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
 ------------>RecyclerView{id=2131297108, res-name=rv_my_items, visibility=VISIBLE, width=1080, height=2509, has-focus=false, 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, layout-params=android.widget.FrameLayout$LayoutParams@1c8480b, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=5}
|
 ------------->FrameLayout{id=-1, visibility=VISIBLE, width=1080, height=505, has-focus=false, 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, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@ca2cd88, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
 -------------->View{id=-1, visibility=VISIBLE, width=3, height=505, 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@b212321, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1009.0, y=0.0}
|
 -------------->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=505, 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, layout-params=android.widget.FrameLayout$LayoutParams@2563346, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
 --------------->LinearLayout{id=2131296862, res-name=ll_date, visibility=VISIBLE, width=1080, 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, layout-params=android.widget.LinearLayout$LayoutParams@775907, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
 ---------------->View{id=-1, visibility=VISIBLE, width=51, height=51, 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.LinearLayout$LayoutParams@25b3a34, tag=null, root-is-layout-requested=false, has-input-connection=false, x=985.0, y=10.0}
|
 ---------------->MaterialTextView{id=2131297308, res-name=tv_date, visibility=VISIBLE, width=229, 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, layout-params=android.widget.LinearLayout$LayoutParams@4cbd05d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=712.0, y=0.0, text=sample-text, input-type=0, ime-target=false, has-links=false}
|
 --------------->CardView{id=2131296553, res-name=cv_display_item, visibility=VISIBLE, width=1080, height=436, 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, layout-params=android.widget.LinearLayout$LayoutParams@9323dd2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=69.0, child-count=1} ****MATCHES****
  

Вот что я делаю для выполнения действий в целевом представлении карты

 onView(
            allOf(withId(R.id.cv_display_item),
                withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
                childAtPosition(
                    childAtPosition(
                        withClassName(Matchers.`is`("android.widget.FrameLayout")),
                        1),
                    1),
                ViewMatchers.isDisplayed())).perform(ViewActions.click())
  

Проблемное представление карты используется на ViewPager и отображается не менее 3 раз с разными значениями данных

Ответ №1:

Для представлений в a RecyclerView вы захотите использовать RecyclerViewActions их.

Вам нужно будет добавить androidx.test.espresso:espresso-contrib зависимость.

Смотрите пример кода:

     @Test
    public void scrollToItemBelowFold_checkItsText() {
        // First scroll to the position that needs to be matched and click on it.
        onView(ViewMatchers.withId(R.id.recyclerView))
                .perform(RecyclerViewActions.actionOnItemAtPosition(ITEM_BELOW_THE_FOLD, click()));

        // Match the text in an item below the fold and check that it's displayed.
        String itemElementText = getApplicationContext().getResources().getString(
                R.string.item_element_text)   String.valueOf(ITEM_BELOW_THE_FOLD);
        onView(withText(itemElementText)).check(matches(isDisplayed()));
    }