Test Automation For Android app

The Android apps test automation can be done using 

- UI Automator Java library from Google or 

- Appium.

This article explains how the UI Automator library works.

The test automation process has 3 parts:

1. set up the environment

2. write the java test automation code

3. run the code

1. Set up the environment

Install the Android device driver

First, confirm that the Android device (Nexus 5 in this case) does not have any errors in Device Manager:

- open Control Panel

- click System

- click Device Manager

- expand the Portable Device category

- check if an error icon is displayed for Nexus 5

If there is an error icon for Nexus 5, most probably, there is no driver installed for the device.

To install it, the driver needs to be downloaded first from 

After the driver archive is downloaded, extract the files in a folder and update the Nexus 5 driver in Device Manager:

Install the Android SDK

The Android SDK is also required.

It can be installed from http://developer.android.com/sdk/index.html.

The download includes the ECLIPSE ADT (Eclipse IDE bundled with ADT = Android Development Tools).

The path of the SDK folder needs to be added to the PATH environment variable (Control Panel --> System --> Advanced Settings --> Environment Variables --> System Variables).

Extract the files from the archive in a folder that will look in the end like this:

Eclipse can be opened using Eclipse.exe from the eclipse folder. 

Eclipse is integrated with the Android SDK as the following options are visible in the toolbar:

Android SDK Manager

Android Virtual Device Manager

The Android SDK has multiple components that we will use:

ADB (Android Debug Bridge)

This is a command tool that allows communication with the connected Android device.

You can see how it works on this link: http://developer.android.com/tools/help/adb.html.

You can find this tool in the platform-tools folder of the Android SDK.

The path of the SDK folder needs to be added to the PATH environment variable (Control Panel --> System --> Advanced Settings --> Environment Variables --> System Variables).


It creates the required build configuration files to build the output JAR for the project.

You can find it in the Tools folder of the Android SDK.

The path of the SDK folder needs to be added to the PATH environment variable (Control Panel --> System --> Advanced Settings --> Environment Variables --> System Variables).


The test automation code will be written in a class file included in an Eclipse project.

To have access to the UIAUTOMATOR library, the following JAR files need to be added to the project in Eclipse (Properties --> Java Build Path --> Libraries):


You can find these files in the platforms\android-21 folder of the Android SDK.

Install ANT

Apache Ant is a Java library and command-line tool that help building software.

Download it from http://ant.apache.org/bindownload.cgi

Extract the files from the archive in a folder.

Make changes to environment variables:

  • Add the bin directory to your path.
  • Set the ANT_HOME environment variable to the directory where you installed Ant
  • Optionally, set the JAVA_HOME environment variable. This should be set to the directory where your JDK is installed.


The viewer can be found in the Tools folder of the Android SDK.

This is what you will see after double clicking on uitestautomatorviewer.bat.

To inspect the app, first take a screenshot from the device by clicking on the second icon in the toolbar. 

Then, as soon as the screeshot is displayed, click on an element in it. 

You should be able to see the element details in the right side of the viewer:

2. Write the java test automation code

First, open ECLIPSE ADT and create a new projects.

Add the uiautomator.jar and android.jar files to the project as explained above.

Add a class file to the project.

Add the java code to it. The following code does the following:

1. taps the  home button of the device

2. tap the All Apps button

3. swipes right to get the second page of apps

4. taps on the Settings app

5. check that the settings app exists

import org.junit.Test;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class AndroidProject extends UiAutomatorTestCase{

public void testApp() throws UiObjectNotFoundException {


UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));


UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));


UiObject settingsApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Settings");


UiObject settingsValidation = new UiObject(new UiSelector().packageName("com.android.settings"));

assertTrue("Unable to detect Settings", settingsValidation.exists());

Save the changes.

3. Run the code

The remaining part will happen in the command prompt:

1. change the active folder to the project folder

2. run adb devices to see that there are connected devices; for any connected device, you will be able to see its serial number

3. run the following command to generate the build.xml file; this file will be used further for generating the jar file with the test automation code

android create uitest-project -n AndroidProject -t 1 -p                                    C:\Users\home\workspace\AndroidProject

4. run the following command to generate the test automation jar file in the bin sub-folder from the project folder:

ant build

5. run the following command to deploy the jar file to the device:

adb push C:\Users\home\workspace\AndroidProject\bin\AndroidProject.jar  /data/local/tmp/

6. run the following command to execute the automated tests:

adb shell uiautomator runtest AndroidProject.jar -c AndroidProject

After starting the command, you can see everything taking place on the device.

Share this