Espresso: how to scroll to the bottom of ScrollView

后端 未结 4 1087
渐次进展
渐次进展 2021-02-05 05:50

How is it possible to scroll down to the bottom of ScrollView in Espresso test? Thanks!

相关标签:
4条回答
  • 2021-02-05 06:06

    For completeness (based on Morozov's answer), you can pass a custom ViewAction instead of scrollTo(), which allows to use NestedScrollView:

    ViewAction customScrollTo = new ViewAction() {
    
        @Override
        public Matcher<View> getConstraints() {
            return allOf(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), isDescendantOfA(anyOf(
                isAssignableFrom(ScrollView.class),
                isAssignableFrom(HorizontalScrollView.class),
                isAssignableFrom(NestedScrollView.class)))
            );
        }
    
        @Override
        public String getDescription() {
            return null;
        }
    
        @Override
        public void perform(UiController uiController, View view) {
            new ScrollToAction().perform(uiController, view);
        }
    };
    

    And use it like this:

    onView(withId(R.id.onBottomOfScrollView)).perform(customScrollTo, click());
    
    0 讨论(0)
  • 2021-02-05 06:07

    for me when using nestedScrollview i just swipeUp (if you want to go down)..here is an example call:

    onView(withId(R.id.nsv_container))
                    .perform(swipeUp());
    
    0 讨论(0)
  • 2021-02-05 06:15

    If at the bottom of the ScrollView you need to find a view and match something against it, then simply perform the scrollTo() action on it, before any other actions that require it to be displayed.

    onView(withId(R.id.onBottomOfScrollView))
        .perform(scrollTo(), click());
    

    Note: scrollTo will have no effect if the view is already displayed so you can safely use it in cases when the view is displayed

    0 讨论(0)
  • 2021-02-05 06:15

    Also u can try:

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

    If you have a view inside android.support.v4.widget.NestedScrollView instead of scrollView scrollTo() does not work.

    0 讨论(0)
提交回复
热议问题