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 appium 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 Appium 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 to perform drag and drop in appium

We will use Drag-Sort Demos app to perform drag and drop Appium 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 Drag and Drop Appium Test

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

PREREQUISITES :
  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 DragAndDropAction.java 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.

DragAndDrop Appium Test

package Android;

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

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
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;

 @BeforeTest
 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, "com.mobeta.android.demodslv-0.5.0-3_APKdot.com.apk");  
              
  // 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", "com.mobeta.android.demodslv");

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

  // 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("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 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("com.mobeta.android.demodslv:id/drag_handle").get(2);
  //Locate 6th element to drop dragged element.
  WebElement ele2 = (WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").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.
  action.longPress(ele1).moveTo(ele2).release().perform();  
  System.out.println("Element has been droped at destination successfully.");
 }

 @AfterTest
 public void End() {
  // Quit
  driver.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 to perform drag and drop in appium. Same thing you can do with any app.

16 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. How can I download Selenium Webdriver 3

    ReplyDelete
  3. Wher can I download Webdriver 3

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

    ReplyDelete
  5. 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?

    Thanks
    Mak

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

      Delete
    2. They are just warnings, sometime script works luckily even some warnings. here we are blessed.
      such warnings make me anxious, but I can do nothing.
      I think you have already known it. I happen to go back here.
      cheer
      Mak

      Delete
  6. 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.
    Thank

    Mak

    ReplyDelete
  7. 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

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

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

    ReplyDelete
    Replies
    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.

      Delete
  10. It worked for me. That is awesome!

    ReplyDelete
  11. Hello Aravind, I have a question:
    how do we know the id is drag_handle. my uiautomatorviewer shows android:id/list

    ReplyDelete
  12. TouchActions is throwing below exception. Please help to solve this.

    INFO: Detected dialect: W3C
    Exception in thread "main" java.lang.ClassCastException: io.appium.java_client.android.AndroidDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen
    at org.openqa.selenium.interactions.touch.TouchActions.(TouchActions.java:38)
    at amazon.StartApplication.main(StartApplication.java:69)

    ReplyDelete