2

I have started using Espresso for automating my tests and the problem I face is, am not able to match the exact textview from my xml file.

Sample xml be like:

xmlns:app="http://schemas.xx.com/apk/res-auto"

android:id="@+id/filter_sort_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="2dp">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:animateLayoutChanges="true"
    android:shrinkColumns="0">

    <TableRow>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/dropdown_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:clickable="true"
                android:ellipsize="end"
                android:focusable="true"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:paddingBottom="15dp"
                android:paddingEnd="5dp"
                android:paddingTop="15dp"
                android:textColor="@color/colorAccent" />

            <android.support.v7.widget.AppCompatImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="3dp"
                app:srcCompat="@drawable/ic_triangle" />
        </LinearLayout>

Am trying to check whether the id 'dropdown_title' is present and have to get the text from it. Code I used:

    onView(allOf(
    withId(R.id.dropdown_title),
    withParent(withId(R.id.filter_sort_layout)))).
    check(matches(isDisplayed();

But, it throws me error saying,

'android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: xx.debug:id/dropdown_title and has parent matching: with id: xx/filter_sort_layout)

View Hierarchy:

    +>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, 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=1080, height=1776, 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=16909227, 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=1080, height=1704, 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=72.0, child-count=1}
    |
    +--->FitWindowsLinearLayout{id=2131820663, res-name=action_bar_root, visibility=VISIBLE, width=1080, height=1704, 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}
    |'

I have tried using descendantOfA() and also hasSibling() methods. Doesn't work. Please help me out.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Pinky
  • 71
  • 5

1 Answers1

2

I think your current code is failing because dropdown_title is not an immediate child of filter_sort_layout. Rather, dropdown_title is a descendant of filter_sort_layout. Use isDescendantOfA instead:

onView(allOf(withId(R.id.dropdown_title),
       isDescendantOfA(withId(R.id.filter_sort_layout))))
    .check(matches(isDisplayed()));

Edit:

From comments below it appears that the OP actually has multiples views in the app all with the same name. The duplicate link covers how to handle this scenario.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks for replying. I tried isDescendantOfA(), but still am facing the same issue. – Pinky Nov 13 '17 at 07:11
  • So you get the same error with `isDescendantOfA` ? – Tim Biegeleisen Nov 13 '17 at 07:16
  • Yes, I used exactly the same code and it throws me this error : android.support.test.espresso.AmbiguousViewMatcherException: '(with id: xx:id/dropdown_title and is descendant of a: with id: xx:id/filter_sort_layout)' matches multiple views in the hierarchy. – Pinky Nov 13 '17 at 07:24
  • Hmmm...do you have more than one `filter_sort_layout` layout tag in your app? This seems to be what the error message is saying. – Tim Biegeleisen Nov 13 '17 at 07:26
  • The view is defined only once in xx.xml file but is used multiple times in all filterviews of the app. In that case, how do we match it? – Pinky Nov 13 '17 at 11:32
  • @Pinky You're going to need to [write some custom code](https://stackoverflow.com/questions/29378552/in-espresso-how-to-choose-one-the-view-who-have-same-id-to-avoid-ambiguousviewm) most likely. If I were you, I would just use unique view names everywhere. There are many arguments for using unique names always just as a matter of good Android practice alone. – Tim Biegeleisen Nov 13 '17 at 11:35
  • Cool.. Thanks :) – Pinky Nov 13 '17 at 11:49