How is it possible to scroll down to the bottom of ScrollView in Espresso test? Thanks!
Asked
Active
Viewed 3.5k times
4 Answers
50
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

appoll
- 2,910
- 28
- 40
-
I haven't tried that, but give it a shot and write a new question with the issue – appoll Jul 24 '15 at 22:39
-
4works only if you know your child elements - what to do if you dont know the last element? – PKAP Aug 19 '15 at 12:24
-
3`perform(swipeDown())`, that saved my day! – Thuy Trinh Sep 15 '17 at 08:28
-
1@IgorGanapolsky Take a look at https://medium.com/@devasierra/espresso-nestedscrollview-scrolling-via-kotlin-delegation-5e7f0aa64c09 – Tony Aug 22 '18 at 22:53
21
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());

j2emanue
- 60,549
- 65
- 286
- 456
13
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());
1
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.

Morozov
- 4,968
- 6
- 39
- 70
-
-
I believe this answer is missing information for whoever is not fully aware. Please check https://stackoverflow.com/questions/35272953/espresso-scrolling-not-working-when-nestedscrollview-or-recyclerview-is-in-coor for the full answer – Rafael Ruiz Muñoz Feb 01 '18 at 13:09