Drag and drop action in Android Emulator using Appium

Some important points

  1. TouchAction and MultiTouchAction class can be used to do drag and drop operation
  2. press, waitAction and moveTo methods should be called in sequence to perform drag and drop
  3. We can find the Point of Source and Destination element using getLocation method
  4. Finally perform method of MultiTouchAction class can be used to perform drag and drop action
  5. Swiping specific element is same as drag and drop operation. Only difference is that we do not have destination element. In this case, we can use destination point.
  6. In same way, we can do this action in iOS as well

Below hypothetical code demonstrates how you can perform drag and drop action in Android Emulator. Here we are assumming that battery element can be dragged to sound element. You need to replace these elements with the ones in your app.
package org.softpost.android.simulator;

import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

 * Created by Sagar on 10-07-2016.
public class DragDropTest {
    WebDriver driver;

    public void test1() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "android");

        caps.setCapability("deviceName", "Pixel 9");
        caps.setCapability("appPackage", "com.android.settings");

    try {
        driver = new AndroidDriver(new URL(""), caps);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


        WebElement battery = driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'Battery')]"));
        WebElement sound = driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'Sound')]"));

        TouchAction action = new TouchAction(((AndroidDriver)driver));

        //build drag and drop action...Dragging Battery element on Sound element
        action.press(new PointOption().point(battery.getLocation().getX(),battery.getLocation().getY()))
                .waitAction(new WaitOptions().withDuration(Duration.ofSeconds(1)))
                .moveTo(new PointOption().point(sound.getLocation().getX(),battery.getLocation().getY())).release();

        //Perform drag and drag action
        MultiTouchAction multiAction = new MultiTouchAction(((AndroidDriver)driver));

    }catch (Exception ex) {

    }finally {


    protected boolean WaitUntilElementIsDisplayed(By by, int seconds)
            WebDriverWait wait = new WebDriverWait(driver, seconds);
            return true;
        catch (Exception ex)
            System.out.println("Exception while waiting {ex.Message}" + ex.getMessage());
            return false;


Youtube | Facebook | Twitter | Website Home | Online Grocery Shopping Brisbane