How to select with espresso : an item in an adapter, present in a fragment inside a framelayout of an activity?

Issue

My application has the MainActivity with a framelayout that acts as a container.

I use this container to be filled by different fragments when I navigate between screens. Now, inside one of these fragments

The main activity :

<LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"
    xmlns:tools"http://schemas.android.com/tools"
    android:layout_width"match_parent"
    android:orientation"vertical"
    android:layout_height"match_parent"
    tools:context".MainActivity">

    <include layout"@layout/toolbar" />


    <android.support.v4.widget.DrawerLayout
        android:layout_width"match_parent"
        android:id"@+id/drawerLayout"
        android:layout_height"match_parent">

        <FrameLayout
            android:layout_width"match_parent"
            android:layout_height"match_parent">

            <!-- Framelayout to display Fragments -->
            <FrameLayout
                android:id"@+id/frame_container"
                android:layout_width"match_parent"
                android:layout_height"match_parent"/>


        </FrameLayout>



        <!-- navigation drawer -->
        <RelativeLayout
            android:layout_gravity"left"
            android:layout_width"match_parent"
            android:background"#fff"
            android:layout_height"match_parent">

            <ListView
                android:id"@+id/left_drawer"
                android:layout_width"match_parent"
                android:layout_height"match_parent"
                android:background"#fff"
                android:dividerHeight"1dp"
                android:divider"@drawable/transparent"
                />
        </RelativeLayout>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

The fragment :

<?xml version"1.0" encoding"utf-8"?>
<RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"
    xmlns:ads"http://schemas.android.com/apk/res-auto"
    xmlns:customfont"http://schemas.android.com/apk/res-auto"
    android:layout_width"match_parent"
    android:layout_height"match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android"http://schemas.android.com/apk/res/android"
        android:id"@+id/swipeContainerNewFaces"
        android:layout_width"match_parent"
        android:layout_height"match_parent">

        <ListView
            android:id"@android:id/list"
            android:layout_alignParentTop"true"
            android:layout_width"match_parent"
            android:layout_height"match_parent"
            android:cacheColorHint"#00000000"
            android:divider"@null"
            android:listSelector"@android:color/transparent"
            android:background"@color/main_bg">
        </ListView>

    </android.support.v4.widget.SwipeRefreshLayout>



    <com.google.android.gms.ads.AdView
        android:id"@+id/adView"
        android:layout_width"match_parent"
        android:layout_height"wrap_content"
        android:layout_centerHorizontal"true"
        ads:adSize"SMART_BANNER"
        ads:adUnitId"@string/admob_publisher_id_radar"/>


    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        xmlns:android"http://schemas.android.com/apk/res/android"
        android:id"@+id/progressBar"
        android:layout_width"match_parent"
        android:layout_height"6dp"/>

    <LinearLayout
        android:id"@+id/layoutRequestTimeout"
        android:layout_width"wrap_content"
        android:layout_height"wrap_content"
        android:layout_centerInParent"true"
        android:orientation"vertical"
        android:layout_margin"20dp"
        android:visibility"invisible" >

        <io.newfaces.layout.MyTextView
            android:layout_width"wrap_content"
            android:layout_height"wrap_content"
            android:textAlignment"center"
            android:gravity"center_vertical"
            android:textSize"30sp"
            android:textColor"@color/app_main_color"
            customfont:fontName"Lato-BoldItalic.ttf"
            android:text"@string/radar_no_connection_title"/>

        <io.newfaces.layout.MyTextView
            android:layout_width"match_parent"
            android:layout_height"wrap_content"
            android:layout_marginTop"10dp"
            android:textSize"16sp"
            android:textColor"@color/black"
            android:gravity"center"
            customfont:fontName"Lato-ThinItalic.ttf"
            android:text"@string/radar_no_connection_message"/>

        <ImageView
            android:id"@+id/ivReloadRadar"
            android:layout_width"wrap_content"
            android:layout_height"wrap_content"
            android:layout_gravity"center_horizontal"
            android:padding"20dp"
            android:src"@drawable/ic_nav_refresh"/>

    </LinearLayout>

