Android Espresso - 嵌套父级的

时间:2017-03-06 11:31:54

标签: android layout nested android-espresso

我想找到一个带有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_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- redFlagQuestion1  -->
        <FrameLayout android:id="@+id/questionOneContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white">

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

        </FrameLayout>

        <!-- redFlagQuestion 2 -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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_width="match_parent"
            android:layout_height="wrap_content"
            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视图层次结构如下:

  

+ ----------&gt; LinearLayout {id = 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,child-count = 1} |   + -----------&gt; LinearLayout {id = 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,child-count = 1} |

     

+ ------------&gt; RelativeLayout {id = 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} |

     

+ -------------&gt; AppCompatTextView {id = 2131493043,res-name = questionPanelQuestionNo,visibility = VISIBLE,width = 1080,   height = 101,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 = 30.0,text = 1,input-type = 0,   ime-target = false,has-links = false} |

     

+ -------------&gt; AppCompatImageButton {id = 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} |

     

+ -------------&gt; AppCompatTextView {id = 2131493045,res-name = questionPanelQestion,visibility = VISIBLE,width = 960,   height = 152,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 = 60.0,y = 161.0,text =是孩子   变得反应迟钝?,input-type = 0,ime-target = false,   has-links = false} |

     

+ -------------&gt; AppCompatTextView {id = 2131493046,res-name = questionPanelQestionExample,visibility = VISIBLE,width = 960,   height = 114,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 = 60.0,y = 343.0,text =例如,&#39; not   回答问题&#39;或者&#39;没有意义,输入类型= 0,   ime-target = false,has-links = false} |

     

+ -------------&gt; RelativeLayout {id = 2131493047,res-name = actionButtonPanel,visibility = VISIBLE,width = 1080,   height = 270,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 = 480.0,child-count = 4} |

     

+ --------------&gt; AppCompatImageButton {id = 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} |

     

+ --------------&gt; AppCompatImageButton {id = 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} |

     

+ --------------&gt; AppCompatTextView {id = 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} |

     

+ --------------&gt; AppCompatTextView {id = 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 个答案:

答案 0 :(得分:1)

根据视图层次结构,您按照outerContainer和questioContainer的顺序错误地设置了。

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

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