19

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.

blurfus
  • 13,485
  • 8
  • 55
  • 61
Testing Singh
  • 1,347
  • 1
  • 15
  • 26

6 Answers6

23

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;
    }
}
Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
11

For example if you use recyclerView in yours application, you can use something like this:

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())

or

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeDown())
Morozov
  • 4,968
  • 6
  • 39
  • 70
5

@testsingh i think there's no out-off-box method to do the "swipeTop" or "swipeBottom" following looping maybe the easiest way (dumbest) that worked for me XDDD

//swipeUp 100 times, vice versa for swipeDown

for(int i=0;i<=100;i++){
  onView(withText("label")).perform(swipeUp());
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Yi Te Lu
  • 81
  • 1
  • 5
5

I know I am late to the party but after quite a bit of finagling I finally found something that can swipe top to bottom, left to right, etc. - without needing any resource id's.

The reason I needed it was for a dynamically populated view where everything was completely ambiguous. With the method below I can scroll all the way to the bottom and or even change the delay to scroll down just a single page.

static void swiper(int start, int end, int delay) {
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    Instrumentation inst = getInstrumentation();

    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 500, start, 0);
    inst.sendPointerSync(event);
    eventTime = SystemClock.uptimeMillis() + delay;
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 500, end, 0);
    inst.sendPointerSync(event);
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 500, end, 0);
    inst.sendPointerSync(event);
    SystemClock.sleep(2000); //The wait is important to scroll
}

I do not need left to right, etc. so I hardcoded the 500 in there (500 being the x axis).

and to call them I did it I did this -

 // This swipes all the way to the bottom of the screen
public static void swipeToBottom(){
    swiper(1000, 100, 0);
}

// This scrolls down one page at a time
public static void scrollSlowlyDown(){
    swiper(775, 100, 100);
}

// This swipes to the top
public static void swipeToTop(){
    swiper(100, 1000, 0);
}

// This scrolls up one page at a time
public static void scrollSlowlyUp(){
    swiper(100, 775, 100);
}

I hope this helps anyone that stumbles up this.

Gillsoft AB
  • 4,185
  • 2
  • 19
  • 35
Nefariis
  • 3,451
  • 10
  • 34
  • 52
  • not sure why i got downvoted - this works quite well – Nefariis Nov 09 '17 at 02:37
  • unfortunately at targetSdk 26, running on Android 7 this throws a: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission, which is only granted to system apps. – cV2 Nov 14 '17 at 18:26
4

I think it could be done by writing complex ViewActions by performing swipe.

public static ViewAction swipeToTop() {
    return new MySwipeAction(Swipe.FAST,
        GeneralLocation.CENTER,
        new CoordinatesProvider() {
            @Override
            public float[] calculateCoordinates(View view) {
                float[] coordinates =  GeneralLocation.CENTER.calculateCoordinates(view);
                coordinates[1] = 0;
                return coordinates;
            }
    }, Press.FINGER);
}


public static ViewAction swipeToBottom() {
    return new MySwipeAction(Swipe.FAST,
        GeneralLocation.CENTER,
        new CoordinatesProvider() {
            @Override
            public float[] calculateCoordinates(View view) {
                float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
                coordinates[1] = view.getContext().getResources().getDisplayMetrics().heightPixels;
                return coordinates;
            }
    }, Press.FINGER);
}

where MySwipeAction could be something like this:

public class MySwipeAction implements ViewAction {
    public MySwipeAction(Swiper swiper, CoordinatesProvider startCoordProvide, CoordinatesProvider endCoordProvide, PrecisionDescriber precDesc) { 
           // store here in class variables to use in perform
           ...
    }

    @Override public Matcher<View> getConstraints() {...}

    @Override public String getDescription() {...}

    @Override
    public void perform(UiController uiController, View view) {
        ...
        float[] startCoord = startCoordProvide.calculateCoordinates(view);
        float[] finalCoord = endCoordProvide.calculateCoordinates(view);
        float[] precision =  precDesc.describePrecision();

        Swiper.Status status = Swiper.Status.FAILURE;

        // you could try this for several times until Swiper.Status is achieved or try count is reached
        try {
            status = m_swiper.sendSwipe(uiController, startCoord, finalCoord, precision);
        } catch (RuntimeException re) {
            ...
        }

        // ensures that the swipe has been run.
        uiController.loopMainThreadForAtLeast(ViewConfiguration.getPressedStateDuration());
    }
}

I hope this could help you.

Bob Liberatore
  • 860
  • 10
  • 24
Anna
  • 759
  • 7
  • 9
  • 2
    is too many comments and dots... if u have "full" example of your code, please show. Many peoples doesn t know what they can to input in yours '...' – Morozov Feb 22 '17 at 14:51
  • I agree. It's confusing to exclude variable definitions and assignments that are used in your code. It's not worth saving a few lines if it makes code harder to read. – Bob Liberatore Feb 24 '17 at 18:26
1
// swipe up
for(int i=0;i<=3;i++){
    onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(actionOnItemAtPosition(4, swipeUp()));
}


// swipe Down  
for(int i=0;i<=3;i++){
     onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(actionOnItemAtPosition(7, swipeDown()));
}
hannes ach
  • 16,247
  • 7
  • 61
  • 84
Max
  • 11
  • 1