如何使用 TouchAction 在 Appium 1.7.1 中滚动 [英] How to scroll with Appium 1.7.1 using TouchAction

查看:21
本文介绍了如何使用 TouchAction 在 Appium 1.7.1 中滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法向下滚动到 iOS 和 Android 应用中的某个元素.由于从 Appium 1.6.3 到 1.7.1 和 io.appium 到 6.1.0 的更新,swipe 方法已被弃用,唯一的解决方案是使用 TouchActions.

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.

我尝试使用 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.

如果我使用 x,y 坐标而不是我在示例中寻找的 webElement,它会如何工作?它不像之前版本的swipe方法那样工作,或者我没有做对.也许有人可以解释一下.

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.

推荐答案

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

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

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

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

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

P.S.S.我用的是 appium 1.6.5

P.S.S. I used appium 1.6.5

这篇关于如何使用 TouchAction 在 Appium 1.7.1 中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