Discovering Espresso for Android: swiping.

In this post we have some tips about swipe view actions in Espresso for Android.

As you may know the latest Espresso release contains new left and right swiping actions: swipeLeft() and swipeRight(). They both are really useful when you'd like to swipe between activity fragments, tab layouts or any other UI elements.

You can use it as any other view action:


But be aware that doing this you will operate on a view, in our case, but not on the screen size. That means that to swipe right or left, for example between fragments you have to deal with some parent layout or maybe list view.

If you take a look inside Espresso's class you will see below code for swipeRight() method:

public static ViewAction swipeRight() {
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.CENTER_LEFT,
        GeneralLocation.CENTER_RIGHT, Press.FINGER);

As you may guess GeneralLocation.CENTER_LEFT and GeneralLocation.CENTER_RIGHT are the from and to coordinates accordingly inside the view. The full positions list which can be used as from and to coordinates are: TOP_LEFT, TOP_CENTER, TOP_RIGHT, CENTER_LEFT, CENTER, CENTER_RIGHT, BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT.

For other two parameters' names Swipe.FAST and Press.FINGER it's clear without saying what each of them means. For now Swipe has only FAST (100 milliseconds) and SLOW (1500 milliseconds) swipe speeds, and Press has PINPOINT (1x1 mm), FINGER (16x16 mm) and THUMB (25x25 mm) press areas.

Of course there is always an option to implement your own speed and press area - thanks for Espresso team for making the source code open.

Seems to be clear enough, right?

And the last thing - implementing our own swipe down and up ViewActions which can be used to initiate swipe down to refresh action or to perform swipe up (going to the bottom of the view), since they are not yet included into Espresso's code:

public static ViewAction swipeDown() {
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER,
        GeneralLocation.BOTTOM_CENTER, Press.FINGER);

public static ViewAction swipeUp() {
    return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER,
        GeneralLocation.TOP_CENTER, Press.FINGER);