Search code examples
androidappiumjava-client

Horizontal swipe using co-ordinates in appium java client 7.0.0


I am trying to swipe horizontally in appium android - java-client 7.0.0. It doesn't work

//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();
}

Called as below in my test. horizontalSwipeByPercentages(0.2,0.5,0.5);

By doing this, code is pulling notification bar


Solution

  • @akbar, from experitest example provided could You please try out

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

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

    or alternatively (approach #2) considering 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();
    }
    

    And calling it with the value from your topic would be:

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

    Notice the comments about absolute/relative values

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

    And another approach (approach #3)

    Take a look into this one thread

    Appium of version 1.9.1 and client upgrading to 7.0.0 swipe worked with the following code (vertical swipe. for horizontal swipe- change getHeight() to getWidth() from 'dimensions' ):

    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();
    

    That worked perfectly for me. Hope this helps,

    Best regards, Eugene