本人最近在Mac OS上使用UiAutomator快速调试类的时候发现跟Windows环境下使用有很大的区别,对于我这个Mac OS小白来说有很多坑要填,今天终于修改完毕,分享代码,供大家参考。主要区别就是在执行命令的时候需要把命令前面加上执行全路径。还有一个就是斜杠的问题,统一改过来就可以了。
遇到的报错情况:
下面这个是没有配置全路径时的报错信息:
Cannot run program "android": error=2, No such file or directory
下面这个是路径错误时的报错信息:
Cannot run program "/Users/dahaohaozai/android-sdk-macosx/toos/android": error=2, No such file or directory
下面是调试类的代码:
1package source;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileNotFoundException;
8import java.io.FileOutputStream;
9import java.io.FileWriter;
10import java.io.IOException;
11import java.io.InputStream;
12import java.io.InputStreamReader;
13import java.io.OutputStreamWriter;
14
15/**
16 * @author ··-·尘
17 * @E-mail:Fhaohaizi@163.com
18 * @version 创建时间:2017年8月18日 上午10:53:24
19 * @alter 修改时间:2017年10月23日10:19:34 类说明:测试调试用例
20 */
21public class UiAutomatorHelper extends Common {
22
23 private static String android_id = "1";// androidId,写好不用传参
24 private static String jar_name = "";// jar名字
25 private static String test_class = "";// 包名.类名
26 private static String test_name = "";// 用例名
27 // private static String devices = Common.NEXUS5DEVICESID;//自定义设备ID
28 private static String workspace_path;// 工作空间不需要配置,自动获取工作空间目录
29
30 public UiAutomatorHelper() {// 如果类有带参构造方法,必须把隐藏的空参构造方法写出来
31 output("欢迎使用自定义调试类!");
32 }
33
34 /**
35 * 构造方法
36 *
37 * @param jarName
38 * jar包的名字
39 * @param testClass
40 * 类名
41 * @param testName
42 * 方法名Ï
43 */
44 public UiAutomatorHelper(String jarName, String testClass, String testName) {
45 output("欢迎使用自定义调试类!");
46 workspace_path = getWorkSpase();
47 jar_name = jarName;
48 test_class = testClass;
49 test_name = testName;
50 // Common.getInstance().setMobileInputMethodToUtf();//设置输入法为utf7
51 runUiautomator();
52 // Common.getInstance().setMobileInputMethodToQQ();//设置输入法为QQ输入法
53 output(Common.LINE + "---FINISH DEBUG----" + Common.LINE);// 结束
54 }
55
56 // 运行步骤
57 private void runUiautomator() {
58 creatBuildXml();
59 modfileBuild();
60 buildWithAnt();
61 pushTestJar(workspace_path + "/bin/" + jar_name + ".jar");
62 runTest(jar_name, test_class + "#" + test_name);
63 }
64
65 // 创建build.xml
66 public void creatBuildXml() {
67 // System.out.println"android create uitest-project -n " + jar_name + " -t " +
68 // android_id + " -p " + workspace_path);
69 execCmd(ANDROID_PATH + "android create uitest-project -n " + jar_name + " -t " + android_id + " -p "
70 + workspace_path);
71 }
72
73 // 修改build
74 public void modfileBuild() {
75 StringBuffer stringBuffer = new StringBuffer();// 创建并实例化stringbuffer
76 try {
77 File file = new File("build.xml");
78 if (file.isFile() && file.exists()) { // 判断文件是否存在
79 InputStreamReader read = new InputStreamReader(new FileInputStream(file));// 通过文件字节输入流创建并实例化输出字符流(流转换)
80 BufferedReader bufferedReader = new BufferedReader(read);// 创建并实例化BufferedReader,用来接收字符流
81 String lineTxt = null;// 用来接收readline的结果
82 while ((lineTxt = bufferedReader.readLine()) != null) {// 循环读取处理内容
83 if (lineTxt.matches(".*help.*")) {// 正则匹配
84 lineTxt = lineTxt.replaceAll("help", "build");// 替换help为build
85 }
86 stringBuffer = stringBuffer.append(lineTxt + "\t\n");// stringbuffer接收修改后的内容
87 }
88 bufferedReader.close();// 关闭流,有依赖关系所以先关闭
89 read.close();// 关闭流
90 } else {
91 System.out.println("找不到指定的文件");
92 }
93 } catch (Exception e) {
94 System.out.println("读取文件内容出错");
95 e.printStackTrace();
96 }
97 // 修改后写回去
98 writerText("build.xml", new String(stringBuffer));
99 }
100
101 // ant 执行build
102 public void buildWithAnt() {
103 execCmd(ANT_PATH + "ant");
104 }
105
106 /**
107 * 把jar包push到手机上
108 * @param localPath jar包的绝对路径
109 */
110 public void pushTestJar(String localPath) {
111 String pushCmd = ADB_PATH + "adb push " + localPath + " /data/local/tmp/";
112 execCmd(pushCmd);
113 }
114
115 /**
116 * 运行用例方法
117 *
118 * @param jarName
119 * jar包名字
120 * @param testName
121 * 运行方法名字
122 */
123 public void runTest(String jarName, String testName) {
124 String runCmd = ADB_PATH + "adb shell uiautomator runtest ";
125 String testCmd = jarName + ".jar " + "--nohup -c " + testName;
126 execCmd(runCmd + testCmd);
127 }
128
129 // 获取工作空间
130 public String getWorkSpase() {
131 File directory = new File("");// 创建并实例化file对象
132 String abPath = directory.getAbsolutePath();// 获取绝对路径
133 return abPath;
134 }
135
136 // 执行cmd命令
137 public void execCmd(String cmd) {
138 try {
139 Process p = Runtime.getRuntime().exec(cmd);// 通过runtime类执行cmd命令
140 // 正确输出流
141 InputStream input = p.getInputStream();// 创建并实例化输入字节流
142 BufferedReader reader = new BufferedReader(new InputStreamReader(input));// 先通过inputstreamreader进行流转化,在实例化bufferedreader,接收内容
143 String line = "";
144 while ((line = reader.readLine()) != null) {// 循环读取
145 System.out.println(line);// 输出
146 saveToFile(line, "runlog.log", false);// 保存,false表示不覆盖
147 }
148 reader.close();// 此处reader依赖于input,应先关闭
149 input.close();
150 // 错误输出流
151 InputStream errorInput = p.getErrorStream();// 创建并实例化输入字节流
152 BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));// 先通过inputstreamreader进行流转化,在实例化bufferedreader,接收内容
153 String eline = "";
154 while ((eline = errorReader.readLine()) != null) {// 循环读取
155 System.out.println(eline);// 输出
156 saveToFile(eline, "runlog.log", false);// 保存,false表示不覆盖
157 }
158 errorReader.close();// 此处有依赖关系,先关闭errorReader
159 errorInput.close();
160 } catch (IOException e) {
161 e.printStackTrace();
162 }
163 }
164
165 // 覆盖写入文件
166 public void writerText(String path, String content) {
167 File dirFile = new File(path);
168 if (!dirFile.exists()) {// 如果不存在,新建
169 dirFile.mkdir();
170 }
171 try {
172 // 这里加入true 可以不覆盖原有TXT文件内容,续写
173 BufferedWriter bw1 = new BufferedWriter(new FileWriter(path));// 通过文件输出流来用bufferedwrite接收写入
174 bw1.write(content);// 将内容写到文件中
175 bw1.flush();// 强制输出缓冲区内容
176 bw1.close();// 关闭流
177 } catch (IOException e) {
178 e.printStackTrace();
179 }
180 }
181
182 // 写入文档,注释见writerText方法
183 public void saveToFile(String text, String path, boolean isClose) {
184 File file = new File("runlog.log");
185 BufferedWriter bf = null;
186 try {
187 FileOutputStream outputStream = new FileOutputStream(file, true);
188 OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);
189 bf = new BufferedWriter(outWriter);
190 bf.append(text);// 添加内容
191 bf.newLine();
192 bf.flush();
193 if (isClose) {
194 bf.close();
195 }
196 } catch (FileNotFoundException e1) {
197 e1.printStackTrace();
198 } catch (IOException e) {
199 e.printStackTrace();
200 }
201
202 }
203}
原文作者:FunTester;如有侵权,请联系管理员删除。