shashank shekhar
shashank shekhar

Reputation: 155

How to scroll up in Android appium

I have an app page where I need to scroll vertically to reach bottom of the app for the save button.
I am trying below code but getting server side error.

 new TouchAction((PerformsTouchActions) driver).press(point(anchor, startPoint))
.waitAction(waitOptions(Duration.ofMillis(duration))).moveTo(point(anchor, endPoint)).release()
                .perform();

Is there any good way to achieve scroll functionality in android?

Upvotes: 1

Views: 9921

Answers (3)

Kovacic
Kovacic

Reputation: 1481

Here is an example how to achieve scrolling in Android:

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(startX, endY)
                    .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;

    }
}

and enum, to set direction:

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

and finally usage:

swipe(driver, DIRECTION.UP, 3);

Hope this helps,

Upvotes: 4

Sarah_Paul
Sarah_Paul

Reputation: 11

Try using this, I don't know how to call it in the main method as someone gave it to me so let me know if you figure that out

public void swipeRight() throws Exception {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting x location set to 5% of the width (near left)
    int startx = (int) (size.width * 0.05);
    //Ending x location set to 95% of the width (near right)
    int endx = (int) (size.width * 0.95);
    //y position set to mid-screen vertically
    int starty = size.height / 2;

    scroll(startx, starty, endx, starty);
}

/**
 * This method does a swipe downwards
 * @author Bill Hileman
 * @throws Exception
 */
public void scrollUp() throws Exception {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 20% of the height (near bottom)
    int starty = (int) (size.height * 0.20);
    //Ending y location set to 80% of the height (near top)
    int endy = (int) (size.height * 0.80);
    //x position set to mid-screen horizontally
    int startx = size.width / 2;

    scroll(startx, starty, startx, endy);
}

Upvotes: 1

Sajid Manzoor
Sajid Manzoor

Reputation: 517

If the application is hybrid and you are in web context you can try this:

driver.execute_script("arguments[0].scrollIntoView();", element)

you will need to pass any element which you know is at the top of the app, so maybe the application header.

However if the application is in native context then touchAction is a good choice as you have already implemented it.

But there is also another work around provided by appium as touch actions does not work in iOS and that is to use the mobile interface which is something like this :

driver.execute_script("mobile: scroll", {"direction": "up"})

Upvotes: 0

Related Questions