概述
最近学习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的工作,感觉好心累啊,不知道是写自动化的人不喜欢分享还是我找不到,自己整理了下,感觉放上去可能会提高大家的效率,特此献上一些资料,希望能介绍大家调试的时间。
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
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的方法,告别cmd所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复