appium - 如何在 Appium 中从左向右滑动?

标签 appium appium-android

由于 swipe() 已弃用,我无法从左向右滑动屏幕。我的应用程序中有 4 个横幅,我想滑动以查看所有横幅。

最佳答案

这适用于所有方向:

枚举:

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

实际代码:

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;

    }
}

用法:

swipe(driver,DIRECTION.RIGHT);

希望对您有所帮助,

关于appium - 如何在 Appium 中从左向右滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51215335/

相关文章:

java - 无法在真实的 Android 设备上运行任何 Appium 测试。参数错误 : BadParametersError: Parameters were incorrect

jasmine - Protractor Jasmine 空测试规范超时问题

selenium - 如何在 Appium 中使用现有的应用程序 session

java - Android 模拟器未启动。 browserName 总是抛出错误,但不包含在大写字母中

python - 如何使用适用于 Appium 的 Python 客户端在 iOS 应用程序中进行触摸和按住

java - 如何重构方法并添加带有 On 和 Off 值的枚举

java - 如何在appium-android中向上/向下滚动

android - Appium for Android 具有多个 Fragment

ios - 无法运行 Appium 服务器

java - Android 自动化错误显示 : "Security exception: Permission Denial: starting Intent"