A Flexible UiAutomator Helper for Android UI Testing – Full Source and Guide

This article presents a customizable Java helper class for Android UiAutomator that streamlines test project creation, build modification, APK deployment, and test execution, complete with detailed code, command explanations, and logging utilities for flexible mobile UI testing.

FunTester
FunTester
FunTester
A Flexible UiAutomator Helper for Android UI Testing – Full Source and Guide

The author shares a reusable Java helper class, UiAutomatorHelper, designed to simplify Android UI testing with UiAutomator. The class centralizes common tasks—project creation, build file adjustment, Ant compilation, jar deployment, and test execution—while providing extensive inline comments (originally in Chinese, now translated).

Class Structure and Key Fields

The class resides in the student package and imports standard I/O utilities. Important static fields include: android_id: default Android ID ("1") used in the create command. jar_name, test_class, test_name: identifiers for the test JAR, its package/class, and the specific test method. devices: device ID (e.g., UseOften.NEXUS5DEVICESID) for ADB commands. workspace_path: automatically resolved workspace directory.

Constructors

Default constructor prints a welcome message via Library.getInstance().output.

Parameterized constructor receives jarName, testClass, and testName, sets the static fields, switches the input method to UTF‑7, runs the full UiAutomator workflow, then restores the input method to QQ and logs completion.

Workflow Method: runUiautomator()

This method orchestrates the testing steps: creatBuildXml() – builds the command to generate a UiAutomator project. modfileBuild() – reads build.xml, replaces occurrences of "help" with "build", and writes the modified file back. buildWithAnt() – invokes ant to compile the project. pushTestJar() – pushes the compiled JAR to /data/local/tmp/ on the target device. runTest() – executes the test via adb shell uiautomator runtest.

Supporting Methods

creatBuildXml()

: Executes

cmd /c android create uitest-project -n {jar_name} -t {android_id} -p "{workspace_path}"

to generate the project skeleton. modfileBuild(): Opens build.xml, iterates each line, replaces the word "help" with "build", and rewrites the file. buildWithAnt(): Runs cmd /c ant to compile the project. pushTestJar(String localPath): Constructs an ADB push command using the device ID and copies the JAR to the device. runTest(String jarName, String testName): Forms the command

adb -s {devices} shell uiautomator runtest {jarName}.jar --nohup -c {testName}

and executes it. getWorkSpase(): Returns the absolute path of the current working directory. execCmd(String cmd): Executes any shell command, streams both standard output and error, prints each line, and appends the output to runlog.log without overwriting. writerText(String path, String content): Writes the supplied content to the specified file, creating the file if it does not exist. saveToFile(String text, String path, boolean isClose): Appends log entries to runlog.log, optionally closing the writer.

Usage Example

Instantiate the helper with the desired JAR name, test class, and test method, then let the constructor run the entire process:

UiAutomatorHelper helper = new UiAutomatorHelper("MyTest", "com.example.tests", "testLogin");

This single line triggers project creation, build modification, compilation, deployment, and execution, providing a flexible foundation for Android UI automation scripts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAndroidAutomationmobile testingUI testingUIAutomator
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.