我是靠谱客的博主 妩媚星星,这篇文章主要介绍从eclipse运行UiAutomator的方法,告别cmd,现在分享给大家,希望可以做个参考。

最近学习uiautomator,感觉每次跑起来都好麻烦,不知道别的大神怎么着,我才入门自动化测试不久,到处学,找的资料都是很零散的,自己感觉原始的方法很麻烦,原始的方法应该是如下的:

字段说明:

 LearnUIAutomator 工程名

D:DEVworkspaceLearnUIAutomato 工程路径

CXQUiautomatorTestCase 类名

1、生成build.xml文件,cmd进入Android sdk下的 Tool文件夹 

android create uitest-project -n  LearnUIAutomator -t 1 -p D:DEVworkspaceLearnUIAutomator

2、cmd 进入工程目录

ant build 

3、把bin下的jar拷到data/local/tmp 

adb push  D:DEVworkspaceLearnUIAutomatorbinLearnUIAutomator.jar data/local/tmp

 4、启用

 uiautomator runtest LearnUIAutomator.jar -c com.cxq.testcase.CXQUiautomatorTestCase


每次调测试类都需要重复上面2-4的工作,感觉好心累啊,不知道是写自动化的人不喜欢分享还是我找不到,自己整理了下,感觉放上去可能会提高大家的效率,特此献上一些资料,希望能介绍大家调试的时间。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.travel.testcase; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class RunTestCase { // 以下参数需要配置,用例集id,用例id,安卓id private static String android_id = "1"; private static String jar_name = "Demo"; private static String test_class = "com.demo.com"; private static String test_name = "Demo"; private static String workspace_path; // 是否被调试,默认不开启调试模式,UiAutomator的调试,需要配合remote Java Apllication 使用,详见后续文章 public boolean isDebug = false; public void setDebug(boolean isDebug) { this.isDebug = isDebug; } public static void main(String[] args) { RunTestCase runCase = new RunTestCase(); runCase.runUiautomator(); } public RunTestCase() { workspace_path = getWorkSpase(); System.out.println("---workspace path:tn" + getWorkSpase()); } /** * 原生工程调试构造器,输入jar包名,包名,类名,用例名 * @param jarName 要打成的jar包名称 * @param testClass packageName+testCaseName * @param testName 测试的方法名称 * @param androidId Android list ID值 */ public RunTestCase(String jarName, String testClass, String testName, String androidId) { System.out.println("-----------start--uiautomator--debug-------------"); workspace_path = getWorkSpase(); System.out.println("----workspace:tn" + getWorkSpase()); jar_name = jarName; test_class = testClass; test_name = ""; android_id = androidId; } public void name() { } // 原生工程运行步骤 public void runUiautomator() { creatBuildXml(); modfileBuild(); buildWithAnt(); if (System.getProperty("os.name").equals("Linux")) { pushTestJar(workspace_path + "/bin/" + jar_name + ".jar"); } else { pushTestJar(workspace_path + "\bin\" + jar_name + ".jar"); } if (test_name.equals("")) { runTest(jar_name, test_class); return; } runTest(jar_name, test_class + "." + test_name); System.out.println("*************************"); System.out.println("*----TESTCASE FINISH----*"); System.out.println("*************************"); } // 1--判断是否有build public boolean isBuild() { File buildFile = new File("build.xml"); if (buildFile.exists()) { return true; } // 创建build.xml execCmd("cmd /c android create uitest-project -n " + jar_name + " -t " + android_id + " -p " + workspace_path); return false; } // 创建build.xml public void creatBuildXml() { execCmd("cmd /c android create uitest-project -n " + jar_name + " -t " + android_id + " -p " + """ + workspace_path + """); } // 2---修改build public void modfileBuild() { StringBuffer stringBuffer = new 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); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { if (lineTxt.matches(".*help.*")) { lineTxt = lineTxt.replaceAll("help", "build"); } stringBuffer = stringBuffer.append(lineTxt + "tn"); } read.close(); } else { System.out.println("File could not be found!"); } } catch (Exception e) { System.out.println("Read file error!"); e.printStackTrace(); } System.out.println("-----------------------"); // 修改后写回去 writerText("build.xml", new String(stringBuffer)); System.out.println("--------build.xml is modified---------"); } /** * 写如内容到指定的文件中 * @param path 文件的路径 * @param content 写入文件的内容 */ public void writerText(String path, String content) { File dirFile = new File(path); if (!dirFile.exists()) { dirFile.mkdir(); } try { BufferedWriter bw1 = new BufferedWriter(new FileWriter(path)); bw1.write(content); bw1.flush(); bw1.close(); } catch (IOException e) { e.printStackTrace(); } } // 3---ant 执行build public void buildWithAnt() { if (System.getProperty("os.name").equals("Linux")) { execCmd("ant"); return; } execCmd("cmd /c ant"); } // 4---push jar public void pushTestJar(String localPath) { localPath = """ + localPath + """; System.out.println("----jar Path: " + localPath); String pushCmd = "adb push " + localPath + " /data/local/tmp/"; System.out.println("----" + pushCmd); execCmd(pushCmd); } // 运行测试 public void runTest(String jarName, String testName) { String runCmd = "adb shell uiautomator runtest "; String testCmd = jarName + ".jar " + "--nohup -c " + testName; System.out.println("----runTest: " + runCmd + testCmd); if (isDebug) { execCmd(runCmd + testCmd + " -e debug true"); } else { execCmd(runCmd + testCmd); } } public String getWorkSpase() { File directory = new File(""); String abPath = directory.getAbsolutePath(); return abPath; } /** * 需求:执行cmd命令,且输出信息到控制台 * @param cmd */ public void execCmd(String cmd) { System.out.println("------execute command: " + cmd); try { Process p = Runtime.getRuntime().exec(cmd); InputStream input = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader( input)); int code = 0; while ((code = reader.read()) != -1) { System.out.print((char) code); } } catch (IOException e) { e.printStackTrace(); } } }


把这个工具类放到自己的工程中,在测试类中添加如下语句,就可以直接在eclipse中右键你的测试类,然后run as-->Java Application


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.travel.testcase; import java.io.IOException; import java.util.ArrayList; import android.R.integer; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.core.UiObject; import com.android.uiautomator.core.UiObjectNotFoundException; import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.testrunner.UiAutomatorTestCase; import com.travel.data.ActivityNode; import com.travel.data.DataOperator; import com.travel.tools.TravelLog; import com.travel.tools.UiUtils; public class CXQUiautomatorTestCase extends UiAutomatorTestCase { public static void main(String[] args) throws IOException { <span style="color:#ff0000;">RunTestCase runTestCase=new RunTestCase("CXQUiautomatorTestCase", "com.travel.testcase.CXQUiautomatorTestCase", "", "1"); runTestCase.setDebug(true); runTestCase.runUiautomator();</span> } @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); } @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super.tearDown(); } @SuppressWarnings("deprecation") public void testDemo() throws UiObjectNotFoundException { //...这里写你的用例 } }



最后

以上就是妩媚星星最近收集整理的关于从eclipse运行UiAutomator的方法,告别cmd的全部内容,更多相关从eclipse运行UiAutomator内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部