Execute Appium Tests on Real Device - Android [Native App]

In the previous tutorials, we have seen executing appium tests on a real device by using chrome browser (Mobile Web App) . Now in this tutorial we will take a very basic native app and execute Appium tests on a real device for that app.

Let us take Calculator App which will be readily available on most of the android devices, just to try to execute simple program of adding of two numbers. Here we will use 'By.name()' locator for now and later we will discuss different element locating strategies and how to get element locators using UIAutomatorViewer which is a tool provided by android SDK. It is mainly used to locate and get properties details of an selected element in android native app.

Execute Appium on Native App Calculator

In any android native app, locating element by Name is very simple, If any element contains unique text 'text='something', we can simply locate it as By.Name("something"). Here for calculator app, numbers and function symbols are unique so, we can simply proceed using 'By.Name()'. As we see in the above screenshot, text value is unique.

Let us now create a class 'AndroidCalcAppExample.java' and add required desired capabilities which are explained along with defined capabilities in the below code.

package com.appium.adnroid;

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

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 org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AndroidCalcAppExample {
	WebDriver driver;

	@BeforeTest
	public void setUp() throws MalformedURLException {
		
		//Created object of DesiredCapabilities class
		DesiredCapabilities capabilities = new DesiredCapabilities();
		
		//The kind of mobile device or emulator to use - iPad Simulator, iPhone Retina 4-inch, Android Emulator, Galaxy S4 etc
		//Find your device name by running command 'adb devices' from command prompt
		capabilities.setCapability("deviceName", "WKZLMJLB7L8TR8SW");
	
		//Which mobile OS platform to use - iOS, Android, or FirefoxOS
		capabilities.setCapability("platformName", "Android");
		
		//Java package of the Android app you want to run- Ex: com.example.android.myApp
		//For Android calculator app, package name is 'com.android.calculator2'
		capabilities.setCapability("appPackage", "com.android.calculator2");

		//Activity name for the Android activity you want to launch from your package
		//For Android calculator app, Activity name is 'com.android.calculator2.Calculator'
		capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
		
		// Initialize the driver object with the URL to Appium Server and pass capabilities
		driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
	}

	@Test
	public void Sum() {

		System.out.println("Calculate sum of two numbers");
		//Locate elements using By.name() to enter data and click +/= buttons
		driver.findElement(By.name("1")).click();
		driver.findElement(By.name("+")).click();
		driver.findElement(By.name("2")).click();
		driver.findElement(By.name("=")).click();
		
		// Get the result text
		WebElement sumOfNumbersEle = driver.findElement(By.className("android.widget.EditText"));
		String sumOfNumbers = sumOfNumbersEle.getText();
		
		//verify if result is 3
		Assert.assertTrue(sumOfNumbers.endsWith("3"));
	}

	@AfterTest
	public void End() {
		driver.quit();
	}
}

In the above example, we have added two numbers (1+2) which results 3. The displayed output may be varied in different apps, here as shown in the below screenshot, the output is displayed at the last. So, we have used 'endsWith()' to compare the results.

Execute Appium on Native App android Real device

Hope you have successfully executed above test on you android native app 'Calculator'. Please do let us know if you have faced any issues, we will look into it and try to provide any solution.

Appium Tutorials: 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.