如何使用TouchAction与Appium 1.7.1滚动

Buz*_*uzz 3 java appium appium-ios

我在向下滚动到iOS和Android应用程序中的某个元素时遇到麻烦。由于从Appium 1.6.3更新到1.7.1,将io.appium更新到6.1.0,因此不建议使用swipe方法,唯一的解决方案是使用TouchActions。

我尝试使用TouchActions解决它,但是它根本没有滚动,或者滚动方向错误。

到目前为止,我的解决方案看起来像这样,也许有人可以解释我做错了什么:

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();
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

它不是完整的代码,但是希望您能理解。

如果我使用x,y坐标代替我在示例中查找的webElement,它将如何工作?它不能像以前版本中的“滑动”方法那样工作,或者我做得不好。也许有人可以解释。

Raj*_*har 9

在最新Appium的版本需要添加(PointOption.point同时通过坐标)一些代码使用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();
}
Run Code Online (Sandbox Code Playgroud)


Yar*_*ych 6

我需要滚动查找屏幕之外的元素。我想出的是:

  1. 在当前/可见屏幕上搜索所需的元素。
  2. 如果找不到该元素(即不在屏幕上)- scrollDown(在我的情况下,只需要向下滚动)并再次转到步骤1。我已经将测试限制为4次迭代,因为对于我而言,这已经足够了,因此请在此处使用您自己的条件。
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();
}
Run Code Online (Sandbox Code Playgroud)

PS您可以从元素获取坐标并在中使用它scroll

PSS我用过鸦片1.6.5