Swipe Gesture – Appium

You already know how to Scroll using Android driver. Now let’s see how to Swipe. Generally while swiping on a device, we move our finger to some X&Y coordinates. We can use TouchAction class to swipe here and there. Also we can use Android Driver function ‘swipe‘ by using screen width and height and we can also use javascript executor to swipe. Here I’m not using any app for demonstration.
Let’s quickly take a look at the code:

package com.training;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Swipe {
AndroidDriver driver;
public void setUp() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, “4.4.2”);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, “Your Device Name”);
caps.setCapability(MobileCapabilityType.APP_PACKAGE,”Your Package Name”);
caps.setCapability(MobileCapabilityType.APP_ACTIVITY,”Your App activity Name”);
driver = new AndroidDriver(new URL(“”),caps);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
public void swipeUsingTouch() {
TouchAction myAction = new TouchAction(driver);
//mention the X,Y offset and start and end points
myAction.press(200, 500).moveTo(700, 500).release().perform();
//you need to release once the gesture has been made
public void swipeUsingAndroidFunction() {
Dimension size = driver.manage().window().getSize();
int startx = (int) (size.width * 0.70);
int starty = size.height;
int endy = size.height / 2;
int endx = (int) (size.width * 0.30);
driver.swipe(startx, starty, endx, endy, 2000);
public void swipeUsingJavascript() {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> swipeElement = new HashMap<String, Double>();
swipeElement.put(“startX”, 0.01);
swipeElement.put(“startY”, 0.3);
swipeElement.put(“endX”, 0.7);
swipeElement.put(“endY”, 0.6);
swipeElement.put(“duration”, 4.0);
js.executeScript(“mobile: swipe”, swipeElement);
public void tearDown() {

If you’re wondering what is JavascriptExecutor and HashMap, then this is for you.
JavascriptExecutor is a class which is used to execute javascript functions in our script. Click here to know how to use. And The HashMap in Java is one of the most popular Collection class. HashMap maintains key and value pairs and often denoted as HashMap<Key, Value> or HashMap<K, V>. HashMap implements Map interface. HashMap is similar to Hashtable with two exceptions – HashMap methods are unsynchornized and it allows null key and null values.
There are few methods inbuilt in the HashMap class:
Set keySet(): It returns the Set of the keys fetched from the map.
value put(Key k, Value v): Inserts key value mapping into the map.
int size(): Returns the size of the map – Number of key-value mappings.
Collection values(): It returns a collection of values of map.
Value remove(Object key): It removes the key-value pair for the specified key.
void putAll(Map m): Copies all the elements of a map to the another specified map.

That’s how you can perform Swipe with different methods based on your need. If you’re not familiar with Desired capabilities then click here.


2 thoughts on “Swipe Gesture – Appium

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s