Swipe from Right to Left

Hi!
Please help , I’m new to testing.
How to do swipe gestures in Android using Java?
This code does not work

` @Test public void testSlider(){ OpenMainMenu(); WebElement slider = driver.findElement(By.className("android.widget.ImageButton")); Point sliderLocation = getCenter(slider); driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 3000); } private Point getCenter(WebElement element) { Point upperLeft = element.getLocation(); Dimension dimensions = element.getSize(); return new Point(upperLeft.getX() + dimensions.getWidth()/2, upperLeft.getY() + dimensions.getHeight()/2); } } `

Hi @Lera_Leschenko

The code looks good, But I Guess there is some problem is getting the exact element locator , also do not manipulate MobileElement co-ordinate value.

In Android apps many elements can have same class name as “android.widget.ImageButton” and appium returns the MobileElement based on index basis, So probably you are not getting the exact MobileElement and hence swipe is not working properly.Try to narrow down your locator strategy so that you will get your desired MobileElement.

For swiping horizontally, capture the elements on both ends and user driver.swipe() method, and for vertical swipe capture top and bottom element and manipulate the co-ordinates in driver.swipe() method.

Let me know if this works for you.

Thanks
Ravi Kumar

  1. log your actual numbers of start_x, start_y, end_x and end_y.
  2. look at appium logs (start appium with debug key)
  3. enable on phone in developer options Show touches + Show Show touch data. you will see where exactly swipe happens. try to do same manually (it could be problems when swipe actions starts/end on elements that consume touches e.g. button and swipe does not happen. in such case change swipe coordinates.)

you trying to swipe left?

You can swipe with it .
int Xfirst = findElementbyXpath("").getLocation().getX();
int Xfirstr=Xfirst + Xfirst;
int Yfirst= findElementbyXpath("").getLocation().getY();
driver.swipe(Xfirst + 300,Yfirst, Xfirst,Yfirst,5000);

Hi @ravikr42

I have now turned this code , the action took place on Android, but the test failed.

@Test public void SwipeLeftoRight() throws InterruptedException { OpenMainMenu(); size = driver.manage().window().getSize(); System.out.println(size); int star_x = (int) (size.width * 0.90); int end_x = (int) (size.width * 0.09); int star_y = size.height / 2; System.out.println("star_x = " + star_x + " ,end_x = " + end_x + " , star_y = " + star_y); //Right to left driver.swipe(star_x, star_y, end_x, star_y,500); }}

That error
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)

Test falls after the last line of code.

print little bit more log before and after

Hi,

Here is detailed video on how you can swipe from left to right, right to left, from top to bottom and from bottom to top using Appium.

Thanks,
Anuja

1 Like

Hi anuja,

Your youtube videos are great and helpfull but im not able to find a solution to horizontal swipe in java client 5.0.4 using touch actions could u post a generic code its really urgent