在Appium Java Client 7.0.0中使用坐标进行水平滑动

时间:2019-08-29 10:33:16

标签: android appium java-client

我正在尝试在appium android-Java-client 7.0.0中水平滑动。它不起作用

//Horizontal Swipe by percentages
public void horizontalSwipeByPercentages(double startPercentage, double endPercentage, double anchorPercentage) {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.height * anchorPercentage);
    int startPoint = (int) (size.width * startPercentage);
    int endPoint = (int) (size.width * endPercentage);

    new TouchAction(driver)
        .press(PointOption.point(endPoint, startPoint))
        .waitAction(new WaitOptions().withDuration(Duration.ofMillis(600)))
        .moveTo(PointOption.point(startPoint, endPoint))
        .release().perform();
}

在我的测试中如下调用。 horizo​​ntalSwipeByPercentages(0.2,0.5,0.5);

这样做,代码将拉动通知栏

2 个答案:

答案 0 :(得分:2)

@akbar,来自experitest example provided,请您尝试一下

TouchAction ta = new TouchAction(driver);
ta.press(434, 468).waitAction(Duration.ofMillis(1000)).moveTo(471, 312).release().perform();

===================================

或者(方法2) 考虑this example

public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.height * anchorPercentage);
    int startPoint = (int) (size.width * startPercentage);
    int endPoint = (int) (size.width * finalPercentage);
    new TouchAction(driver).press(startPoint, anchor)
    .waitAction(duration)
    .moveTo(endPoint, anchor)
    .release()
    .perform();

    //In documentation they mention moveTo coordinates are relative to initial ones, but thats not happening. When it does we need to use the function below
    //new TouchAction(driver).press(startPoint, anchor).waitAction(duration).moveTo(endPoint-startPoint,0).release().perform();
}

使用您主题中的值进行调用将是:

swipeHorizontal(driver,0.9,0.01,0.5,3000);

注意有关绝对/相对值的评论

==================================

另一种方法(方法3)

看看this one thread

Appium 1.9.1版本和客户端升级到7.0.0 可以使用以下代码(垂直滑动。水平滑动-将getHeight()更改为getWidth() '尺寸'):

TouchAction action = new TouchAction(driver);
PointOption p1= new PointOption();
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int h1 = screenHeightStart.intValue();
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int h2 = screenHeightEnd.intValue();
action.press(PointOption.point(0,h1))
.waitAction(new WaitOptions().withDuration(Duration.ofMillis(600)))
.moveTo(PointOption.point(0, h2))
.release()
.perform();

对我来说效果很好。 希望这会有所帮助,

最好的问候, 尤金

答案 1 :(得分:0)

尝试此操作可能会解决您的问题。

new TouchAction(driver)
            .longPress(start_x,start_y)
            .waitAction()
            .moveTo(PointOption.point(end_x,end_y)
            .release().perform();
    }