【问题标题】:How to scroll with Appium 1.7.1 using TouchAction如何使用 TouchAction 滚动 Appium 1.7.1
【发布时间】:2017-11-01 02:52:15
【问题描述】:

我在向下滚动到 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();
       }
    }
}

这不是完整的代码,但我希望你能明白。

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

【问题讨论】:

    标签: java appium appium-ios


    【解决方案1】:

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

    【讨论】:

    • 这完全适用于正常滚动,而在我的情况下,屏幕分为两个部分。第 1 节是静态的,第 2 节是可滚动的。这里怎么处理?
    【解决方案2】:

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

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

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

    附言我用的是appium 1.6.5

    【讨论】:

    • 谢谢@Yaroslav Havrylovych。上面的代码是从上到下工作的。你能帮我从下到上如何滚动吗?
    • 我之前没有看到你的评论。您是否尝试将滚动更改为: scroll(pressX, topY, pressX, bottomY); ?或者你需要别的东西(也许我没有得到问题)?我这里是晚上,所以我只能在第二天查看。
    • 实际上,它从上到下滚动按预期工作。但我需要从下到上滚动。
    • @YaroslavHavrylovych 的回答是有效的,但是如果您更新您的 appium 版本,您将看到 x,y 坐标不再可用于 TouchAction。您必须使用 PointOption 来封装 x,y 坐标。此外,如果要执行多次滚动来定位元素,则应添加 WaitOption。
    猜你喜欢
    • 2019-09-25
    • 2017-03-16
    • 2021-12-17
    • 2019-04-09
    • 2018-04-25
    • 2018-05-18
    • 1970-01-01
    • 2020-10-06
    • 2020-03-15
    相关资源
    最近更新 更多