简体   繁体   中英

How to scroll up/down in appium-android

I have an app page where I need to scroll vertically to reach an element of the app.

I've googled and tried many solutions. Many commands are deprecated and don't support anymore in appium. Also, previous questions/answers in Stack didn't help me. I'm using appium v1.13.0 + Java (IntelliJ).

Sometimes I see this error:

java.lang.ClassCastException: io.appium.java_client.android.AndroidDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen

Anyway, my problem isn't just to solve the mentioned error. I'm looking for workable and correct commands to make a scroll action with appium. Please bring me the complete sample project cause I'm junior. Thanks

My Swiping method:

public void swipe(int startX, int startY, int endX, int endY, int msDuration) {
    TouchAction touchAction = new TouchAction(mDriver);
    touchAction.press(PointOption.point(startX, startY))
            .waitAction(WaitOptions.waitOptions(Duration.ofMillis(msDuration)))
            .moveTo(PointOption.point(endX, endY))
            .release();

    touchAction.perform();
}

Try this

String scrollViewContainer_finder = "new UiSelector().resourceIdMatches(\".*id/your_scroll_view_id\")";
String neededElement_finder = "new UiSelector().resourceIdMatches(\".*id/elemnt1\")";

WebElement abc = driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(" + scrollViewContainer_finder + ")" +
                ".scrollIntoView(" + neededElement_finder + ")"));

For swiping vertically/horizontally I'm using TouchAction:

TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
touchAction.press(startPoint)
           .waitAction(WaitOptions.waitOptions(Duration.ofMillis(waitBetweenSwipes)))
           .moveTo(endPoint)
           .release()
           .perform();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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