Espresso: How to do custom swipe e.g. swipeTop or swipeBottom

testsingh picture testsingh · Jan 29, 2015 · Viewed 15.5k times · Source

So far we can do:

  • swipeLeft
  • swipeRight
  • swipeUp
  • swipeDown

How can we swipeTop(all the way to the Top) or swipeBottom(all the way to the bottom) is expresso. Please give me an example if these methods already exists.

Answer

Alexander Pacha picture Alexander Pacha · Oct 4, 2015

Have you tried a GeneralSwipeAction like that?

private static ViewAction swipeFromTopToBottom() {
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER,
            GeneralLocation.BOTTOM_CENTER, Press.FINGER);
}

Maybe you have to provide a custom implementation for the second and/or third parameter, as Anna already mentioned:

new CoordinatesProvider() {
    @Override
    public float[] calculateCoordinates(View view) {
        float[] coordinates =  GeneralLocation.CENTER.calculateCoordinates(view);
        coordinates[1] = 0;
        return coordinates;
    }
}