javaandroidandroid-recyclerviewswiperefreshlayoutandroid-espresso-recorder

Robo Test Aborted when Clicked RecyclerView Item under SwipeRefreshLayout


My Robo script and Android Espresso test aborted whenever I clicked any item of the RecyclerView having id android:id="@+id/Primary_recycler_view. I have checked the reason and I found that this issue is because of SwipeRefreshLayout because if I removed that layout and run the script it gets successful.

So to rectify this problem I have added a relativeLayout below SwipeRefreshLayout having an id android:id="@+id/relative.So now Android Espresso test runs successfully but Robo Script still aborting. And as per my application requirement, I can't remove SwipeRefreshLayout

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/myConstraint_layOut"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

  <RelativeLayout
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/Primary_recycler_view"
        android:scrollbars="vertical"
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

  </RelativeLayout>

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

</RelativeLayout>

ROBO SCRIPT (Aborting)

[
  {
    "eventType": "VIEW_CLICKED",
    "timestamp": 1560334484260,
    "replacementText": "",
    "actionCode": -1,
    "delayTime": 0,
    "canScrollTo": false,
    "elementDescriptors": [
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": 0,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": -1,
        "resourceId": "com.example.myapplication:id/layoutAccounts",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.support.v7.widget.RecyclerView",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/Primary_recycler_view",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/relative",
        "contentDescription": "",
        "text": ""
      }
    ]
  },
  {
    "eventType": "VIEW_CLICKED",
    "timestamp": 1560334491086,
    "replacementText": "",
    "actionCode": -1,
    "delayTime": 0,
    "canScrollTo": false,
    "elementDescriptors": [
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": 5,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": -1,
        "resourceId": "com.example.myapplication:id/layoutAccounts",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.support.v7.widget.RecyclerView",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/Primary_recycler_view",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/relative",
        "contentDescription": "",
        "text": ""
      }
    ]
  },
  {
    "eventType": "VIEW_CLICKED",
    "timestamp": 1560334500084,
    "replacementText": "",
    "actionCode": -1,
    "delayTime": 0,
    "canScrollTo": false,
    "elementDescriptors": [
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": 14,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": -1,
        "resourceId": "com.example.myapplication:id/layoutAccounts",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.support.v7.widget.RecyclerView",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/Primary_recycler_view",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/relative",
        "contentDescription": "",
        "text": ""
      }
    ]
  },
  {
    "eventType": "VIEW_CLICKED",
    "timestamp": 1560334508008,
    "replacementText": "",
    "actionCode": -1,
    "delayTime": 0,
    "canScrollTo": false,
    "elementDescriptors": [
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": 22,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": -1,
        "resourceId": "com.example.myapplication:id/layoutAccounts",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.support.v7.widget.RecyclerView",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/Primary_recycler_view",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/relative",
        "contentDescription": "",
        "text": ""
      }
    ]
  },
  {
    "eventType": "VIEW_CLICKED",
    "timestamp": 1560334516473,
    "replacementText": "",
    "actionCode": -1,
    "delayTime": 0,
    "canScrollTo": false,
    "elementDescriptors": [
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": 28,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": -1,
        "resourceId": "com.example.myapplication:id/layoutAccounts",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.support.v7.widget.RecyclerView",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/Primary_recycler_view",
        "contentDescription": "",
        "text": ""
      },
      {
        "className": "android.widget.RelativeLayout",
        "recyclerViewChildPosition": -1,
        "adapterViewChildPosition": -1,
        "groupViewChildPosition": 0,
        "resourceId": "com.example.myapplication:id/relative",
        "contentDescription": "",
        "text": ""
      }
    ]
  }
]

Androi EspressoTest (Successfull)

package com.example.myapplication;


