androidandroid-espresso

Espresso select children of included layout


I have been using Espresso to carry out automated UI testing with an Android app. (I have been trying to find a resolution to the issue whilst at home from work, so I don’t have the exact examples and errors, but I can update tomorrow morning). I have run into an issue with unit testing buttons within a layout that is included multiple times within a single user interface. Below is a quick example:

<include 
   android:id="@+id/include_one"
   android:layout="@layout/boxes" />

<include 
   android:id="@+id/include_two"
   android:layout="@layout/boxes" />

<include 
    android:id="@+id/include_three"
    android:layout="@layout/boxes" />

Here is an example of what is within the @layout/boxes:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1" />
    <Button
        android:id="@+id/button2" />
</RelativeLayout>

I am seemingly unable to access button one within the include I want “include_one”, without accessing all three of the buttons.

I have tried accessing the buttons with the following:

onView(allOf(withId(R.id.include_one), isDescendantOfA(withId(R.id.button1)))).perform(click());

and

onView(allOf(withId(R.id.button1), hasParent(withId(R.id.include_one)))).perform(click());

Both of which I found from this answer: onChildView and hasSiblings with Espresso Unfortunately I haven’t had any success!

I know this isn’t great, but as I am not with my work computer I can’t tell you the exact errors I have come across, but I have encountered:

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException

also an error telling me there were no matches found.

The code I am using makes sense, although I am new to using Espresso Can anyone offer some advice, or point out what I may be misunderstanding?


Solution

  • This is a common pitfall when trying to <include/> the same custom xml several times in the same layout.

    If you now try calling

    Button button1 = (Button) findViewById(R.id.button1);
    

    since the boxes.xml is included more than once, you will always get as a result the button present in the first sub layout, and never another one.

    You were pretty close but you need to use the withParent() view matcher.

    onView(allOf(withId(R.id.button1), withParent(withId(R.id.include_one))))
                    .check(matches(isDisplayed()))
                    .perform(click());