简体   繁体   中英

RecyclerView not scrolling on android espresso test

I want to scroll a recyclerView and click on an item which is initially not visible on screen, with Espresso, but the scroll action is not working (it does in other tests for other recyclerViews in other activities).

I have tried the onData() method indicated in https://developer.android.com/training/testing/espresso/lists

but it won't work as well.

Here is my method to perform the scroll and click :

fun scrollToPositionAndClick(resId: Int, position: Int) {
        getActivityInstance()?.findViewById<RecyclerView>(resId)?.adapter?.let {
            onView(withId(resId))
                .perform(
                    scrollToPosition<RecyclerView.ViewHolder>(position),
                    actionOnItemAtPosition<RecyclerView.ViewHolder>(position, click())
                )
        }
    }

This method works in other activities tests but for some reason it doesn't for this screen (fragment) and I get this error :

androidx.test.espresso.PerformException: Error performing 'single click - At Coordinates: 290, 2197 and precision: 16, 16'
...
Caused by: androidx.test.espresso.PerformException: Error performing 'click (after 3 attempts)' on view 'unknown'.

I had this not scrolling issue when I had a RecyclerView inside a ViewPager. I was able to fix it by doing my own implementation of a RecyclerViewAction.scrollToPosition. Instead of using the API of the recyclerView scroll to, I used the API of the LayoutManager and if it's a LinearLayoutManager, I used the scrollToPositionWithOffset.

Here is the code for the action. Everything is the same as the original except the perform method

public final class CustomRecyclerViewActions {

private CustomRecyclerViewActions() { }


public static <VH extends RecyclerView.ViewHolder> ViewAction scrollToPosition(@IntRange(from = 0) final int position) {
   return new ScrollToPositionViewAction(position);
}

private static final class ScrollToPositionViewAction implements ViewAction {
   private final int position;

   private ScrollToPositionViewAction(int position) {
     this.position = position;
   }

   public Matcher<View> getConstraints() {
      return Matchers.allOf(ViewMatchers.isAssignableFrom(RecyclerView.class), ViewMatchers.isDisplayed());
   }

  public String getDescription() {
    int var1 = this.position;
    return (new StringBuilder(44)).append("scroll RecyclerView to position: ").append(var1).toString();
  }

  public void perform(@NonNull UiController uiController, @NonNull View view) {
    RecyclerView recyclerView = (RecyclerView)view;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
      ((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(this.position, 0);
    } else {
      layoutManager.scrollToPosition(this.position);
    }
    uiController.loopMainThreadUntilIdle();
   }
 }
}

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