import android.support.test.espresso.ViewInteraction;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class tapss3 {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void tapss3() {
        ViewInteraction recyclerView = onView(
                allOf(withId(R.id.Primary_recycler_view),
                        childAtPosition(
                                withId(R.id.relative),
                                0)));
        recyclerView.perform(actionOnItemAtPosition(0, click()));

        ViewInteraction recyclerView2 = onView(
                allOf(withId(R.id.Primary_recycler_view),
                        childAtPosition(
                                withId(R.id.relative),
                                0)));
        recyclerView2.perform(actionOnItemAtPosition(3, click()));

        ViewInteraction recyclerView3 = onView(
                allOf(withId(R.id.Primary_recycler_view),
                        childAtPosition(
                                withId(R.id.relative),
                                0)));
        recyclerView3.perform(actionOnItemAtPosition(12, click()));

        ViewInteraction recyclerView4 = onView(
                allOf(withId(R.id.Primary_recycler_view),
                        childAtPosition(
                                withId(R.id.relative),
                                0)));
        recyclerView4.perform(actionOnItemAtPosition(27, click()));
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

Solution

  • Hey Guys I found the solution after some changes done in XML file. So I have added one more RelativeLayout having id android:id="@+id/relative2 above the RecyclerView and after this change robo script runs successfully without aborting.

    Final changed XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:id="@+id/myConstraint_layOut"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <RelativeLayout
                android:id="@+id/relative"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
            <RelativeLayout
                android:id="@+id/relative2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/Primary_recycler_view"
                android:scrollbars="vertical"
                android:layout_marginTop="50dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
            </RelativeLayout>
    
         </RelativeLayout>
    
        </android.support.v4.widget.SwipeRefreshLayout>
    
    </RelativeLayout>
    

    Final SuccessFull RoboScript

    [
      {
        "eventType": "VIEW_CLICKED",
        "timestamp": 1560409968465,
        "replacementText": "",
        "actionCode": -1,
        "delayTime": 0,
        "canScrollTo": false,
        "elementDescriptors": [
          {
            "className": "android.widget.RelativeLayout",
            "recyclerViewChildPosition": 2,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": -1,
            "resourceId": "com.example.myapplication:id/layoutAccounts",
            "contentDescription": "",
            "text": ""
          },
          {
            "className": "android.support.v7.widget.RecyclerView",
            "recyclerViewChildPosition": -1,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": 0,
            "resourceId": "com.example.myapplication:id/Primary_recycler_view",
            "contentDescription": "",
            "text": ""
          },
          {
            "className": "android.widget.RelativeLayout",
            "recyclerViewChildPosition": -1,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": 0,
            "resourceId": "com.example.myapplication:id/relative2",
            "contentDescription": "",
            "text": ""
          }
        ]
      },
      {
        "eventType": "VIEW_CLICKED",
        "timestamp": 1560409976951,
        "replacementText": "",
        "actionCode": -1,
        "delayTime": 0,
        "canScrollTo": false,
        "elementDescriptors": [
          {
            "className": "android.widget.RelativeLayout",
            "recyclerViewChildPosition": 15,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": -1,
            "resourceId": "com.example.myapplication:id/layoutAccounts",
            "contentDescription": "",
            "text": ""
          },
          {
            "className": "android.support.v7.widget.RecyclerView",
            "recyclerViewChildPosition": -1,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": 0,
            "resourceId": "com.example.myapplication:id/Primary_recycler_view",
            "contentDescription": "",
            "text": ""
          },
          {
            "className": "android.widget.RelativeLayout",
            "recyclerViewChildPosition": -1,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": 0,
            "resourceId": "com.example.myapplication:id/relative2",
            "contentDescription": "",
            "text": ""
          }
        ]
      },
      {
        "eventType": "VIEW_CLICKED",
        "timestamp": 1560409984752,
        "replacementText": "",
        "actionCode": -1,
        "delayTime": 0,
        "canScrollTo": false,
        "elementDescriptors": [
          {
            "className": "android.widget.RelativeLayout",
            "recyclerViewChildPosition": 25,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": -1,
            "resourceId": "com.example.myapplication:id/layoutAccounts",
            "contentDescription": "",
            "text": ""
          },
          {
            "className": "android.support.v7.widget.RecyclerView",
            "recyclerViewChildPosition": -1,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": 0,
            "resourceId": "com.example.myapplication:id/Primary_recycler_view",
            "contentDescription": "",
            "text": ""
          },
          {
            "className": "android.widget.RelativeLayout",
            "recyclerViewChildPosition": -1,
            "adapterViewChildPosition": -1,
            "groupViewChildPosition": 0,
            "resourceId": "com.example.myapplication:id/relative2",
            "contentDescription": "",
            "text": ""
          }
        ]
      }
    ]