</RelativeLayout>

I have an adapter which has a layout which look like this :

<?xml version"1.0" encoding"utf-8"?>
<LinearLayout
    android:id"@+id/row_story_view"
    xmlns:android"http://schemas.android.com/apk/res/android"
    xmlns:customfont"http://schemas.android.com/apk/res-auto"
    android:orientation"vertical" android:layout_width"fill_parent"
    android:background"#f1f1f1"
    android:layout_height"match_parent">
<RelativeLayout
    android:layout_width"match_parent"
    android:layout_height"match_parent">
<LinearLayout
    android:id"@+id/storiesHeader"
    android:layout_width"match_parent"
    android:layout_height"50dp"
    android:orientation"horizontal"
    android:background"#ffffff"
    android:layout_marginTop"5.2dp">
    <FrameLayout
        android:id"@+id/storiesHeaderprofileImage"
        android:layout_weight"0.025"
        android:layout_marginLeft"10.1dp"
        android:layout_width"40dp"
        android:layout_height"50dp">
        <View
            android:id"@+id/storiesViewChatBorder"
            android:layout_width"40dp"
            android:layout_height"40dp"
            android:background"@drawable/border_not_visible"
            android:layout_gravity"center_vertical"/>
        <de.hdodenhof.circleimageview.CircleImageView
            android:id"@+id/storiesProfilePicture"
            android:layout_width"40dp"
            android:layout_height"40dp"
            android:layout_gravity"center_vertical"
            android:src"@drawable/com_facebook_profile_default_icon"
            android:transitionName"my_transition"/>
    </FrameLayout>
    <LinearLayout
        android:id"@+id/storiesHeaderProfileInfo"
        android:layout_width"wrap_content"
        android:layout_height"fill_parent"
        android:orientation"vertical"
        android:layout_marginLeft"6.2dp"
        android:layout_gravity"center_vertical"
        android:layout_weight"0.8">
        <io.newfaces.layout.MyTextView
            android:id"@+id/storiesName"
            android:layout_width"wrap_content"
            android:layout_height"fill_parent"
            android:gravity"center_vertical"
            android:textColor"#434343"
            android:text"Dinesh"
            android:textSize"14sp"
            android:layout_weight"0.6"
            customfont:fontName"Lato-BoldItalic.ttf"/>
    </LinearLayout>
    <LinearLayout
        android:id"@+id/storiesHeaderDistance"
        android:layout_width"wrap_content"
        android:layout_height"50dp"
        android:orientation"vertical"
        android:layout_marginRight"12.5dp"
        android:layout_weight"0.175">
        <io.newfaces.layout.MyTextView
            android:id"@+id/storiesDate"
            android:layout_width"fill_parent"
            android:layout_height"wrap_content"
            android:textColor"#000000"
            android:textSize"12sp"
            android:layout_weight"0.6"
            android:gravity"center_vertical|right"
            customfont:fontName"Lato-BoldItalic.ttf" />
        <io.newfaces.layout.MyTextView
            android:id"@+id/storiesLocation"
            android:layout_width"fill_parent"
            android:layout_height"wrap_content"
            android:layout_marginTop"-18dp"
            android:textColor"#797979"
            android:textSize"12sp"
            android:layout_weight"0.4"
            android:gravity"center_vertical|right"
            customfont:fontName"Lato-MediumItalic.ttf"/>
    </LinearLayout>
