Espresso, scrolling not working when NestedScrollView or RecyclerView is in CoordinatorLayout Espresso, scrolling not working when NestedScrollView or RecyclerView is in CoordinatorLayout android android

Espresso, scrolling not working when NestedScrollView or RecyclerView is in CoordinatorLayout


This is happening because the Espresso scrollTo() method explicitly checks the layout class and only works for ScrollView & HorizontalScrollView. Internally it's using View.requestRectangleOnScreen(...) so I'd expect it to actually work fine for many layouts.

My workaround for NestedScrollView was to take ScrollToAction and modify that constraint. The modified action worked fine for NestedScrollView with that change.

Changed method in ScrollToAction class:

public Matcher<View> getConstraints() {    return allOf(withEffectiveVisibility(Visibility.VISIBLE), isDescendantOfA(anyOf(            isAssignableFrom(ScrollView.class), isAssignableFrom(HorizontalScrollView.class), isAssignableFrom(NestedScrollView.class))));}

Convenience method:

public static ViewAction betterScrollTo() {    return ViewActions.actionWithAssertions(new NestedScrollToAction());}


Here is how I did the same thing that @miszmaniac did in Kotlin. With delegation in Kotlin, it is much cleaner and easier because I don't have to override the methods I don't need to.

class ScrollToAction(    private val original: android.support.test.espresso.action.ScrollToAction = android.support.test.espresso.action.ScrollToAction()) : ViewAction by original {  override fun getConstraints(): Matcher<View> = anyOf(      allOf(          withEffectiveVisibility(Visibility.VISIBLE),          isDescendantOfA(isAssignableFrom(NestedScrollView::class.java))),      original.constraints  )}


I had this issue with CoordinatorLayout->ViewPager->NestedScrollView an easy work around from me to get the same scrollTo() behavior was to just swipe up on the screen:

onView(withId(android.R.id.content)).perform(ViewActions.swipeUp());