Implementing Black-Box UI Testing with Android UI Automator
Environment Configuration
Setting up UI Automator requires the following components:
- JDK with properly configured environment variables.
- Android Studio or an alternative IDE.
- Android SDK.
- Apache Ant (for building the test scripts into executable JAR files).
To configure Apache Ant:
- Download and extract the Ant installation package.
- Create a new system environment variable named
ANT_HOMEpointing to the Ant installation directory. - Append the Ant
bindirectory (e.g.,C:\tools\apache-ant-1.10.12\bin) to the systemPATHvariable. - Verify the setup by executing
ant -versionin the command prompt.
Project Setup and Test Implementation
Create a new Java project in your IDE. Ensure the build path includes JUnit along with the android.jar and uiautomator.jar files from your target Android platform directory.
Define a package (e.g., com.test.automation) and create a new class extending UiAutomatorTestCase. The following example demonstrates navigating to the Settings application, accessing the Sound menu, and verifying the interface:
package com.test.automation;
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 SettingsNavigator extends UiAutomatorTestCase {
public void testNavigateToSoundSettings() throws UiObjectNotFoundException {
// Return to the home screen
getUiDevice().pressHome();
// Locate the App Drawer button using its content description
UiObject appDrawerBtn = new UiObject(new UiSelector().description("Apps"));
appDrawerBtn.clickAndWaitForNewWindow();
// Switch to the Apps tab within the drawer
UiObject applicationTab = new UiObject(new UiSelector().text("Apps"));
applicationTab.click();
// Scroll horizontally to find the target application
UiScrollable scrollableList = new UiScrollable(new UiSelector().scrollable(true));
scrollableList.setAsHorizontalList();
UiObject targetApp = scrollableList.getChildByText(
new UiSelector().className(android.widget.TextView.class.getName()),
"Settings"
);
targetApp.clickAndWaitForNewWindow();
// Confirm the Settings application is open
UiObject settingsScreenCheck = new UiObject(new UiSelector().packageName("com.android.settings"));
assertTrue("Settings application did not open", settingsScreenCheck.exists());
// Navigate to the Sound section
UiObject soundMenuOption = new UiObject(new UiSelector().text("Sound"));
soundMenuOption.clickAndWaitForNewWindow();
// Verify the Sound section is displayed
UiObject volumeHeaderCheck = new UiObject(new UiSelector().text("Volumes"));
assertTrue("Sound settings not found", volumeHeaderCheck.exists());
// Return to the home screen
getUiDevice().pressHome();
}
}Building and Deploying
To generate the build XML file, use the Android SDK command-line tool. First, check available targets by running android list and note the target ID corresponding to your platform.
Execute the following command to create the UI test project structure:
android create uitest-project -n AutoSettingsTest -t 1 -p D:\projects\AndroidUITestIf you encounter the error Class not found: javac1.8 during the build process, it indicates an incompatibility between your Ant version and the Java environment. Updating to the latest version of Apache Ant resolves this issue.
Navigate to your project directory and compile the JAR file using Ant:
ant buildPush the generated JAR file to the target device or emulator:
adb push AutoSettingsTest.jar /data/local/tmpExecute the test class on the device:
adb shell uiautomator runtest AutoSettingsTest.jar -c com.test.automation.SettingsNavigatorFramework Capabilities and Constraints
UI Automator provides robust cross-application testing without requiring source code access or re-signing of the target application. It interacts with the device at the system level, allowing automation of native applications like the Dialer or Messenger. Element location is generally straightforward using selectors.
However, the framework has specific constraints. It operates only on Android API level 16 and higher. Utilizing resource-id for element selection requires API level 18 or above. Natively, it offers poor support for non-ASCII character input, though third-party libraries can mitigate this. Additionally, UI object location is flat rather than hierarchical, making deep view traversal less granular compared to instrumentation-based frameworks like Robotium.