</LinearLayout>
    <RelativeLayout
        android:id"@+id/storiesBody"
        android:layout_width"wrap_content"
        android:layout_height"wrap_content"
        android:background"#f8f8f8"
        android:layout_below"@+id/storiesHeader">
        <RelativeLayout
            android:id"@+id/storiesBodyOnlyTextLayout"
            android:layout_width"match_parent"
            android:layout_height"250dp">
            <io.newfaces.layout.MyTextView
                android:id"@+id/storyTextOnly"
                android:layout_marginTop"110dp"
                android:layout_width"wrap_content"
                android:layout_height"wrap_content"
                android:textSize"24.5sp"
                android:textColor"#000000"
                android:textStyle"italic"
                customfont:fontName"Lato-MediumItalic.ttf"
                android:gravity"center"
                android:layout_alignParentLeft"true"
                android:layout_alignParentStart"true"
                android:layout_alignParentRight"true"
                android:layout_alignParentEnd"true" />
        </RelativeLayout>
        <RelativeLayout
            android:id"@+id/storiesBodyImageTextLayout"
            android:layout_width"match_parent"
            android:layout_height"250dp">
            <ImageView
                android:id"@+id/storyImage"
                android:scaleType"centerCrop"
                android:layout_width"match_parent"
                android:layout_height"250dp" />
            <View
                android:id"@+id/gradient"
                android:layout_width"match_parent"
                android:layout_height"250dp"
                android:alpha"0.5"
                android:background"@drawable/gradient" />
            <io.newfaces.layout.MyTextView
                android:id"@+id/storyTextWithImage"
                android:layout_width"match_parent"
                android:layout_height"wrap_content"
                android:layout_marginLeft"14.9dp"
                android:layout_marginRight"42.2dp"
                android:layout_marginBottom"16.4dp"
                android:textSize"16.5sp"
                android:textColor"#ffffff"
                android:gravity"bottom"
                customfont:fontName"Lato-MediumItalic.ttf"
                android:layout_alignBottom"@+id/storyImage"/>
        </RelativeLayout>
    </RelativeLayout>
    <LinearLayout
        android:id"@+id/storiesFooter"
        android:layout_width"match_parent"
        android:layout_height"50dp"
        android:layout_below"@id/storiesBody"
        android:layout_marginBottom"20dp"
        android:background"#ffffff"
        android:orientation"horizontal">
        <LinearLayout
            android:id"@+id/storiesLikesLinLayout"
            android:layout_width"wrap_content"
            android:layout_height"match_parent"
            android:layout_weight"0.3"
            android:orientation"vertical">
            <RelativeLayout
                android:id"@+id/storiesLikesRelLayout"
                android:layout_width"wrap_content"
                android:layout_height"match_parent"
                android:layout_gravity"center_horizontal">
                <LinearLayout
                    android:id"@+id/storiesLikesLinInRelLayout"
                    android:layout_width"wrap_content"
                    android:layout_height"match_parent"
                    android:gravity"center"
                    android:orientation"horizontal">
                    <ImageView
                        android:id"@+id/likesImg"
                        android:layout_width"14.6dp"
                        android:layout_height"13.4dp"
                        android:src"@drawable/ic_story_like"/>
                    <io.newfaces.layout.MyTextView
                        android:id"@+id/noOfLikesForStory"
                        android:layout_width"wrap_content"
                        android:layout_height"wrap_content"
                        android:textSize"15sp"
                        customfont:fontName"Lato-Light.ttf"
                        android:layout_marginLeft"5.6dp"
                        android:text"0 likes">
                    </io.newfaces.layout.MyTextView>
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
        <View
            android:id"@+id/borderShare1"
            android:layout_width"1.5dp"
            android:layout_height"match_parent"
            android:background"#d8d8d8"/>
        <LinearLayout
            android:id"@+id/storiesCommentsLinLayout"
            android:layout_width"wrap_content"
            android:layout_height"match_parent"
            android:layout_weight"0.4"
            android:orientation"vertical">
            <RelativeLayout
                android:id"@+id/storiesCommentsRelLayout"
                android:layout_width"wrap_content"
                android:layout_height"match_parent"
                android:layout_gravity"center_horizontal">
                <LinearLayout
                    android:id"@+id/storiesCommentsLinInRelLayout"
                    android:layout_width"wrap_content"
                    android:layout_height"match_parent"
                    android:gravity"center"
                    android:orientation"horizontal">
                    <ImageView
                        android:id"@+id/commentsImg"
                        android:layout_width"14.6dp"
                        android:layout_height"13.4dp"
                        android:src"@drawable/ic_story_comment"/>
                    <io.newfaces.layout.MyTextView
                        android:id"@+id/noOfComments"
                        android:layout_width"wrap_content"
                        android:layout_height"wrap_content"
                        android:textSize"15sp"
                        customfont:fontName"Lato-Light.ttf"
                        android:layout_marginLeft"5.6dp"
                        android:text"0 comments">
                    </io.newfaces.layout.MyTextView>
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
        <View
            android:id"@+id/borderShare2"
            android:layout_width"1.5dp"
            android:layout_height"match_parent"
            android:background"#d8d8d8"/>
        <LinearLayout
            android:id"@+id/storiesShareLinLayout"
            android:layout_width"wrap_content"
            android:layout_height"match_parent"
            android:layout_weight"0.3"
            android:orientation"vertical">
            <RelativeLayout
                android:id"@+id/storiesShareRelLayout"
                android:layout_width"wrap_content"
                android:layout_height"match_parent"
                android:layout_gravity"center_horizontal">
                <LinearLayout
                    android:id"@+id/storiesShareLinInRelLayout"
                    android:layout_width"wrap_content"
                    android:layout_height"match_parent"
                    android:gravity"center"
                    android:orientation"horizontal">
                    <ImageView
                        android:id"@+id/shareImg"
                        android:layout_width"14.6dp"
                        android:layout_height"13.4dp"
                        android:src"@drawable/ic_story_share"/>
                    <io.newfaces.layout.MyTextView
                        android:id"@+id/buttonShare"
                        android:layout_width"wrap_content"
                        android:layout_height"wrap_content"
                        android:textSize"15sp"
                        customfont:fontName"Lato-Light.ttf"
                        android:layout_marginLeft"5.6dp"
                        android:text"Share">
                    </io.newfaces.layout.MyTextView>
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
</LinearLayout>

I want select the storiesCommentsLinLayout or storiesBodyOnlyTextLayout in this with espresso.

I tried with onData(withClild(anyOf(withText(“mySampleText”)))) and got error

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'is assignable from class: class android.widget.AdapterView'.

at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1970)

Caused by: java.lang.RuntimeException: No data found matching: has child: (with text: is "royalTreatment") contained values: <[Data: io.newfaces.bo.Story@ad6689a (class: io.newfaces.bo.Story) token: 0, Data: io.newfaces.bo.Story@245a5cb (class: io.newfaces.bo.Story) token: 1, Data: io.newfaces.bo.Story@62da1a8 (class: io.newfaces.bo.Story) token: 2, Data: io.newfaces.bo.Story@89f34c1 (class: io.newfaces.bo.Story) token: 3, Data: io.newfaces.bo.Story@c227466 (class: io.newfaces.bo.Story) token: 4, Data: io.newfaces.bo.Story@706d3a7 (class: io.newfaces.bo.Story) token: 5, Data: io.newfaces.bo.Story@c8f3054 (class: io.newfaces.bo.Story) token: 6, Data: io.newfaces.bo.Story@ea95bfd (class: io.newfaces.bo.Story) token: 7, Data: io.newfaces.bo.Story@ad170f2 (class: io.newfaces.bo.Story) token: 8, Data: io.newfaces.bo.Story@8c30343 (class: io.newfaces.bo.Story) token: 9]>

Solution

When you are using the onData statement you are working with data classes (as I can see io.newfaces.bo.Story in your case) and not with Views. So you should define what io.newfaces.bo.Story is matches in your onData and then you can make necessary actions on View that is corresponded to this data.

Something like this:

onData(allOf(is(new BoundedMatcher<Object, Person>(Person.class) {
    @Override
    public void describeTo(Description description) {
    }
    @Override
    protected boolean matchesSafely(Story story) {
        return story.id  123;
   }
})))

I am expecting here that your Story object has an id (as example).

You can find more details here

Answered By – Vyacheslav Pedak

Leave a Comment