简体   繁体   中英

How to write an espresso test case for a fragment container with recyclerview click item action

I have an activity which has a fragment container which loads a fragment with recyclerview. On clicking any of the recycler view items, it will open a new activity. I want to write an espresso test case for this scenario.

My Activity:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">

<FrameLayout
    android:id="@+id/fragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:layout_weight="1">

</FrameLayout>

My Fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MyTestCase:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

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

    @Before
    public void init() {
        mActivityTestRule.getActivity()
                .getSupportFragmentManager().beginTransaction();
    }

    @Test
    public void recyclerview_clickTest() {
       /* onView(withId(R.id.fragmentContainer)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
        onView(withText("Alpha")).perform(click());
        onView(allOf(instanceOf(TextView.class), withParent(withResourceName("action_bar"))))
                .check(matches(withText("Alpha")));*/

        onData(allOf(is(new BoundedMatcher<Object, MyModel>(MyModel.class) {
            @Override
            public void describeTo(Description description) {
            }
            @Override
            protected boolean matchesSafely(MyModel abc) {
                return onView(allOf(instanceOf(TextView.class), withParent(withResourceName("action_bar"))))
                        .check(matchesSafely(withResourceName(abc)));
            }
        })));
    }
}

TIA.

You can use ViewMatchers for this action

Example

onView(withId(R.id.your_recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

Try the following

onView(allOf(isDisplayed(), withId(R.id.your_recycler_view))).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

If the above still does not work and you're sure that the recycler view is on display then launch UIAutomator and try to highlight above your recycler view, check if the thing it highlights is your recycler view, chances are your recycler view might have something on top of it which makes it not clickable (or it might not even be on display).

Another thing to check is if there is a transition and no animation is in place, the test might be looking for your recycler view while it is not yet on display. You might want to check on https://developer.android.com/training/testing/espresso/idling-resource

For a quicker check, just make the thread sleep prior to clicking on the recycler view

Thread.sleep(timeMs, timeout)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM