如何使用TouchAction滚动Appium 1.7.1

我在向下滚动到iOS和Android应用中的某个元素时遇到了麻烦。 自从Appium 1.6.3更新到1.7.1和io.appium到6.1.0之后,不推荐使用滑动方法,唯一的解决方案是使用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(); } } } 

这不是完整的代码,但我希望你能得到这个想法。

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

我需要滚动才能找到屏幕外的元素。 我想出的是:

  1. 搜索我需要的元素,如果找到它(即在屏幕上) – 使用它。
  2. 如果没有找到(即在屏幕外) – scrollDown (在我的情况下我不需要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(); } 

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

PSS我使用了app 1.6.5

在最新版本的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(); }