How to scroll with Appium 1.7.1 using TouchAction

Buzz picture Buzz · May 31, 2017 · Viewed 7.5k times · Source

I'm having trouble with scrolling down to a certain element in an iOS and Android app. Since the update from Appium 1.6.3 to 1.7.1 and io.appium to 6.1.0 the swipe method is deprecated and the only solution for it is to use TouchActions.

I tried to solve it with TouchActions but it didn't scroll at all or the scroll direction was wrong.

My solution so far looks like this, maybe someone can explain what I'm doing wrong:

public void scrollDownUntilElementVisible(WebElement element){
    TouchAction touchAction = new TouchAction(getDriver());
    for(int i=0; i<dimensions.getHeight();i++){
       if(element.isDisplayed()){
          break;
       }else{
          touchAction.press(0,0).moveTo(element).release().perform();
       }
    }
}

It's not the complete code, but I hope you get the idea.

How would it work if I would use the x,y-coordinates instead of the webElement I look for in my example? It does not work like the swipe method from the version before, or I didn't do it right. Maybe someone can explain it.

Answer

Rajasekhar picture Rajasekhar · Oct 5, 2018

On Latest Version of Appium need to add(PointOption.point while passing the coordinates) some code for scrolling using TouchAction:

private void scrollDown() {
    //if pressX was zero it didn't work for me
    int pressX = driver.manage().window().getSize().width / 2;
    // 4/5 of the screen as the bottom finger-press point
    int bottomY = driver.manage().window().getSize().height * 4/5;
    // just non zero point, as it didn't scroll to zero normally
    int topY = driver.manage().window().getSize().height / 8;
    //scroll with TouchAction by itself
    scroll(pressX, bottomY, pressX, topY);
}



private void scroll(int fromX, int fromY, int toX, int toY) {
    TouchAction touchAction = new TouchAction(driver);
    touchAction.longPress(PointOption.point(fromX, fromY)).moveTo(PointOption.point(toX, toY)).release().perform();
}