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

How to locate Android Application elements using uiautomatorviewer in Ubuntu

UI Automator Viewer(UIautomatorviewer):
The user needs to Identifying Android application (AUT) objects for automating any android application using Appium. UIautomatorviewer such a GUI tool which provided by android SDK, to support automated, functional UI testing on android application. UIautomatorviewer provides a convenient visual interface for scanning and analyzing the UI components of an Android application. Users can inspect the UI of an android application in order to find out the hierarchy and view different properties (id, class,  text etc) of the element.

1. Connect your Android device with development machine(USB debugging mode should be enabled) or start emulator like Genymotion emulator
2. Open a terminal (Ctrl+Alt+T) and navigate to <android-sdk>/tools/
3. Run the tool with this command
./uiautomatorviewer
uiautomatorviewer window will show.
4. Open desired android application in your android phone or emulator
5. Click on the devices icon at the top right corner. It will start taking the UI XML snapshot of the screen currently opened in the device.
6. process to capture device screenshot.
7. After that, the snapshot of your device screen in the uiautomatorviewer window.
1. Left side: Android app's screenshot which is open in android device or emulator
2. Right side top part: Android app's UI element's hierarchy view
3. Right side bottom part: The property detail of selected element
8. Select button 9 to view it's different properties as shown in bellow image.

How to find Package Name And Launcher Activity Name from android APK file for Appium in Ubuntu

Android aapt stands for Android Asset Packaging Tool. This tool is part of the SDK (and build system) and allows you to view, create, and update Zip-compatible archives (zip, jar, apk). It can also compile resources into binary assets.
Android Asset Packaging Tool installation:
Open terminal (Ctrl+Alt+T) and run the below command to install aapt
sudo apt-get install aapt

aapt is a generic Android asset package tool that be used as:
"aapt list" - Listing contents of a ZIP, JAR or APK file.
"aapt dump" - Dumping specific information from an APK file.
"aapt package" - Packaging Android resources.
"aapt remove" - Removing files from a ZIP, JAR or APK file.
"aapt add" - Adding files to a ZIP, JAR or APK file.
"aapt crunch" - Crunching PNG files.
Please go to below url and follow mention steps to download an android apk file from Google Play Store
https://hiromia.blogspot.com/2017/02/how-to-download-android-apk-file-from.html

Find Package Name And Launcher Activity Name from android APK file:
Command: aapt dump badging <path-to-apk>
Example Command: aapt dump badging /home/jannat/Downloads/com.andatsoft.myapk.fwa.apk
OR
Find Package Name:
Command: aapt dump badging <path-to-apk> | grep package:\ name
Example Command: aapt dump badging /home/jannat/Downloads/com.andatsoft.myapk.fwa.apk | grep package:\ name
Find Launcher Activity Name:
Command: aapt dump badging <path-to-apk> | grep launchable-activity:\ name
Example Command: aapt dump badging /home/jannat/Downloads/com.andatsoft.myapk.fwa.apk | grep launchable-activity:\ name

How to download an android apk file from Google Play Store and install it in Genymotion android emulator?.

You can download android apk file from Google play Store using browser plugin and website. In this tutorial, I am going show, how to download android apk file from Google Play Store using a website

Download an android apk file from Google Play Store:
1. Go to the Play Store and find the app you want to download and copy the app's URL address from the browser's address bar.

2. Browse to  website like http://apps.evozi.com/apk-downloader/

3. Paste the app package name or the whole Google Play URL  in the box at the top of the page then Click on 'Generate Download Link' button


4. Click download icon button to download apk file to PC

Install an android apk file  in Genymotion android emulator:
1. Start Genymotion android emulator and go to apk file  director where downloaded
2. Drag downloaded apk file, drop on Genymotion emulator screen

3. Wait few moment to install in Genymotion android emulator

4. Application home screen  shows after successfully installed





How to run Appium Android automation test with IntelliJ Idea and Genymotion emulator using java in ubuntu

Please visit the url for install & configure appium with IntelliJ Idea and Genymotion emulator in ubuntu

Install Genymotion plugin in IntelliJ Idea
File --> Settings : Select 'plugins' from left sidebar, Enter 'genymotion' in search input then install

Set Genymotion path



Create a Appium Java project in IntelliJ Idea
1. Open IntelliJ Idea, File --> New --> Project
Select 'Java' from left sidebar, Set jdk path and Click 'Next' button
Click 'Next' button
Enter Project name and set desired Project location which wants to save project, finally, Click 'Finish' button

Add Appium Java Client and Selenium Standalone Server jar as external libraries
File --> Project Structure : Select 'libraries' from left sidebar. Click '+' to add Appium Java Client and Selenium Standalone Server jar as external libraries

After starting Genymotion emulator, run 'adb devices' command in terminal to get Genymotion emulator deviceName

