Android Espresso - 嵌套父母的组合视图匹配器

Posted

技术标签:

【中文标题】Android Espresso - 嵌套父母的组合视图匹配器【英文标题】:Android Espresso - combined view matchers for nested parents 【发布时间】:2017-07-26 06:11:46 【问题描述】:

我想从以下视图层次结构中找到 id 为 'buttonActionNo' 的按钮。

我尝试了以下代码,但它不起作用。给出“层次结构中未找到匹配的视图:”错误。

**ViewInteraction appCompatImageButton3 = onView(  
            allOf(withId(R.id.buttonActionNo),   
                        allOf( withhParent(withId(R.id.actionButtonPanel)),

                        allOf( withParent(withId(R.id.outerContainer)),

                        allOf( withParent(withId(R.id.questioContainer)),

                        withParent(withId(R.id.redFlagQuestion1))))),

                        isDisplayed()));

        appCompatImageButton3.perform(click());**

有没有人试图抓住嵌套父母级别很少的视图?

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <!-- redFlagQuestion1  -->
        <FrameLayout android:id="@+id/questionOneContainer"
            android:layout_
            android:layout_
            android:background="@color/white">

            <include
                android:id="@+id/redFlagQuestion1"
                layout="@layout/row_base" />

        </FrameLayout>

        <!-- redFlagQuestion 2 -->
        <FrameLayout
            android:layout_
            android:layout_
            android:background="@color/white">

            <include
                android:id="@+id/fillerLayout2"
                layout="@layout/fill_space" />

            <include
                android:id="@+id/redFlagQuestion2"
                layout="@layout/row_base" />


        </FrameLayout>

        <!-- redFlagQuestion 3 -->
        <FrameLayout
            android:layout_
            android:layout_
            android:background="@color/white">

            <include
                android:id="@+id/fillerLayout3"
                layout="@layout/fill_space" />

            <include
                android:id="@+id/redFlagQuestion3"
                layout="@layout/row_base" />


        </FrameLayout>

row_base.xml 视图层次结构如下:

+---------->LinearLayoutid=2131492990, res-name=redFlagQuestion1, visibility=VISIBLE, width=1080, height=780, 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=0.0,子数=1 | +----------->LinearLayoutid=2131493041, res-name=outerContainer, visibility=VISIBLE, width=1080, height=780, 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=0.0,子数=1 |

+------------>RelativeLayoutid=2131493042, res-name=questioContainer, visibility=VISIBLE, width=1080, height=780, 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=0.0, child-count=5 |

+------------->AppCompatTextViewid=2131493043, res-name=questionPanelQuestionNo, visibility=VISIBLE, width=1080, 高度=101,有焦点=假,有焦点=假, 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=30.0, text=1, input-type=0, ime-target=false, has-links=false |

+------------->AppCompatImageButtonid=2131493044, res-name=questionPanelUpArrow, visibility=GONE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0 |

+------------->AppCompatTextViewid=2131493045, res-name=questionPanelQestion, visibility=VISIBLE, width=960, 高度=152,有焦点=假,有焦点=假, 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=60.0, y=161.0, text=是孩子 变得反应迟钝?,input-type=0,ime-target=false, 有链接=假 |

+------------->AppCompatTextViewid=2131493046, res-name=questionPanelQestionExample, visibility=VISIBLE, width=960, 高度=114,有焦点=假,有焦点=假, 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=60.0, y=343.0, text=例如'not 回答问题'或'没有意义,输入类型=0, ime-target=false, has-links=false |

+------------->RelativeLayoutid=2131493047, res-name=actionButtonPanel, visibility=VISIBLE, width=1080, 高度=270,有焦点=假,有焦点=真, 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=480.0, child-count=4 |

+-------------->AppCompatImageButtonid=2131493048, res-name=buttonActionYes, visibility=VISIBLE, width=200, height=200, 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=260.0, y=0.0 |

+------------->AppCompatImageButtonid=2131493049, res-name=buttonActionNo, visibility=VISIBLE, width=200, height=200, 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=620.0, y=0.0 |

+-------------->AppCompatTextViewid=2131493050, res-name=txtViewYes, visibility=VISIBLE, width=200, height=49, 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=260.0, y=221.0, text=Yes, input-type=0, ime-target=false, has-links=false |

+------------->AppCompatTextViewid=2131493051, res-name=txtViewNo, visibility=VISIBLE, width=200, height=49, 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=620.0, y=221.0, text=No, input-type=0, ime-target=false, has-links=false |

【问题讨论】:

嘿,我对回收器视图适配器上的嵌套父布局有相同的查询。如果您找到任何解决方案,请分享您的宝贵答案提前谢谢您 【参考方案1】:

基于视图层次结构,您错误地按了 outerContainer 和 questioContainer 的顺序。

我会在 onView 中尝试不同的代码示例:

allOf(withId(R.id.buttonActionNo), withParent(withParent(withParent(withParent(withId(R.id.redFlagQuestion1)))))

【讨论】:

以上是关于Android Espresso - 嵌套父母的组合视图匹配器的主要内容,如果未能解决你的问题,请参考以下文章

Android的报错提示:Failed to resolve: com.android.support.test.espresso:espresso-core:3.0.2

Espresso Android,点击 WebView 元素错误

浓缩咖啡:适配器视图的 android.support.test.espresso.AmbiguousViewMatcherException

Android Espresso单元测试

Android - Espresso:为每次测试重新创建活动

Espresso 和 Android 联系人选择器