How to scroll with Appium 1.7.1 using TouchAction

天涯浪子 提交于 2019-11-28 01:38:55

问题


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.


回答1:


I needed scroll to find elements which are out of the screen. What I come up with is:

  1. Search for the element you need on the current/visible screen.
  2. If the element is not found (i.e. out of the screen) - scrollDown (in my case I was needed only scroll down) and go to step 1 again. I had limited the test with 4 iterations as it was enough in my case, so use your own condition here.
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);
}

/*
 * don't forget that it's "natural scroll" where 
 * fromY is the point where you press the and toY where you release it
 */
private void scroll(int fromX, int fromY, int toX, int toY) {
    TouchAction touchAction = new TouchAction(driver);
    touchAction.longPress(fromX, fromY).moveTo(toX, toY).release().perform();
}

P.S. you could get coordinates from the element and use it in scroll.

P.S.S. I used appium 1.6.5




回答2:


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();
}


来源:https://stackoverflow.com/questions/44282417/how-to-scroll-with-appium-1-7-1-using-touchaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!