Mobile Development 4 min read

Running UiAutomator Tests on Multiple Android Devices Simultaneously Using adb -s

This article explains how to modify a Java UiAutomator helper to use the adb -s option, allowing simultaneous debugging and test execution on two Android devices by adding an extra device identifier parameter to the helper methods.

FunTester
FunTester
FunTester
Running UiAutomator Tests on Multiple Android Devices Simultaneously Using adb -s

The author encountered a limitation when using Android UiAutomator for testing: only one device could be connected at a time. After discovering the -s parameter in adb commands, they updated the debugging helper class to differentiate multiple devices, enabling concurrent testing on two phones.

Below is the revised constructor of the helper class, which now accepts an additional devicesId argument:

public UiAutomatorHelper(String jarName, String testClass, String testName, String androidId, String devicesId) {
    System.out.println("-----------start--uiautomator--debug-------------");
    workspace_path = getWorkSpase();
    System.out.println("----工作空间:\t\n" + getWorkSpase());
    jar_name = jarName;
    test_class = testClass;
    test_name = testName;
    android_id = androidId;
    devices = devicesId;
    runUiautomator();
    System.out.println("*******************");
    System.out.println("---FINISH DEBUG----");
    System.out.println("*******************");
}

The pushTestJar and runTest methods are also updated to prepend the device identifier to the adb commands:

public void pushTestJar(String localPath) {
    localPath = "\"" + localPath + "\"";
    System.out.println("----jar包路径: " + localPath);
    String pushCmd = "adb -s " + devices + " push " + localPath + " /data/local/tmp/";
    System.out.println("----" + pushCmd);
    execCmd(pushCmd);
}

public void runTest(String jarName, String testName) {
    String runCmd = "adb -s " + devices + " shell uiautomator runtest "; // -s specifies the device
    String testCmd = jarName + ".jar " + "--nohup -c " + testName;
    System.out.println("----runTest:  " + runCmd + testCmd);
    execCmd(runCmd + testCmd);
}

Usage of the helper now requires passing the device ID as the last argument, for example:

new UiAutomatorHelper("Demo", "student.Student", "testTest", "1", NEXUS5DEVICESID);

The author notes that a future improvement will involve multithreading to run different test cases on multiple phones simultaneously, though the exact management strategy is still under consideration.

javaAndroidADBUiAutomatormulti-device testing
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

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