How to Perform Scroll up and Down in Android For L

2020-08-05 10:41发布

问题:

I'm trying to scroll the page up and down could someone help me? I'm using appium version 1.6. and java client 6.01For latest Version it is Not Showing driver.scroll()methods or TouchAction action = new TouchAction (this.driver);action.press (startX, startY).moveTo (endX, endY) .release () .perform(); it is not performing press

回答1:

These methods are untested as I currently have no test apps that require scrolling, but I've put these together for my own future potential use. I have two methods for each scroll/swipe direction - one using Appium and the other using javascript executor. I'll show just the two scroll down routines I have below, and you should be able to determine easily how to write the other direcections from just these two examples.

First, the Appium version:

public void scrollDown() throws Exception {

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

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

    new TouchActions(driver)
            .down(startx, starty)
            .move(startx, endy)
            .release()
            .build()
            .perform();

}

Now the corresponding javascript version:

public void jsScrollDown() throws Exception {

    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, String> scrollObject = new HashMap<String, String>();
    scrollObject.put("direction", "down");
    js.executeScript("mobile: scroll", scrollObject);

}

Remember, neither of these are tested, but I am using the information I gathered from the Appium web site itself, so they should both work.