Create Appium script for Android Mobile App

In this tutorial, we are going to create our first Appium script for Android App. In our previous posts, we have already covered the installation and configuration part that you need to do before creating Appium script. If you haven’t done it yet then please follow the links provided under Pre-requisite section.

Pre-requisite

a) Download and install tools required to run Appium script on Windows

b) Create new project in Eclipse and import Selenium Stand alone Jar and Appium Client Jar file into it. Following article will help you in this regard. Setup Selenium WebDriver in Eclipse

c) Start Appium Server

d) Connect Android real device to your computer and Enable USB Debugging

Scenario

Here is the scenario that we are going to cover :

1.   Launch Calculator App on Android real device
2.   Multiple two numbers
3.  Print result
4.  Close Calculator App

NOTE: First, we need following 2 important information about the Android App that we need to automate :

  • Main Activity Class Name
  • Package Name

We have to mention the Main Activity Class and Package Name in the Desired Capabilities of our script, so that Appium could recognize that which Page(Activity) it has to launch on device.Â

NOTE: Selenium opens webpage using URL and Appium launch App using Activity.

How to to get App Activity and Package Name?

NOTE: Assuming that Android Device is connected to your computer via USB cable and USB Debugging option of Android Device is also enabled.

1. Launch the App that you want to test on Android Device

2. Open Command Prompt and execute following command to make sure that Android Device is connected:

adb devices

adbdevices

3. Execute following 2 commands one by one to get App Activity and Package Name:

adb shell
dumpsys window windows | grep -E 'mCurrentFocus'

GetActivityAndPackageCommand

Program


import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class CalculatorTest {
	public AppiumDriver<MobileElement> driver = null;

	@BeforeTest
	public void setupstart() throws MalformedURLException {
		DesiredCapabilities capabilities = DesiredCapabilities.android();
		capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
		capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
		capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");
		capabilities.setCapability("appPackage", "com.sec.android.app.popupcalculator");
		capabilities.setCapability("appActivity", "com.sec.android.app.popupcalculator.Calculator");
		driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities);
	}

	@Test
	public void calcTest1() throws Exception {
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='4']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='Multiplication']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='3']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='Equal']")).click();
		String result = driver.findElement(By.className("android.widget.EditText")).getText();
		System.out.println("Result : " + result);
	}

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

In you noticed in the above program, Desired Capabilities are mentioned which helps Appium to understand that which Mobile app it has to launch and on which device etc.
Desired capabilities are basically a set of keys and values which goes to the Appium server to tell that what kind of automation session user is interested in starting up. There are various capabilities which can modify the behavior of the server during automation. You can refer the server capabilities from Appium Doc.

Below, We have explained the Appium server capabilities that have been used in our script.

1. Automaton name is UiAutomator2

What is UIAutomator?

UIAutomator or UIAutomator2 are automation frameworks based on Android instrumentation and allows user to run UI tests on Android Device and Appium uses Google’s UIAutomator framework to execute commands on real devices and emulators. Or you can also say that UIAutomator is Google’s test framework for native app automation at the UI level.

Note: UIAutomator2 does not support Android versions below 5.0 and if your android device OS version is below 5.0 then you have to use UIAutomator framework. In our script, we have used UIAutomator2 as we are using Android version 9.

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");

2. Platform Name : Android
As we have created this script for android, so we have to tell Appium about the platform where we actually want to run our script. For Iphone, it would be iOS.

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

3. Device Name : {Unique Device ID}
We have to tell the Device name to Appium server so that it could recognize the device before launching the Mobile App. You will get this uniqueID after running the “adb devices” command in command Prompt.

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");

4. In order to launch Application on device, We have to mention App Package and Activity name in Desired Capabilities to allow the script to launch the application under test.

capabilities.setCapability("appPackage", "com.sec.android.app.popupcalculator");
capabilities.setCapability("appActivity", "com.sec.android.app.popupcalculator.Calculator");

5. Here, we have to provide the IP address and port number that we have used while starting the Appium server.

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

If you find the information provided above is useful, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *