How to perform drag and drop in android application using Appium?

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

 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();  
   
   }  
 }  
   

How to rotate android application screen portrait to landscape and landscape to portrait in Appium using java?

Android application testing, sometimes we need to check everything display and working fine in portrait and landscape orientation. In this tutorial, I am going to show how to rotate screen orientation from portrait to landscape and landscape to portrait using rotate() method of appium.

We will use getOrientation() method to get current screen orientation.
We will use rotate() method to change orientation from landscape to portrait and portrait to landscape.
driver.rotate(ScreenOrientation.PORTRAIT) will rotate screen orientation from landscape to portrait.

driver.rotate(ScreenOrientation.LANDSCAPE) will rotate screen orientation from portrait to landscape.
Download Drag-Sort Demos app from Google Play Store to perform screen orientation operation in android device and install device or Genymotion emulator.
Please visit below URL to download apk file from Google play Store

 import io.appium.java_client.android.AndroidDriver;  
 import java.io.File;  
 import java.io.InterruptedIOException;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.util.concurrent.TimeUnit;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.ScreenOrientation;  
 import org.openqa.selenium.remote.CapabilityType;  
 import org.openqa.selenium.remote.DesiredCapabilities;  
   
   
   
 public class AppiumScreenOrientation {  
   
   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);  
     driver.findElementById("com.mobeta.android.demodslv:id/activity_title").click();  
   
     // perform Screen Orientation  
   
     //Get and print current screen orientation.  
     System.out.println("Current screen orientation is : " + driver.getOrientation());  
   
     //Changing screen Orientation to LANDSCAPE.  
     driver.rotate(ScreenOrientation.PORTRAIT);  
   
     //Get and print screen orientation after changing It.  
     System.out.println("Now screen orientation is : "+ driver.getOrientation());  
     Thread.sleep(2000);  
   
     //Changing screen Orientation to PORTRAIT.  
     driver.rotate(ScreenOrientation.LANDSCAPE);  
     //Get and print screen orientation after changing It.  
     System.out.println("Now screen orientation is : "+ driver.getOrientation());  
     Thread.sleep(5000);  
   
     //Changing screen Orientation to LANDSCAPE.  
     driver.rotate(ScreenOrientation.PORTRAIT);  
     Thread.sleep(2000);  
   
     // Close application  
     driver.quit();  
   
   }  
 }  

How to work with an already installed application using appium?

Please browse to below url to see prerequisites for mobile test automation:

New android application run in appium:
Please visit below url to download apk file from Google play Store

The path to APK file:
Download desired android application file from Google Play Store and save the desired location on the computer. We create a file object which represents the actual apk file on the disk. I placed my apk file in the folder '/home/jannat/apk/'.

// Path of the android application file (apk)
File appDir = new File("/home/jannat/apk");
// path with android application name
File app = new File(appDir, "selendroid-test-app-0.17.0.apk");
// An absolute local path to the APK file
capabilities.setCapability(“app”, app.getAbsolutePath());

Example:

Handle already installed an android application using appium:
We need to provide android application package name and activity name which help to open already installed application.

Please browse to below url to find android application package name and activity name.

// Java package of the tested Android app
capabilities.setCapability("appPackage", "io.selendroid.testapp");
// An activity name for the Android activity you want to run from your package.
capabilities.setCapability("appActivity", "io.selendroid.testapp.HomeScreenActivity");

Demo java program for selendroid-test-app
 import io.appium.java_client.android.AndroidDriver;  
 import java.io.File;  
 import java.io.InterruptedIOException;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.util.concurrent.TimeUnit;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.remote.CapabilityType;  
 import org.openqa.selenium.remote.DesiredCapabilities;  
   
public class SimpleAppiumProgram {  
   
  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", "io.selendroid.testapp");

      // An activity name for the Android activity you want to run from your package.
     capabilities.setCapability("appActivity", "io.selendroid.testapp.HomeScreenActivity");

     driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);  
   
     driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);  
     Thread.sleep(5000);  
   
   
     driver.findElement(By.id("io.selendroid.testapp:id/my_text_field")).sendKeys("Hiro Mia");  
   
     driver.findElement(By.id("io.selendroid.testapp:id/touchTest")).click();  
     driver.quit();  
   
   
   
   }  
 }