本人在使用android UiAutomator的时候,有个问题一直困扰着我,使用调试类做测试,电脑只能插上一台手机,前些天偶然发现了adb命令里面有一个 -s 的参数可以区分不同的手机设备,故修改了一下快速调试类的方法,使得能够同时连上两台手机做测试了,下面分享一下主要的代码,共大家参考。
下面这个是快速调试类的运行方法,多加一个参数就可以了:
1 public UiAutomatorHelper(String jarName, String testClass, String testName, String androidId, String devicesId) {
2 System.out.println("-----------start--uiautomator--debug-------------");
3 workspace_path = getWorkSpase();
4 System.out.println("----工作空间:\t\n" + getWorkSpase());
5
6 jar_name = jarName;
7 test_class = testClass;
8 test_name = testName;
9 android_id = androidId;
10 devices = devicesId;
11 runUiautomator();
12 System.out.println("*******************");
13 System.out.println("---FINISH DEBUG----");
14 System.out.println("*******************");
15 }
下面是push和run方法,在adb命令后面加上devices参数即可:
1 // 4---push jar
2 public void pushTestJar(String localPath) {
3 localPath = "\"" + localPath + "\"";
4 System.out.println("----jar包路径: " + localPath);
5 String pushCmd = "adb -s "+devices+" push " + localPath + " /data/local/tmp/";
6 System.out.println("----" + pushCmd);
7 execCmd(pushCmd);
8 }
9
10 // 运行测试
11 public void runTest(String jarName, String testName) {
12 String runCmd = "adb -s "+devices+" shell uiautomator runtest ";//此处-s表示机器
13 String testCmd = jarName + ".jar " + "--nohup -c " + testName;
14 System.out.println("----runTest: " + runCmd + testCmd);
15 execCmd(runCmd + testCmd);
16 }
下面是要调试类中的使用方法,也是加一个参数即可,将来肯定是要做一个list或者map数组和实时获取设备的devicesid的,这里比较粗糙,直接写了值。
new UiAutomatorHelper("Demo", "student.Student", "testTest", "1", NEXUS5DEVICESID);
以后打算用多线程让两台手机同时运行不同的测试用例,不过暂时没想好管理策略。
原文作者:FunTester;如有侵权,请联系管理员删除。