Espresso:如何滚动到 ScrollView 的底部

Espresso: how to scroll to the bottom of ScrollView

如何在 Espresso 测试中向下滚动到 ScrollView 的底部? 谢谢!

如果您需要在 ScrollView 的底部找到一个视图并与之匹配某些内容,则只需对其执行 scrollTo() 操作,然后再执行任何其他需要显示它的操作。

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

你也可以试试:

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

如果您在 android.support 中有一个视图而不是 scrollView scrollTo() 则不起作用。

为了完整性(基于 Morozov 的回答),您可以传递自定义 ViewAction 而不是 scrollTo(),这允许使用 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);
    }
};

并像这样使用它:

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

对我来说,当我使用 nestedScrollview 时,我只是向上滑动(如果你想向下滑动)。这是一个调用示例:

onView(withId(R.id.nsv_container))
                .perform(swipeUp());