How to swipe Left to Right in Appium?

Sujit picture Sujit · Jul 6, 2018 · Viewed 7.6k times · Source

Since swipe() is deprecated, I am unable to swipe the screen from Left to Right. My App has 4 banners in it and I want to swipe to view all the banners.

Answer

Kovacic picture Kovacic · Jul 9, 2018

This applies in all directions:

enum:

public enum DIRECTION {
    DOWN, UP, LEFT, RIGHT;
}

actual code:

public static void swipe(MobileDriver driver, DIRECTION direction, long duration) {
    Dimension size = driver.manage().window().getSize();

    int startX = 0;
    int endX = 0;
    int startY = 0;
    int endY = 0;

    switch (direction) {
        case RIGHT:
            startY = (int) (size.height / 2);
            startX = (int) (size.width * 0.90);
            endX = (int) (size.width * 0.05);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(endX, startY)
                    .release()
                    .perform();
            break;

        case LEFT:
            startY = (int) (size.height / 2);
            startX = (int) (size.width * 0.05);
            endX = (int) (size.width * 0.90);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(endX, startY)
                    .release()
                    .perform();

            break;

        case UP:
            endY = (int) (size.height * 0.70);
            startY = (int) (size.height * 0.30);
            startX = (size.width / 2);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(endX, startY)
                    .release()
                    .perform();
            break;


        case DOWN:
            startY = (int) (size.height * 0.70);
            endY = (int) (size.height * 0.30);
            startX = (size.width / 2);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(startX, endY)
                    .release()
                    .perform();

            break;

    }
}

usage:

swipe(driver,DIRECTION.RIGHT);

Hope this helps,