How to swipe across screens for Android in Appium?

I want to swipe across screens with Appium in Android. The swiping is across and I want to traverse each screen and perform actions on it. Any help would be appreciated!

There are several ways to swipe in Appium. Here is the method we wrote in Ruby to make the call a bit simpler:

def swipe(start_x, start_y, end_x, end_y)
  action = Appium::TouchAction.new.press(x: start_x, y: start_y).wait(10).move_to(x:end_x, y:end_y).wait(50).release(x: end_x, y: end_y)
  action.perform
end

You’ll need to determine start and end values for x and y. In the example below, we want to swipe across a tab element in the Android settings.

  tab = appium.driver.find_element(:id, 'com.android.settings:id/tabs')
  dimensions = apm.appium_module.window_size
  max_x = dimensions.width - 1
  swipe(max_x, tab.location.y + 5, tab.location.x, tab.location.y + 5)
2 Likes

Hi Will…thanks for your update on the same…but I need a Java code for the issue.

  1. Swiping from top to bottom (Tried the conventional x,y coordinate approach but dint work)
  2. Swiping across the screens.

Help is much appreciated.

You can look at the source for the java implementation at https://github.com/appium/java-client/blob/master/src/main/java/io/appium/java_client/TouchAction.java. here are some of the pertinent lines from the Java TouchAction Header:

  • The flow is to chain individual touch actions into an entire gesture. e.g.
  • TouchAction action = new TouchAction(driver);
  • action.press(element).waitAction(300).moveTo(element1).release().perform();

Thanks Will for the update on the answer. Things started to work with Scrollto and swipe functions with the new Java Client library

Better approach, less verbose is the following

Appium::TouchAction.new.swipe({start_x: 0.8, start_y: 0.5, end_x:0.2, end_y: 0.5, duration: 500}).perform
1 Like