如何在Android appium中向上滚动

时间:2018-06-25 07:15:03

标签: java android selenium automation appium

我有一个应用程序页面,我需要在其中垂直滚动以到达应用程序底部的“保存”按钮。
我正在尝试下面的代码,但出现服务器端错误。

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

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

3 个答案:

答案 0 :(得分:0)

如果该应用程序是混合应用程序,并且您处于网络环境中,则可以尝试以下操作:

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

您将需要传递任何您知道在应用程序顶部的元素,因此可能会传递应用程序标头。

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

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

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

答案 1 :(得分:0)

以下是在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(endX, startY)
                    .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 :(得分:0)

尝试使用它,因为有人给我,我不知道如何在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);
}