Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Black-Box UI Testing with Android UI Automator

Tech May 16 1

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:

  1. Download and extract the Ant installation package.
  2. Create a new system environment variable named ANT_HOME pointing to the Ant installation directory.
  3. Append the Ant bin directory (e.g., C:\tools\apache-ant-1.10.12\bin) to the system PATH variable.
  4. Verify the setup by executing ant -version in 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\AndroidUITest

If 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 build

Push the generated JAR file to the target device or emulator:

adb push AutoSettingsTest.jar /data/local/tmp

Execute the test class on the device:

adb shell uiautomator runtest AutoSettingsTest.jar -c com.test.automation.SettingsNavigator

Framework 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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.