How to do “Custom scroll” when “swipe ” and “scrollto” approach not work in Espresso?
In espresso when we have to scroll to any of the element and in that page if the code having nested scroll then “swipeup()”, “swipedown()” and “scrollto()” approach may not work at some time.
So here is the solution where we can scroll to the desired position of element which is not currently displaying on screen but will be displayed on scroll.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
ViewAction customScrollTo = new ViewAction() { @Override public Matcher<View> getConstraints() { return CoreMatchers.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); } }; |
After this we have to use “custom scroll” like this :-
1) For matching text of element and scroll.
1 |
onView(withText("Address Book")).perform(customScrollTo,click()); |
2) When searching on the basis of id and scroll.
1 |
onView(withId(R.id.save_account_info)).perform(customScrollTo,click()); |
3) When searching on the basis of string and scroll.
1 |
onView(withText(R.string.price_from)).perform(customScrollTo,click()); |