appium中的水平滚动

时间:2018-05-28 08:48:09

标签: selenium automation appium appium-android

如何在appium中水平滚动 比如我们做的垂直滚动 driver.findElementByAndroidUIAutomator(" new UiScrollable(new UiSelector())。scrollIntoView("")");

有那样的???

4 个答案:

答案 0 :(得分:1)

您可以使用这种方式来水平滚动,也可以通过更改滚动开始和滚动结束来对垂直滚动使用相同的代码。

public void scrollHorizontal(double value1,double value2) {

Dimension dimension =driver.manage().window().getSize();
Double scrollheight = dimension.getHeight()*value1;
int scrollStart = scrollheight.intValue();

Double scrollheightEnd = dimension.getHeight()*value2;

int scrollEnd = scrollheightEnd.intValue();

new TouchAction((PerformsTouchActions)driver)
.press(PointOption.point(scrollStart,0))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
.moveTo(PointOption.point(scrollEnd , 0))
.release()
.perform(); }

答案 1 :(得分:0)

使用以下方法进行水平滚动:

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.ofMillis(duration)).moveTo(endPoint, anchor).release().perform();
    }

通过以下方式调用上述方法:

swipeHorizontal((AppiumDriver) driver,0.9,0.01,0.5,2000);

答案 2 :(得分:0)

这适用于所有方向:

枚举:

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(endX, startY)
                    .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, Util.DIRECTION.RIGHT);

希望这有帮助,

答案 3 :(得分:0)

您可以将UiSelector设置为可滚动并设置滚动方向

driver().findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true)."
                + "resourceId(\"<id of scrollable control>\"))"
                + ".setAsHorizontalList().scrollIntoView(new UiSelector().textContains(\"<text to search for>\"))"); 

链接提供有关UiScrollable

的详细信息