本人最近在Mac OS上使用UiAutomator快速調試類的時候發現跟Windows環境下使用有很大的區別,對於我這個Mac OS小白來講有不少坑要填,今天終於修改完畢,分享代碼,供你們參考。主要區別就是在執行命令的時候須要把命令前面加上執行全路徑。還有一個就是斜槓的問題,統一改過來就能夠了。java
遇到的報錯狀況:android
下面這個是沒有配置全路徑時的報錯信息:shell
Cannot run program "android": error=2, No such file or directory
下面這個是路徑錯誤時的報錯信息:macos
Cannot run program "/Users/dahaohaozai/android-sdk-macosx/toos/android": error=2, No such file or directory
下面是調試類的代碼:編程
package source; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /** * @author ··-·塵 * @E-mail:Fhaohaizi@163.com * @version 建立時間:2017年8月18日 上午10:53:24 * @alter 修改時間: 2017年10月23日10:19:34 類說明:測試調試用例 */ public class UiAutomatorHelper extends Common { private static String android_id = "1";// androidId,寫好不用傳參 private static String jar_name = "";// jar名字 private static String test_class = "";// 包名.類名 private static String test_name = "";// 用例名 // private static String devices = Common.NEXUS5DEVICESID;//自定義設備ID private static String workspace_path;// 工做空間不須要配置,自動獲取工做空間目錄 public UiAutomatorHelper() {// 若是類有帶參構造方法,必須把隱藏的空參構造方法寫出來 output("歡迎使用自定義調試類!"); } /** * 構造方法 * * @param jarName * jar包的名字 * @param testClass * 類名 * @param testName * 方法名Ï */ public UiAutomatorHelper(String jarName, String testClass, String testName) { output("歡迎使用自定義調試類!"); workspace_path = getWorkSpase(); jar_name = jarName; test_class = testClass; test_name = testName; // Common.getInstance().setMobileInputMethodToUtf();//設置輸入法爲utf7 runUiautomator(); // Common.getInstance().setMobileInputMethodToQQ();//設置輸入法爲QQ輸入法 output(Common.LINE + "---FINISH DEBUG----" + Common.LINE);// 結束 } // 運行步驟 private void runUiautomator() { creatBuildXml(); modfileBuild(); buildWithAnt(); pushTestJar(workspace_path + "/bin/" + jar_name + ".jar"); runTest(jar_name, test_class + "#" + test_name); } // 建立build.xml public void creatBuildXml() { // System.out.println"android create uitest-project -n " + jar_name + " -t " + // android_id + " -p " + workspace_path); execCmd(ANDROID_PATH + "android create uitest-project -n " + jar_name + " -t " + android_id + " -p " + workspace_path); } // 修改build public void modfileBuild() { StringBuffer stringBuffer = new StringBuffer();// 建立並實例化stringbuffer try { File file = new File("build.xml"); if (file.isFile() && file.exists()) { // 判斷文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file));// 經過文件字節輸入流建立並實例化輸出字符流(流轉換) BufferedReader bufferedReader = new BufferedReader(read);// 建立並實例化BufferedReader,用來接收字符流 String lineTxt = null;// 用來接收readline的結果 while ((lineTxt = bufferedReader.readLine()) != null) {// 循環讀取處理內容 if (lineTxt.matches(".*help.*")) {// 正則匹配 lineTxt = lineTxt.replaceAll("help", "build");// 替換help爲build } stringBuffer = stringBuffer.append(lineTxt + "\t\n");// stringbuffer接收修改後的內容 } bufferedReader.close();// 關閉流,有依賴關係因此先關閉 read.close();// 關閉流 } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("讀取文件內容出錯"); e.printStackTrace(); } // 修改後寫回去 writerText("build.xml", new String(stringBuffer)); } // ant 執行build public void buildWithAnt() { execCmd(ANT_PATH + "ant"); } /** * 把jar包push到手機上 * @param localPath jar包的絕對路徑 */ public void pushTestJar(String localPath) { String pushCmd = ADB_PATH + "adb push " + localPath + " /data/local/tmp/"; execCmd(pushCmd); } /** * 運行用例方法 * * @param jarName * jar包名字 * @param testName * 運行方法名字 */ public void runTest(String jarName, String testName) { String runCmd = ADB_PATH + "adb shell uiautomator runtest "; String testCmd = jarName + ".jar " + "--nohup -c " + testName; execCmd(runCmd + testCmd); } // 獲取工做空間 public String getWorkSpase() { File directory = new File("");// 建立並實例化file對象 String abPath = directory.getAbsolutePath();// 獲取絕對路徑 return abPath; } // 執行cmd命令 public void execCmd(String cmd) { try { Process p = Runtime.getRuntime().exec(cmd);// 經過runtime類執行cmd命令 // 正確輸出流 InputStream input = p.getInputStream();// 建立並實例化輸入字節流 BufferedReader reader = new BufferedReader(new InputStreamReader(input));// 先經過inputstreamreader進行流轉化,在實例化bufferedreader,接收內容 String line = ""; while ((line = reader.readLine()) != null) {// 循環讀取 System.out.println(line);// 輸出 saveToFile(line, "runlog.log", false);// 保存,false表示不覆蓋 } reader.close();// 此處reader依賴於input,應先關閉 input.close(); // 錯誤輸出流 InputStream errorInput = p.getErrorStream();// 建立並實例化輸入字節流 BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));// 先經過inputstreamreader進行流轉化,在實例化bufferedreader,接收內容 String eline = ""; while ((eline = errorReader.readLine()) != null) {// 循環讀取 System.out.println(eline);// 輸出 saveToFile(eline, "runlog.log", false);// 保存,false表示不覆蓋 } errorReader.close();// 此處有依賴關係,先關閉errorReader errorInput.close(); } catch (IOException e) { e.printStackTrace(); } } // 覆蓋寫入文件 public void writerText(String path, String content) { File dirFile = new File(path); if (!dirFile.exists()) {// 若是不存在,新建 dirFile.mkdir(); } try { // 這裏加入true 能夠不覆蓋原有TXT文件內容,續寫 BufferedWriter bw1 = new BufferedWriter(new FileWriter(path));// 經過文件輸出流來用bufferedwrite接收寫入 bw1.write(content);// 將內容寫到文件中 bw1.flush();// 強制輸出緩衝區內容 bw1.close();// 關閉流 } catch (IOException e) { e.printStackTrace(); } } // 寫入文檔,註釋見writerText方法 public void saveToFile(String text, String path, boolean isClose) { File file = new File("runlog.log"); BufferedWriter bf = null; try { FileOutputStream outputStream = new FileOutputStream(file, true); OutputStreamWriter outWriter = new OutputStreamWriter(outputStream); bf = new BufferedWriter(outWriter); bf.append(text);// 添加內容 bf.newLine(); bf.flush(); if (isClose) { bf.close(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }