Mobile Automation Testing using APPIUM, Selenium Webdriver and Java

All things mentioned below is considering Windows 10.
Prerequisite
1)Download and Install JavaDownload here
2)Download and Install eclipseDownload here
3)Download and Install Node JSDownload here
4)Download and Install Android SDK(android-studio-ide-143.3101438-windows.exe) and keep it under C:\ Download here
5)Download and Install Selenium Webdriver(Selenium Standalone Server)Download here
6)Download and Install Appium Java Client Lib(jar)Download here
7)Download and Install APPIUM for Windows(Appium.exe for Windows)Download here

Setup Environment Variable:-
 User Variables:-
 Start->Control Panel\All Control Panel Items\System->Advance system setting->Environments variables->User Variables->New
  for Android:- Variable name: ANDROID_HOME and Variable value:C:\SDK\android-sdk-windows
  for APPIUM:- Variable name: APPIUM and Variable value:C:\Appium\node_modules\.bin
  for Java:- Variable name: JAVA_HOME and Variable value:C:\Program Files\Java\jdk1.8.0_101

 System Variables:-
 Start->Control Panel\All Control Panel Items\System->Advance system setting->Environments variables->system Variables->New
  for Android:- Variable name: ANDROID_HOME and Variable value:C:\SDK\android-sdk-windows
  for APPIUM:- Variable name: APPIUM and Variable value:C:\Appium\node_modules\.bin
  for Java:- Variable name: JAVA_HOME and Variable value:C:\Program Files (x86)\Java\jre1.8.0_101\
  for ADB:- Variable name: ADB_HOME and Variable value:C:\adb.exe

 Set Path:-
  C:\Program Files\nodejs\
  C:\Program Files (x86)\Java\jre1.8.0_101\bin
  C:\Program Files\nodejs\node_modules\.bin
  %APPIUM%
  C:\SDK\android-sdk-windows
  C:\SDK\android-sdk-windows\tools
  C:\SDK\android-sdk-windows\tools\bin
  C:\adb.exe

For TestNG:
  Start eclipse->Help->Install New Software->Works With
     Name:- TestNG
     Location:-Update site for release: http://beust.com/eclipse.
                         Or
     Update site for beta: http://testng.org/eclipse-beta
  Tick checkboxes and follow steps

Start First APPIUM Test for Android Native App
 1)Start Eclipse, Create new java project (File->New->Other->Java Project->Enter Project name
 2)Add Selenium and TestNG Jar( right click on project folder->build Path->Configure build path->Libraries->Add 
 external jars (browse your jar files form computer(java-client-3.2.0 and selenium-server-standalone-3.0.0-beta2 JAR files)
 3)Add TestNG JAR( right click on project folder->build Path->Configure build path->Libraries->Add Library ->select 
 TestNG Lib and follow steps.
 4)Create Java class file(Right Click on Project Folder->New->Class->Enter Package and Class name)
 5) Write following code for first Test Case for App Orientation check (Make Sure Twitter app should be installed into 
 Your Android Devices)
 Demo Code
        package example;

        import io.appium.java_client.android.AndroidDriver;
        import java.io.IOException;
        import java.net.MalformedURLException;
        import java.net.URL;
        import java.util.concurrent.TimeUnit;
        import org.apache.commons.exec.CommandLine;
        import org.apache.commons.exec.DefaultExecuteResultHandler;
        import org.apache.commons.exec.DefaultExecutor;
        import org.openqa.selenium.remote.DesiredCapabilities;
        import org.testng.Assert;
        import org.testng.annotations.AfterTest;
        import org.testng.annotations.BeforeTest;
        import org.testng.annotations.Test;
        
        public class CheckElementPresent 
        {
        AndroidDriver driver;

              @BeforeTest
              public void setUp() throws MalformedURLException 
              {
                   DesiredCapabilities capabilities = new DesiredCapabilities();
                   capabilities.setCapability("deviceName", "VPK");
                   capabilities.setCapability("browserName", "Android");
                   capabilities.setCapability("platformVersion", "5.2");
                   capabilities.setCapability("platformName", "Android");
                   capabilities.setCapability("appPackage", "com.twitter.android");
                   capabilities.setCapability("appActivity","com.twitter.android.LoginActivity");
                   driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
                   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
              }

              @Test
              public void performOrientation() throws InterruptedException 
              {
     
                   System.out.println("Current screen orientation Is : " + driver.getOrientation());
                   driver.rotate(org.openqa.selenium.ScreenOrientation.LANDSCAPE);
                   System.out.println("Now screen orientation Is : "+ driver.getOrientation());
                   Thread.sleep(5000);
                   System.out.println("Changing screen Orientation to PORTRAIT.");
                   driver.rotate(org.openqa.selenium.ScreenOrientation.PORTRAIT);
                   System.out.println("Now screen orientation Is : "+ driver.getOrientation());
                   Thread.sleep(5000);
              }
 
              @AfterTest
              public void End() throws IOException 
              {
  
                   driver.quit();  
  
              }
        }

  Note(to find App Activity and package name install APK Info appDownload here

How to run:
 1) Right click on code area->Run As-> TestNG Test (Before run it start your appium server and connect your device to 
 computer   via usb(make sure device connected))
 You will get result.(To view HTML result , just right click on project folder->refresh and view test-output 
 folder->open index.html file in browser.


For Appium Setting:-Launch Appium server
  Condition 1(If already present into device):
  
  1)Click Android icon->make sure all fields are unchecked 
  2)Click on setting icon-> make sure server address: 127.0.0.1 and Port:4730

  Condition 2(If apk file present into computer):

  1)Click Android icon->Add Applicatipn Path then we will get package and Activity name automatically
  2)Click on setting icon-> make sure server address: 127.0.0.1 and Port:4730

  Click on Launch Button before executes your test script


For Inspect app  Elements use UI Automator
  Path:-C:\SDK\android-sdk-windows\tools open uiautomatorviewer.bat (make sure adb installed properly and devices 
  should be connected to computer)

For Share Mobile screen on Computer. Just Open JAR Download here