如何使用 appium+Java 在 Android 应用程序中滚动?

How to scroll in the Android App using appium+Java?

我需要使用 appium + selenium + java 垂直(上下)和水平(左右)滚动。任何人都可以帮我解释一下所需的代码片段,以便我可以在其他项目中进一步使用它。

从右向左滑动 使用下面的代码

Dimension size = driver.manage().window().getSize();            
int startx = (int) (size.width * 0.8);          
int endx = (int) (size.width * 0.20);       
int starty = size.height / 2;   
driver.swipe(startx, starty, endx, starty, 1000);

从左到右 :方向只需将 start-x 更改为 end-x 并将 end-x 更改为 startx

滑动up/down:x-axis co-ordinates将保持不变,只有y-coordinates会改变。

如果您想了解更多关于 co-ordinates 的信息,请打开“开发人员选项”中的“指针位置”设置" 并手动观察 co-ordinates。

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);
            break;

        case LEFT:
            startY = (int) (size.height /2);
            startX = (int) (size.width * 0.05);
            endX = (int) (size.width * 0.90);
            break;

        case UP:
            endY= (int) (size.height * 0.70);
            startY  = (int) (size.height * 0.30);
            startX = (size.width / 2);
            break;


        case DOWN:
            startY = (int) (size.height * 0.70);
            endY = (int) (size.height * 0.30);
            startX = (size.width / 2);

            break;

    }

    new TouchAction(driver)
            .press(startX, startY)
            .waitAction(Duration.ofMillis(duration))
            .moveTo(endX, startY)
            .release()
            .perform();

}

这里是向各个方向滑动的示例代码。只需提供滑动的方向和持续时间。

代码被证明是好的。