繁体   English   中英

如何使用appium + Java在Android App中滚动?

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

我需要使用appium +硒+ 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

向上/向下滑动 :x轴坐标将保持不变,只有y坐标会改变。

如果您想了解更多有关坐标的信息,请从“ 开发人员选项 ”中打开“ 指针位置 ”设置,并手动观察坐标。

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

}

这是在各个方向上滑动的示例代码。 只需提供方向和滑动时间即可。

事实证明,代码是好的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM