Native android app automation using appium

To automate the native app in Android phone, you will have to start the android emulator and appium server. If you provide incorrect value for appActivity capability, You may see error saying "permission to start activity denied - are you using correct activity"

Example - Calculator Android app automation using Appium

Below example shows how to automate the calculator app in Android Emulator.

package org.softpost.android.simulator;
import org.junit.Assert;
import org.junit.Test;
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 org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sagar on 10-07-2016.
 */
public class NativeAppTest {
    WebDriver driver;
    @Test
    public void test1() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "android");

        caps.setCapability("deviceName", "Pixel 9");
        caps.setCapability("appPackage", "com.android.calculator2");
        caps.setCapability("appActivity",
        "com.android.calculator2.Calculator");
        caps.setCapability("automationName","uiautomator2");

try {
    driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


    driver.findElement(By.xpath("//android.widget.Button[@resource-id='com.android.calculator2:id/digit_2']")).click();

    driver.findElement(By.xpath("//*[@text='+']")).click();

    driver.findElement(By.xpath("//*[@text='6']")).click();

    driver.findElement(By.xpath("//*[@text='=']")).click();
    WebElement txt= driver.findElement(By.xpath("//*[@resource-id='com.android.calculator2:id/result']"));

    String p = txt.getText();
    String replacement = p.replaceAll("	|
|
", "");
    Assert.assertTrue("",replacement.equalsIgnoreCase("8"));

}catch (Exception ex) {
    ex.printStackTrace();


}finally {

    driver.quit();
}
    }

}

Complex problems, Simple Solutions