Appium - Java, How to automate swipe in android?

2019-06-10 14:13发布

问题:

Trying to automate android app using appium, when entered the below code for swipe giving ----

TouchAction io.appium.java_client.TouchAction.press(WebElement el) 
@Deprecated
TouchAction ac = new TouchAction(driver);
ac.press(436,652).moveTo(-311,-14).release().perform();

What can be used to swipe?

回答1:

The use of coordinates or wait times has been deprecated. You're now supposed to use ActionOptions. In the case of points, they're called PointOptions.



回答2:

TouchAction works for me when giving it elements to start and stop the swiping at:

WebElement start = driver.findElement(By.id("xxxxxx"));
WebElement stop = driver.findElement(By.xpath("xxxxxx"));
TouchAction action = new TouchAction(driver);
action.longPress(start).moveTo(stop).release().perform();


回答3:

The waitAction in the following code is crucial to properly implementing a swipe (that took me a couple hours of investigation to learn).

public static void actionSwipeLeft(AndroidDriver driver)
{
    Dimension dims = driver.manage().window().getSize();
    int       x    = (int) (dims.getWidth() * .5);
    int       y    = (int) (dims.getHeight() * .8);

    int       endX = 0;

    System.out.println("Swiping left.");

    new TouchAction(driver)
            .press(new PointOption().withCoordinates(x, y))
            .waitAction(new WaitOptions().withDuration(Duration.ofMillis(500)))
            .moveTo(new PointOption().withCoordinates(endX, y))
            .release()
            .perform();
}