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

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Akbar
  • 68
  • 1
  • 10

2 Answers2

2

@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

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
  • Hi Eugene, Thanks for your inputs All the above samples pull notification bar Do I need to use press(x,y) or press(PointOption.point(x,y)) – Akbar Aug 30 '19 at 04:16
  • @Akbar, please try out ```press(PointOption pressOptions)``` e.g https://kobiton.com/book/chapter-11-automating-gestures/ => *touchAction.press(point(x,y))* – eugene.polschikov Aug 30 '19 at 10:18
  • @Akbar, please feel free to accept my answer if this works for You. Thankful in advance – eugene.polschikov Aug 30 '19 at 10:21
0

Try this maybe resolve your problem.

new TouchAction(driver)
            .longPress(start_x,start_y)
            .waitAction()
            .moveTo(PointOption.point(end_x,end_y)
            .release().perform();
    }
frianH
  • 7,295
  • 6
  • 20
  • 45
Ahmet
  • 1
  • 1