Download Drag-Sort Demos app from Google Play Store to perform drag and drop operation in android device and install device or Genymotion emulator.
Please visit below URL to download apk file from Google play Store
https://hiromia.blogspot.com/2017/02/how-to-download-android-apk-file-from.html
Before drag and drop
After drag and drop
Please visit below URL to download apk file from Google play Store
https://hiromia.blogspot.com/2017/02/how-to-download-android-apk-file-from.html
Before drag and drop
After drag and drop
import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class DrapandDrop {
private static AndroidDriver driver;
public static void main(String []arg) throws MalformedURLException, InterruptedException {
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
// Set android deviceName desired capability. Set genymotion emulator name.
capabilities.setCapability("deviceName", "192.168.56.101:5555");
// Set android platformName desired capability. It's Android in our case here.
capabilities.setCapability("platformName", "Android");
// Set android VERSION desired capability. Set genymotion emulator OS version.
capabilities.setCapability(CapabilityType.VERSION, "6.0.0");
// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
// Java package of the tested Android app
capabilities.setCapability("appPackage", "com.mobeta.android.demodslv");
// An activity name for the Android activity you want to run from your package.
capabilities.setCapability("appActivity", "com.mobeta.android.demodslv.Launcher");
driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Tap on first element.
driver.findElementById("com.mobeta.android.demodslv:id/activity_title").click();
//Locate 4th element from list to drag.
WebElement webElement1=(WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(3);
//Locate 6th element to drop dragged element.
WebElement webElement2=(WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(6);
//Perform drag and drop operation using TouchAction class.
//Created object of TouchAction class.
TouchAction action=new TouchAction((MobileDriver) driver);
//It will hold tap on 4th element and move to 7th position and then release tap.
action.longPress(webElement1).moveTo(webElement2).release().perform();
Thread.sleep(2000);
driver.quit();
}
}