Testing Android apps on Emulators (Virtual devices) using Appium

Appium can be used to test the android applications running on Emulators. You need to follow below steps to create the emulator in Windows.

  1. Download and install Android SDK
  2. Create AVD – Android Virtual Device using AVD manager and Start it
  3. Start the Appium server
  4. From the code, you can pass the name of the AVD device in deviceName capability.

Here is the sample code to launch the calculator app on Emulator.

package nativeapptests;

import io.appium.java_client.android.AndroidDriver;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

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

public class LaunchCalculatorOnEmulator
{
    private AndroidDriver driver;

    @Test
    public void loadingSinglePageTest() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "4.4.4");
        capabilities.setCapability("deviceName", "AVD1");
        capabilities.setCapability("browser_Name", "Android");
        capabilities.setCapability("appPackage", "com.android.calculator2");
        capabilities.setCapability("appActivity","com.android.calculator2.Calculator");
        //capabilities.setCapability("browserName", "Chrome");

        driver = new AndroidDriver(new URL("https://127.0.0.1:4723/wd/hub"), capabilities) {
        };
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.name("2")).click();

        driver.findElement(By.name("+")).click();

        driver.findElement(By.name("6")).click();

        driver.findElement(By.name("=")).click();
        WebElement txt= driver.findElement(By.className("android.widget.EditText"));

        String p = txt.getText();
        String replacement = p.replaceAll("\t|\n|\r", "");
        Assert.assertTrue("",replacement.equalsIgnoreCase("2+6=8"));

        driver.quit();
        //quit the browser
    }
}

You may also like...