【问题标题】:How to scroll up in Android appium如何在 Android appium 中向上滚动
【发布时间】:2018-12-03 17:01:28
【问题描述】:

我有一个应用页面,我需要垂直滚动到应用底部才能找到保存按钮。
我正在尝试以下代码,但出现服务器端错误。

 new TouchAction((PerformsTouchActions) driver).press(point(anchor, startPoint))
.waitAction(waitOptions(Duration.ofMillis(duration))).moveTo(point(anchor, endPoint)).release()
                .perform();

有什么好的方法可以在android中实现滚动功能吗?

【问题讨论】:

标签: java android selenium automation appium


【解决方案1】:

下面是一个如何在Android中实现滚动的例子:

public static void swipe(MobileDriver driver, DIRECTION direction, long duration) {
    Dimension size = driver.manage().window().getSize();

    int startX = 0;
    int endX = 0;
    int startY = 0;
    int endY = 0;

    switch (direction) {
        case RIGHT:
            startY = (int) (size.height / 2);
            startX = (int) (size.width * 0.90);
            endX = (int) (size.width * 0.05);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(endX, startY)
                    .release()
                    .perform();
            break;

        case LEFT:
            startY = (int) (size.height / 2);
            startX = (int) (size.width * 0.05);
            endX = (int) (size.width * 0.90);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(endX, startY)
                    .release()
                    .perform();

            break;

        case UP:
            endY = (int) (size.height * 0.70);
            startY = (int) (size.height * 0.30);
            startX = (size.width / 2);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(startX, endY)
                    .release()
                    .perform();
            break;


        case DOWN:
            startY = (int) (size.height * 0.70);
            endY = (int) (size.height * 0.30);
            startX = (size.width / 2);
            new TouchAction(driver)
                    .press(startX, startY)
                    .waitAction(Duration.ofMillis(duration))
                    .moveTo(startX, endY)
                    .release()
                    .perform();

            break;

    }
}

和枚举,设置方向:

public enum DIRECTION {
    DOWN, UP, LEFT, RIGHT;
}

最后的用法:

swipe(driver, DIRECTION.UP, 3);

希望这会有所帮助,

【讨论】:

    【解决方案2】:

    尝试使用这个,我不知道如何在 main 方法中调用它,因为有人给了我,所以如果你弄清楚了,请告诉我

    public void swipeRight() throws Exception {
    
        //The viewing size of the device
        Dimension size = driver.manage().window().getSize();
    
        //Starting x location set to 5% of the width (near left)
        int startx = (int) (size.width * 0.05);
        //Ending x location set to 95% of the width (near right)
        int endx = (int) (size.width * 0.95);
        //y position set to mid-screen vertically
        int starty = size.height / 2;
    
        scroll(startx, starty, endx, starty);
    }
    
    /**
     * This method does a swipe downwards
     * @author Bill Hileman
     * @throws Exception
     */
    public void scrollUp() throws Exception {
    
        //The viewing size of the device
        Dimension size = driver.manage().window().getSize();
    
        //Starting y location set to 20% of the height (near bottom)
        int starty = (int) (size.height * 0.20);
        //Ending y location set to 80% of the height (near top)
        int endy = (int) (size.height * 0.80);
        //x position set to mid-screen horizontally
        int startx = size.width / 2;
    
        scroll(startx, starty, startx, endy);
    }
    

    【讨论】:

      【解决方案3】:

      如果应用程序是混合的,并且您在网络环境中,您可以试试这个:

      driver.execute_script("arguments[0].scrollIntoView();", element)
      

      您将需要传递您知道位于应用顶部的任何元素,因此可能是应用标题。

      但是,如果应用程序在本机上下文中,那么 touchAction 是一个不错的选择,因为您已经实现了它。

      但是 appium 还提供了另一种解决方法,因为触摸操作在 iOS 中不起作用,那就是使用类似这样的移动界面:

      driver.execute_script("mobile: scroll", {"direction": "up"})
      

      【讨论】:

        猜你喜欢
        • 2019-12-24
        • 2016-05-16
        • 1970-01-01
        • 2019-01-03
        • 1970-01-01
        • 2018-01-01
        • 1970-01-01
        • 2015-01-23
        • 1970-01-01
        相关资源
        最近更新 更多