本人在使用android UiAutomator的時候,有個問題一直困擾着我,使用調試類作測試,電腦只能插上一臺手機,前些天偶然發現了adb命令裏面有一個 -s 的參數能夠區分不一樣的手機設備,故修改了一下快速調試類的方法,使得可以同時連上兩臺手機作測試了,下面分享一下主要的代碼,共你們參考。java
下面這個是快速調試類的運行方法,多加一個參數就能夠了:android
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("*******************"); }
下面是push和run方法,在adb命令後面加上devices參數便可:shell
// 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);
數組
之後打算用多線程讓兩臺手機同時運行不一樣的測試用例,不過暫時沒想好管理策略。多線程