package rom;

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 = "1";
private static String jar_name = "Camera_before";
private static String test_class = "rom.Camera_before";
private static String test_name = "testCamera_before"; // 工作空间不需要配置,自动获取工作空间目录
private static String workspace_path; public static void main(String[] args) {
UiAutomatorHelper Hepler = new UiAutomatorHelper();
Hepler.runUiautomator(); }
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("*******************");
}
/**
* 需求:build 和 复制jar到指定目录
* @param jarName
* @param testClass
* @param testName
* @param androidId
* @param isRun
*/
public UiAutomatorHelper(String jarName, String testClass, String testName,
String androidId,String ctsTestCasePath){
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;
buildUiautomator(ctsTestCasePath); 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()) { // 判断文件是否存在 //isFile() 测试此抽象路径名表示的文件是否是一个标准文件
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();
} }
/**
* 需求:编译和复制jar包指定文件
* @param newPath
*/
private void buildUiautomator(String newPath) {
creatBuildXml();
modfileBuild();
buildWithAnt();
//复制文件到指定文件夹
copyFile(workspace_path + "\\bin\\" + jar_name + ".jar", newPath); }
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
System.out.println("源文件路径:"+oldPath);
System.out.println("目标文件路径:"+newPath);
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace(); } } }

UiAutomatorHelper 调试类的更多相关文章

  1. 自写UiAutomator 调试类

    package sms_test; import java.lang.*; import java.util.ArrayList; import java.util.Collection; impor ...

  2. iOS逆向开发(3):锁定APP的目标类与函数 | reveal | lldb | debugserver | 远程调试

    之前介绍了怎么获取APP的所有类的结构信息,这个有什么用呢?用处大了,比如以这一步为基础,下一步通过注入来做更多研究工作. 注入的最小单位是函数,实际上,编译执行的程序在编译后,类就不复存在了,留下来 ...

  3. 调试 - Visual Studio调试

    Visual Studio - 调试 异常处理机制 windows预定义了一系列的异常错误码,每种程序异常都有一个对应的错误码,windows系统将这些类似键值对关系的数据存储在异常处理表中(称为SE ...

  4. 调试 .NET Framework 源代码、.DotNetCore源码

    调试 .NET Framework 源代码..DotNetCore源码 如何调试 .NET Framework 源代码 在 Visual Studio 调试器中指定符号 (.pdb) 和源文件 .NE ...

  5. @Java Web 程序员,我们一起给程序开个后门吧:让你在保留现场,服务不重启的情况下,执行我们的调试代码

    一.前言 这篇算是类加载器的实战第五篇,前面几篇在这里,后续会持续写这方面的一些东西. 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证) 还是Tomcat,关于类加载器的趣味实验 ...

  6. php简单实用的调试工具类

    <?php /* * 调试类 */ class Common_Debug { //打开错误报告 public static function showError($debug = true) { ...

  7. 调试 Android* x86 应用程序的方法以及要使用的工具

    作者:Xiaodong Wang 1.简单介绍 众所周知,Android* 开发者头顶很多称呼:设计员.程序员等,而且一般会不可避免地被称为故障检修工. 代码中的错误无法避免.因此不管您是否一開始就造 ...

  8. C#在代码中编写输出debug信息-类Debug的使用

    文章:C# 的两种debug 方法 文章:C#跟踪和调试程序-Debug类使用 很全面的文章,可以仔细学习使用下. 文章:C#调试类 没有仔细看. 关键字:Debug类和Trace类有什么区别? 微软 ...

  9. Linux高级调试与优化——gdb调试命令

    番外 2019年7月26日至27日,公司邀请<软件调试>和<格蠹汇编——软件调试案例集锦>两本书的作者张银奎老师进行<Linux高级调试与优化>培训,有幸聆听张老师 ...

随机推荐

  1. 脱壳:OEP(即程序入口点)查找 --- 基本思路和常见方法

    OEP:程序的入口点,软件加壳就是隐藏了OEP(或者用了假的OEP), 只要我们找到程序真正的OEP,就可以立刻脱壳. PUSHAD (压栈) 代表程序的入口点, POPAD (出栈) 代表程序的出口 ...

  2. IIS webService 并发 性能

    IIS webService 并发 性能     做的WebService,客户压力测试500并发(随机步长60s-90s),响应速度不理想. 1,优化程序,压缩执行时间2,提高IIS“最大工作进程数 ...

  3. 启动代码和Bootloader区别

    很多人都会把两者混淆,所以这里稍微说一下. 启动代码和Bootloader有点类似于类和结构体的关系,也就是说结构体只是一种很特殊很特殊的类,但不能说类是一种结构体. 也就是说可以说启动代码是Boot ...

  4. FastReport问题整理(http://129.sqdj.gov.cn/?p=77)

    1.FastReport中如果访问报表中的对象?可以使用FindObject方法.TfrxMemoView(frxReport1.FindObject(’memo1′)).Text:=’FastRep ...

  5. 17行代码解决微信小程序图片延迟加载

    js 页面 Page({ data: { realScrollTop: 0,//页面滚动距离 driveHeight //屏幕高度可初始化设置 }, scroll(e){ if(e.detail.sc ...

  6. WebGL和ThreeJs学习6--射线法确定3D空间中所选物体

    一.在 threejs 中如何确定下图3D空间中鼠标点击位置的 object 对象? 二.射线法确定步骤及代码 //Three.js提供一个射线类Raycaster来拾取场景里面的物体.更方便的使用鼠 ...

  7. spring 整合Junit学习

    测试一般是测试的局部功能,使用时需要自己写个测试相关的spring配置文件,比较费劲,所以常用的是纯配置的方式来实现测试. @RunWith(SpringJUnit4ClassRunner.class ...

  8. [蓝桥杯]ALGO-86.算法训练_矩阵乘法

    问题描述 输入两个矩阵,分别是m*s,s*n大小.输出两个矩阵相乘的结果. 输入格式 第一行,空格隔开的三个正整数m,s,n(均不超过200). 接下来m行,每行s个空格隔开的整数,表示矩阵A(i,j ...

  9. (转载)java list排序

    1.简介 这个和数组的排序又不一样了. 其实Java针对数组和List的排序都有实现,对数组而言,你可以直接使用Arrays.sort,对于List和Vector而言,你可以使用Collections ...

  10. Zabbix agentd 命令

    #zabbix_agentd -p 查看zabbix所有的内置监控项 [root@nod01 zabbix_agentd.d]# zabbix_agentd -pagent.hostname [s|Z ...