Delivered by FeedBurner

Appium Tutorial - Perform Drag And Drop In Android App

Drag And Drop Is one of the common action of any android app. You will see many android mobile apps where you can drag and drop element from one place to other place. If you are automating android mobile app where you have to perform drag and drop then you need to use TouchAction class. TouchAction class provides us facility to automate mobile gestures of android app using appium. Let's take very simple example to perform drag and drop operation on android application using appium.

PREREQUISITES : Previous 18 steps (Listed In PART 1 and PART 2) of appium tutorials should be completed.

Download Drag-Sort Demos app
We will use Drag-Sort Demos app to perform drag and drop operation in android device. You can download it from Google Play Store or THIS PAGE.

Create New Folder Under Project Folder To Store APK File
You need to create new folder "Apps" under your project folder and put downloaded Drag-Sort Demos's .apk file inside it.

To create new folder under project in eclipse,
  • Right click on project(MavenProject1 for me) folder -> New -> Folder.
  • Give folder name = Apps and click on Finish button. It will add Apps folder under project folder.
  • Copy-paste downloaded Drag-Sort Demos apk file inside it as shown in bellow image.

Drag-Sort Demos apk
Aim Of This Appium Android Test
Our appium automation test script will,
  • Install Drag Sort Demos app in android mobile device automatically.
  • Then It will launch Drag Sort Demos app in your android mobile device.
  • It will tap on "Basic usage playground" text.
Drag-Sort Demos app
  • Then It will locate and drag 3rd element and drop it to 6th position as shown in bellow images.
Before drag and drop
appium android - perform drag and drop

After drag and drop
appium - drag and drop element

This is our aim to achieve from bellow given test script.

Create And Run Test
Now you already knows what we wants to do In this test. Let's create and run test in eclipse.

  1. Android mobile device should be connected with PC with USB debugging mode enabled. View THIS POST.
  2. Drag-Sort Demos app should be there in Apps folder as described above.
  3. There should be enough free space in your mobile device to install Drag-Sort Demos app.
  4. appium node server should be launched and running mode. View THIS POST.
  5. TestNG should be installed in eclipse. View THIS POST.
Now create new class file under package(for me, package name is Android) of your project and copy paste bellow given test script in it.

Note : Please replace deviceName and platformVersion as per you actual in bellow given test.
package Android;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DragAndDropAction {
 //Object reference of AndroidDriver.
 AndroidDriver driver;

 public void setUp() throws MalformedURLException {
  //Set Drag-Sort Demos app folder path. This statement will refer project's folder path.
  File classpathRoot = new File(System.getProperty("user.dir"));
  //Set folder name "Apps" where .apk file is stored.
  File appDir = new File(classpathRoot, "/Apps");
  //Set Drag-Sort Demos .apk file name.  
  File app = new File(appDir, "");  
  // Created object of DesiredCapabilities class.
  DesiredCapabilities capabilities = new DesiredCapabilities();
  // Set android deviceName desired capability. Set your device name.
  capabilities.setCapability("deviceName", "ZX1B32FFXF");

  // Set BROWSER_NAME desired capability. It's Android in our case here.
  capabilities.setCapability("browserName", "Android");

  // Set android VERSION desired capability. Set your mobile device's OS version.
  capabilities.setCapability("platformVersion", "4.4.2");

  // Set android platformName desired capability. It's Android in our case here.
  capabilities.setCapability("platformName", "Android");
  //Set .apk file's path capabilities.
  capabilities.setCapability("app", app.getAbsolutePath());

  // Set app Package desired capability of Drag-Sort Demos app.
  capabilities.setCapability("appPackage", "");

  // Set app Activity desired capability of Drag-Sort Demos app.
  capabilities.setCapability("appActivity", "");

  // Created object of AndroidDriver and set capabilities.
  // Set appium server address and port number in URL string.
  // It will launch Drag-Sort Demos app in emulator.
  driver = new AndroidDriver(new URL(""), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

 public void dragDrop() {
  //Tap on Basic usage Playground.
  driver.findElementByName("Basic usage playground").click();

  //Locate 3rd element(Chick Corea) from list to drag.
  WebElement ele1 = (WebElement) driver.findElementsById("").get(2);
  //Locate 6th element to drop dragged element.
  WebElement ele2 = (WebElement) driver.findElementsById("").get(5);

  //Perform drag and drop operation using TouchAction class.
  //Created object of TouchAction class.
  TouchAction action = new TouchAction((MobileDriver) driver);
  System.out.println("It Is dragging element.");
  //It will hold tap on 3rd element and move to 6th position and then release tap.
  System.out.println("Element has been droped at destination successfully.");

 public void End() {
  // Quit

Comments are already there with each sentence of test script. Let me describe you few Important class and methods which are used in above test script.
  • TouchAction : This is class of Webdriver 3 which provide different methods(longPress, moveTo, perform, press, tap, etc..) to automate mobile gestures like drag and drop, swipe etc. It will help us to generate action chain of different actions.
  • longPress : longPress is method to hold tap for long time on given element.
  • moveTo : moveTo is method to move action.
  • release : release is used to release from longPress tap action.
  • perform : perform will execute full action chain of drag and drop.
Also If you notice in above test script, We have used AndroidDriver at place of RemoteWebDriver as we are automating android app. And also used findElementByName("") and findElementsById("") methods at place of findElement(By.Name("")) and findElements(By.Id("")) methods.

Run above test using testng and observe Drag And Drop in your mobile device. 
  • It will launch Drag-Sort Demos app.
  • Tap on "Basic usage playground" text and
  • Perform draga and drop operation as shown in above Images.
This way we can automate mobile gestures in appium android automation test. Same thing you can do with any app.


  1. This comment has been removed by the author.

  2. How can I download Selenium Webdriver 3

  3. Getting error on AndroidDriver driver;, saying change to AndroidApp/AndroidDriverAPKBuilder/AndroidSDK

  4. Hi, Aravind. Thank you very much, I am here now, and learnt lots from you.

    here I have a problem. My eclipse gave "AndroidDriver driver;" a say" AndroidDriver is a raw type. References to generic type AndroidDrivershould be parameterized.

    I don't what I have done wrong?


    1. Hi Mak,
      I run into the same issue, and can you share how you solve it with me?

  5. Hi, Avavind.
    I have solved my problem. I really enjoy finding and solving problem, and go forward following you.
    thank you very much.

    Today, when I use the way try to open a music. I did it, open music, went to the album, chose a song. it singed,
    but I don't know how to close it, and go to previous page.

    I am looking for your teaching us this techniques and others.


  6. Hello Aravind ... Truly I have become a big fan of yours ...!!! No words to explain ..!!!
    Gestures .. This topic u have made it as a cake walk ....

    Thanks a ton

  7. Hello Aravind....i want to drag 2nd index element to 9th index getting java.lang.IndexOutOfBoundsException: Index: 9, Size: 8 ....please help me

  8. Hello Aravind....i want to drag 2nd index element to 9th index getting java.lang.IndexOutOfBoundsException: Index: 9, Size: 8 ....please help me

    1. I guess you have to scroll down and then click on 9th element else it will give you exception. Selenium will count only visible elements in the screen, since 9th element is not visible, it will give exception.