0

我正在尝试在 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);

通过这样做,代码正在拉动通知栏

4

2 回答 2

2

@akbar,从提供的经验示例中,您可以尝试一下吗

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

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

或者(方法#2) 考虑这个例子

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)

看看这个线程

Appium 版本 1.9.1 和客户端升级到 7.0.0刷卡使用以下代码(垂直刷卡。用于水平刷卡 - 从 'dimensions' 更改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();

这对我来说非常有效。希望这可以帮助,

最好的问候,尤金

于 2019-08-29T15:09:03.157 回答
0

试试这个也许可以解决你的问题。

new TouchAction(driver)
            .longPress(start_x,start_y)
            .waitAction()
            .moveTo(PointOption.point(end_x,end_y)
            .release().perform();
    }
于 2019-08-29T10:42:56.227 回答