简体   繁体   English

Espresso:如何滚动到ScrollView的底部

[英]Espresso: how to scroll to the bottom of ScrollView

How is it possible to scroll down to the bottom of ScrollView in Espresso test? 如何在Espresso测试中向下滚动到ScrollView的底部? Thanks! 谢谢!

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. 如果在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 注意:如果已经显示视图,scrollTo将不起作用,因此您可以在显示视图时安全地使用它

for me when using nestedScrollview i just swipeUp (if you want to go down)..here is an example call: 对我来说,当使用nestedScrollview我只是swipeUp(如果你想要下去)..这是一个示例调用:

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

For completeness (based on Morozov's answer), you can pass a custom ViewAction instead of scrollTo() , which allows to use NestedScrollView : 为了完整性(基于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);
    }
};

And use it like this: 并像这样使用它:

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

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. 如果你在android.support.v4.widget.NestedScrollView中有一个视图而不是scrollView scrollTo()不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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