Setup and create first Appium Webdriver android script in Java

About Appium:

Appium is open source automation tool for iOS and android application supported by Sauce Lab. It is cross platform, supported Native, Hybrid iOS and android application. At backend it uses webdriver JSON wire protocol to communicate with iOS and android applications.
Appium supported many languages like Webdriver. You can write your test scripts any language that has selenium client library.
Appium support UIAutomator framework for ios and android library (For newer Api) and use selendroid for older android platform.
In this post I will show you how to setup and create Appium android script in java language.

Prerequisite:

1.JDK should be installed and java home path setup in your machine.
2.Android SDK should be installed on your machine and android path should be setup in your machine.
3.Appium for window download from : Click here
4.Selenium jar file from : Download
5.Eclipse.

Steps to create and run android test script:

1.Go to Appium downloaded package unzip and click on “Appium.exe”. Appium server console will be open like below:
2.You can change IP address and port, but keep it as a default.
3.Click on Launch button to run appium server you should see appium server running window like below
4.Launch emulator or connect device to the machine. Run adb command to check device is connected to your machine.
5.Below is sample code of webdriver in java for Appium in TestNg framework.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.test;
 
import java.io.File;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
 
public class AppiumTest {
  
    private WebDriver driver;
 
    @BeforeMethod
    public void setUp() throws Exception {
      
        // set up appium
        File appDir = new File("{pathApk file}");
        File app = new File(appDir, "{APK file Name}"); //my case “demo1.apk”
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("device","Android");
        capabilities.setCapability(CapabilityType.BROWSER_NAME,  "");
        capabilities.setCapability(CapabilityType.VERSION, "4.2");
        capabilities.setCapability(CapabilityType.PLATFORM, "WINDOW");
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("app-package", "{app package name}");  
        capabilities.setCapability("app-activity", ".{main activity class}");  //my case RootActivity
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        
    }
 
    @AfterMethod
    public void tearDown() throws Exception { 
        driver.quit();
    }
     
    @Test
    public void loginTest() throws InterruptedException
    {     
     List<webelement> textFieldsList = driver.findElements(By.className("android.widget.EditText")); 
        int size = textFieldsList.size();
 
        textFieldsList.get(0).sendKeys("Admin");
        textFieldsList.get(1).sendKeys("Admin");
        driver.findElements(By.xpath("android.widget.Button")).get(1).click();
        WebElement logout = driver.findElement(By.name("LOGOUT"));
        String logOut = logout.getText();
        System.out.println(logOut);
        logout.click();
    }
     
    @Test
    public void formTest() throws InterruptedException
    {     
     List<webelement> textFieldsList = driver.findElements(By.className("android.widget.RelativeLayout")); 
        textFieldsList.get(2).click();
         
        driver.findElement(By.className("android.widget.Spinner")).click();        
        List<webelement> listsDropDown = driver.findElements(By.className("android.widget.CheckedTextView")); 
        for(WebElement listdropdown: listsDropDown){
         String list = listdropdown.getText();        
         if(list.equals("Oxygen")){
          listdropdown.click();
         }
        }
         
        //click on check box
        WebElement checkBox = driver.findElement(By.className("android.widget.CheckBox"));        
        if(checkBox.isSelected()){
         checkBox.click();
        }
         
        //radio button 
        driver.findElements(By.className("android.widget.RadioButton")).get(1).click();      
        
    }      
}