Mobile Development 21 min read

A Complete UiAutomator Utility Library for Android UI Testing

This article shares a comprehensive collection of reusable UiAutomator methods—including swipe gestures, screenshot handling, image analysis, UI element lookup, waiting strategies, click actions, scrolling, app launch/stop commands, and miscellaneous helpers—providing Android developers with ready‑to‑use Java code for robust UI automation testing.

FunTester
FunTester
FunTester
A Complete UiAutomator Utility Library for Android UI Testing

The author noticed that previous posts did not cover many useful UiAutomator wrappers, so they compiled and published a full set of utility methods to simplify Android UI automation. The library is written in Java and extends UiAutomatorTestCase, offering ready‑made functions for common testing tasks such as gestures, screenshots, image processing, element interaction, and app control.

Key functional groups :

Swipe gestures: swipeLeft(), swipeRight(), swipeUp(), swipeDown(), plus fine‑grained variants swipUpLittle() and swipDownLittle().

Time utilities: getNow() returns the current timestamp; getDayHourMinute() formats date as dd-HH-mm.

Screenshot handling: screenShot() (overloaded to accept a name or an integer), compressPictureToJpeg() for JPEG conversion, and methods to extract pixel colors or RGB values from captured images.

Image comparison: comparePicture(String path1, String path2, double limit) computes similarity by sampling pixels.

UI element retrieval: a suite of getUiObjectBy* methods (by text, description, resource ID, class name, etc.) that return UiObject instances for further actions.

Interaction helpers: clickPiont(int x, int y), longclickUiObectBy*, writeText(String text), and scrolling utilities ( scrollForward(), scrollBackward()).

App lifecycle control: methods to start or stop specific apps (e.g., startClassApp(), stopClassApp(), startWechat(), stopWechat()), as well as generic commands to toggle Wi‑Fi or launch other packages.

Miscellaneous: random integer generation, short/fast/long wait configuration via Configurator, and file‑system cleanup for screenshot directories.

The library also includes debugging output helpers ( output(...)) overloaded for different parameter types, and a simple warning tester that ensures the device screen is on.

package source;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import com.android.uiautomator.core.Configurator;
import com.android.uiautomator.core.UiDevice;
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;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Bitmap.CompressFormat;
import android.os.RemoteException;
import android.view.KeyEvent;
import jp.jun_nama.test.utf7ime.helper.Utf7ImeHelper;
/**
 * @author ··-·尘
 * @E-mail:[email protected]
 * @version 创建时间:2017年8月18日 上午10:53:24
 * @alter 修改时间:2017年9月12日 09:20:29
 * 类说明:基本api封装
 */
@SuppressWarnings("deprecation")
public class UiaLibrary extends UiAutomatorTestCase {
  public String LINE = "
";
  public void swipeLeft() { //左滑
    int y = UiDevice.getInstance().getDisplayHeight();
    int x = UiDevice.getInstance().getDisplayWidth();
    UiDevice.getInstance().swipe(x-100, y/2, 100, y/2, 20);
    sleep(150);
  }
  public void swipeRight() { //右滑
    int y = UiDevice.getInstance().getDisplayHeight();
    int x = UiDevice.getInstance().getDisplayWidth();
    UiDevice.getInstance().swipe(100, y/2, x-100, y/2, 20);
    sleep(150);
  }
  // ... (remaining methods omitted for brevity) ...
}

Developers can copy this class into their test projects, adjust package names if needed, and call the static-like methods directly to accelerate test script development. Some methods reference file paths such as /mnt/sdcard/123/; these should be adapted to the target device's storage layout. The code also contains a few deprecated calls (e.g., UiAutomatorTestCase), which may require migration to the newer UiAutomator2 framework for future compatibility.

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.