Run 'appium' command in terminal to start Appium and also get ip:portnumer

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{  
   
       
     // apk file localtion  
     File appDir = new File("/home/jannat/apk");  
     File app = new File(appDir, "selendroid-test-app-0.17.0.apk");  
   
     //// 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");  
   
     //other caps  
     capabilities.setCapability("app", app.getAbsolutePath());      
     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();  
   
   
   
   }  
 }  
   
   

How to install & configure appium with IntellIj Idea and Genymotion emulator in ubunutu

1. Required software
  • Oracle java JDK
  • IntelliJ Idea
  • nodeJS
  • Android sdk tools
  • Appium
  • Oracle Virtualbox
  • Genymotion
  • Selenium Standalone Server jar
  • Appium java client jar 
  • apk file
Latest Oracle java JDK install and configure in ubuntu
1. Add the PPA.
Open terminal (Ctrl+Alt+T) and run the below command:
sudo add-apt-repository ppa:webupd8team/java
2. Update system package index and install Java installer
sudo apt update; sudo apt install oracle-java8-installer
3. Check the Java version
javac -version
4. Set Java environment variables
The PPA also contains a package to automatically set Java environment variables, just run the command:
sudo apt install oracle-java8-set-default

Intellij idea download and install in ubuntu
1. Please watch the video for installing IntelliJ Idea

Latest version nodeJS install
First, choose the Node.js version you need and add the sources for it
# for Node.js v4
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
# OR for Node.js v5
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
# OR for Node.js v6
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
# OR for Node.js v7
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

Then install the Node.js package.
sudo apt-get install -y nodejs 

Android sdk tools download, update the latest API version and configure Android Home variable
2. update the latest API  
Create a folder name like 'sdk' in your desired location like 'Home' and extract downloaded sdk tools under sdk folder,  then Open terminal (Ctrl+Alt+T) and navigate to tools folder
Type below command for executable permission
sudo chmod a+x android
run the command to update desired API version
./android
Select desired API version then click on 'Install packages'
Note: It will take time to update

3. Configure Android Home variable
Run the command
nano ~/.bashrc

Add Android Home variable at the end of line 
export ANDROID_HOME=/home/jannat/sdk
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools


Save Android Home variable
Press: Ctrl+O  then Enter
Type 'android' command on terminal: Android SDK Manager shows if properly configured

Appium installation
Run the below command in terminal to get appium
sudo npm install -g appium  
Run the below command in terminal to get appium client
sudo npm install wd 
Run the below command in terminal to start appium       
appium 


Oracle Virtualbox download and install
Run the below command to install
sudo dpkg -i virtualbox-5.1_5.1.14-112924-Ubuntu-yakkety_amd64.deb

If missing packages when install, Run the below command
sudo apt-get install libpng16-16 libqt5core5a libqt5widgets5 libsdl1.2debian libqt5x11extras5 libsdl-ttf2.0-0 

OR Please watch the video for installing VirtualBox using command
 

Genymotion download and install
After login in Genymotion site  then click to download

Run the command to give executable permission
 sudo chmod a+x genymotion-2.8.1_x64.bin 
Run the command to install Genymotion
 sudo ./genymotion-2.8.1_x64.bin 

Please watch the video for more information







How to create a Maven java project in IntelliJ IDEA

IntelliJ IDEA: is a Java integrated development environment (IDE) for developing computer software. It is developed by JetBrains (formerly known as IntelliJ), and is available as an Apache 2 Licensed community edition and in a proprietary commercial edition. Both can be used for commercial development
Prerequisite Software:
1. Jdk download and install

2. IntelliJ IDEA download and install
3. Gradle distribution download and extract desired location
Create a Maven Java project in IntelliJ IDEA:

1.  File --> New --> Project
       a. Select 'Maven' from left sidebar, Set jdk path and Click 'Next' button

       b. Enter GroupId and ArtifactId, then Click 'Next' button

      c. Type Project name and set desired Project location which want to save project, finally Click 'Finish' button
   d. Choose an option to open project
e. Maven project created and download necessary things
3. Add gradle dependencies
     a.  Click View --> Tools Windows --> Maven
     b. Click on 'pom.xml' file from left sidebar (1) 
     c. Add desired dependency under <dependencies</dependencies>  (2)
   <dependencies>  
     <dependency>  
       <groupId>io.rest-assured</groupId>  
       <artifactId>rest-assured</artifactId>  
       <version>3.0.1</version>  
       <scope>test</scope>  
     </dependency>  
   </dependencies>  

        added upper dependencies for REST Assured
Note: Two https://mvnrepository.com/ and http://search.maven.org/ site where you will desired maven dependency    
         d. Click refresh button (3)  to import jar files and Synchronizing Changes in Maven Project


4. Add java package or class 
   a. add main project code : src --> main --> java
  b. add code testing main project : src --> test --> java