Test Android native app with Appium – quick tutorial example

In this post we will show how to create automatic test for native application in Android.
We will show it in simple test case (only swipe to second fragment in FragmentActivity)

Tests run thanks to Appium (link)

1. Download Appium and install it
2. Open folder that contains installed appium and appium.exe file
3. Start appium.exe

In our simple test class we will use following libraries:
java-client-2.2.0.jar
selendroid-standalone-0.16.0-with-dependencies.jar

appium

4. Click on triangle in top-right of window.
Now, you can see some logs in appium console window.
5. Make sure, that your emulator (I tried it on emulator only) is running.

Ok now we can create separate class with our test case. For simplicty we use only class with main method and we will not consider any test frameworks like testNG or jUnit.

So create new class. In this case class name is “MyClass”

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
 
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
 
import io.appium.java_client.android.AndroidDriver;
 
 
public class MyClass {
 
    static AndroidDriver driver;
 
    public static void main(String... args) {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("device", "Android");
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability(CapabilityType.VERSION, "4.x");
        capabilities.setCapability(CapabilityType.PLATFORM, "Windows");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "5_PALCOVy");
 
        try {
            driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
 
            driver.context("NATIVE_APP");
 
            SwipeHelper.swipeFromRightToLeft(driver);
            Thread.sleep(1500);
            SwipeHelper.swipeFromRightToLeft(driver);
            Thread.sleep(1500);
            driver.findElement(By.id("net.demcak:id/hostTxtView")).clear();
            Thread.sleep(1500);
            driver.findElement(By.id("net.demcak:id/authButton2")).click();
            Thread.sleep(1500);
        } catch (Exception e) {
            driver.quit();
        }
    }
 
    public static List<WebElement> getWebElementList(By by) {
 
        return driver.findElements(by);
    }
}

I have created and used small helper class which contains method for swipe from right to left

public class SwipeHelper {
 
    public static void swipeFromRightToLeft(AndroidDriver driver){
        Dimension size = driver.manage().window().getSize();
        int startX = (int)(size.width * 0.80);
        int endX = (int)(size.width * 0.10);
        int startY = size.height/2;
        driver.swipe(startX, startY, endX, startY, 500);
    }
}

UI automator viewer
You can use uiautomatoviewer for inspecting ui elements in Android layout.