uiatuomator如何调试
博主较笨,在使用junit 和uiatuomator结合时不知道怎么调试,因为uiatuomator一直是push在手机上,而junit是需要代码运行的,那我该怎么办,现在发一下不知道是哪位大神写的代码,很实用,在调用方只要传入他的参数即可
package com.uiautomator.test; 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; public class UiAutomatorHelper { // 以下参数需要配置,用例集id,用例id,安卓id
private static String android_id = "3";
private static String jar_name = "";
private static String test_class = "";
private static String test_name = ""; // 工作空间不需要配置,自动获取工作空间目录
private static String workspace_path; public static void main(String[] args) { }
public UiAutomatorHelper() {
workspace_path = getWorkSpase();
System.out.println("---工作空间:\t\n" + getWorkSpase());
} /**
* 需求:UI工程调试构造器,输入jar包名,包名,类名,用例名
* @param jarName
* @param testClass
* @param testName
* @param androidId
*/
public UiAutomatorHelper(String jarName, String testClass, String testName,
String androidId) {
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;
runUiautomator();
System.out.println("*******************");
System.out.println("---FINISH DEBUG----");
System.out.println("*******************");
}
// 运行步骤
private 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);
} // 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");
// System.out.println("修改后: " + lineTxt);
}
stringBuffer = stringBuffer.append(lineTxt + "\t\n");
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
} System.out.println("-----------------------"); // 修改后写回去
writerText("build.xml", new String(stringBuffer));
System.out.println("--------修改build完成---------");
} // 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包路径: "+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);
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("----execCmd: " + cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
//正确输出流
InputStream input = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
input));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
saveToFile(line, "runlog.log", false);
}
//错误输出流
InputStream errorInput = p.getErrorStream();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(
errorInput));
String eline = "";
while ((eline = errorReader.readLine()) != null) {
System.out.println(eline);
saveToFile(eline, "runlog.log", false);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 需求:写如内容到指定的文件中
*
* @param path
* 文件的路径
* @param content
* 写入文件的内容
*/
public void writerText(String path, String content) { File dirFile = new File(path); if (!dirFile.exists()) {
dirFile.mkdir();
} try {
// new FileWriter(path + "t.txt", true) 这里加入true 可以不覆盖原有TXT文件内容 续写
BufferedWriter bw1 = new BufferedWriter(new FileWriter(path));
bw1.write(content);
bw1.flush();
bw1.close();
} catch (IOException e) {
e.printStackTrace();
}
} 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();
} } }
调用它的方法为:
只要使用以上方法,就可以在手机上跑了,因为他生成了xml文件,还把日志打印在控制台
uiatuomator如何调试的更多相关文章
- uiatuomator提示shortMsg=java.lang.RuntimeException
自动化要做断言,原本打算使用的testng,因为它断言后就能出结果,还能生成报告,但是在实践过程中,硬是没有成功,所以还是放弃,使用的junit,后面使用的cts框架生成的邮件,现在记录使用junit ...
- C# Web应用调试开启外部访问
在用C#开发Web应用时有个痛点,就是本机用VS开启Web应用调试时外部机器无法访问此Web应用.这里将会介绍如何通过设置允许局域网和外网机器访问本机的Web应用. 目录 1. 设置内网访问 2. 设 ...
- NodeJs之调试
关于调试 当我们只专注于前端的时候,我们习惯性F12,这会给我们带来安全与舒心的感觉. 但是当我们使用NodeJs来开发后台的时候,我想噩梦来了. 但是也别泰国担心,NodeJs的调试是很不方便!这是 ...
- 微信公众号开发之VS远程调试
目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 前言 微信公众平台消息接口的工作原理大概可以这样理解:从用户端到公众号端一个流 ...
- 写出易调试的SQL(修订版)
h4 { background: #698B22 !important; color: #FFFFFF; font-family: "微软雅黑", "宋体", ...
- tomcat开发远程调试端口以及利用eclipse进行远程调试
一.tomcat开发远程调试端口 方法1 WIN系统 在catalina.bat里: SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compi ...
- Hawk 4.7 单步调试
单步调试的意义 已经编写的工作流,可能会因为某些外界环境的变化而出错,此时需要排除错误,我们可以使用单步调试. 单步调试的本质,相当于只使用前n个模块,这样就能看到每个步骤下,流的改变. 例子 还是上 ...
- Visual Studio 2012远程调试中遇到的问题
有的时候开发环境没问题的代码在生产环境中会某些开发环境无法重现的问题,或者需要对生产环境代码进行远程调试该怎么办? Vs已经提供给开发者远程调试的工具 下面简单讲讲该怎么用,前期准备:1.本地登录账户 ...
- iOS逆向工程之Hopper+LLDB调试第三方App
LLDB是Low Level Debugger的简称,在iOS开发的调试中LLDB是经常使用的,LLDB是Xcode内置的动态调试工具.使用LLDB可以动态的调试你的应用程序,如果你不做其他的额外处理 ...
随机推荐
- js特效第九天
offset家族(获取元素尺寸) offsetWidth得到对象的宽度,自己的,与他人无关 offsetWidth = width+border+padding,不包含margin offsetHei ...
- codeforce 609A - USB Flash Drives
排序水题 #include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm&g ...
- phpMyAdmin导入本地数据库
phpMyAdmin导入本地数据库 在PHPMyAdmin导入数据时,点击导入--执行后出现错误: 您可能正在上传很大的文件,请参考文档来寻找解决方法. 可能就是因为数据库太大的原因. 那么如何 才能 ...
- MultiByteToWideChar和WideCharToMultiByte用法详解
今天写ini文件的时候发现的问题: TCHAR temp[]; //strcpy_s(temp, request.newVersion); MultiByteToWideChar(CP_ACP, , ...
- 有关按位DP
这是一道正式比赛的题目 数据范围是 10^999 ~ 10^1000 的两个整数以及一个k我记得好像是不超过100,计算两个数中间有多少个每一位相乘最后和k取摸等于0的数.这道题对于不会按位dp的人是 ...
- [转]带花树,Edmonds's matching algorithm,一般图最大匹配
看了两篇博客,觉得写得不错,便收藏之.. 首先是第一篇,转自某Final牛 带花树……其实这个算法很容易理解,但是实现起来非常奇葩(至少对我而言). 除了wiki和amber的程序我找到的资料看着都不 ...
- how to learn device driver
making a linux usb driver http://www.kroah.com/linux/ http://matthias.vallentin.net/blog/2007/04/wri ...
- Umbraco中Document Type取名限制
在Umbraco中,每一个Document type都会被ModelsBuilder生成一个class,但是,有些developers发现,当你把一些Document Type命名为Grid, Pro ...
- magento 列表页显示产品属性值的几种调用方式
之前有人提到要在列表显示一些特定的属性,除了自带的名字,价格等.因为列表页和产品页都有一个同名的产品对象:$_product,而在产品页,$_product是直接可以用$_product->ge ...
- Latex 中宽度的设置和理解
\textwidth, 文本区域的全部宽度 \columnwidth, 文本中一列的宽度,单栏或者多栏的情况下,值是不同的 但是,一旦\textwidth, \columnwidth, \linewi ...