­

android UiAutomator控制多台手机同时运行用例的方法

  • 2019 年 10 月 11 日
  • 笔记

本人在使用android UiAutomator的时候,有个问题一直困扰着我,使用调试类做测试,电脑只能插上一台手机,前些天偶然发现了adb命令里面有一个 -s 的参数可以区分不同的手机设备,故修改了一下快速调试类的方法,使得能够同时连上两台手机做测试了,下面分享一下主要的代码,共大家参考。

下面这个是快速调试类的运行方法,多加一个参数就可以了:

    public UiAutomatorHelper(String jarName, String testClass, String testName, String androidId, String devicesId) {          System.out.println("-----------start--uiautomator--debug-------------");          workspace_path = getWorkSpase();          System.out.println("----工作空间:tn" + 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("*******************");      }  

下面是push和run方法,在adb命令后面加上devices参数即可:

    // 4---push jar      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表示机器          String testCmd = jarName + ".jar " + "--nohup -c " + testName;          System.out.println("----runTest:  " + runCmd + testCmd);          execCmd(runCmd + testCmd);      }  

下面是要调试类中的使用方法,也是加一个参数即可,将来肯定是要做一个list或者map数组和实时获取设备的devicesid的,这里比较粗糙,直接写了值。

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

以后打算用多线程让两台手机同时运行不同的测试用例,不过暂时没想好管理策略。