Search code examples
androidtestingandroid-testingandroid-espresso

Swipe until element visible in Android Espresso


I have a long vertical screen with data entry form. I need swipeUp (scroll down) this screen until one of form's field is displayed on screen. I try use swipeUp() and scrollTo() ViewActions on the elements located higher on the screen, but it didn't help me, that these elements hiding from screen after swiping.


Solution

  • The idiomatic approach today would be to use RepeatActionUntilViewState. For example, the following would swipe up on an element with ID largeElementId up to 10 times until a view with text "Target text" is visible to the user:

    onView(withId(R.id.largeElementId)).perform(repeatedlyUntil(swipeUp(), 
            hasDescendant(withText("Target text")), 
            10));