在Eclipse里调试UiAutomator

2024-02-28 21:38
文章标签 调试 eclipse uiautomator

本文主要是介绍在Eclipse里调试UiAutomator,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

该类来自github开源项目fan2597/UiAutomatorHelper,在此感谢该项目作者,如有侵犯权益,望告知,速删!!!

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,安卓idprivate 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("*******************");}/*** 需求: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--判断是否有buildpublic boolean isBuild() {File buildFile = new File("build.xml");if (buildFile.exists()) {return true;}// 创建build.xmlexecCmd("cmd /c android create uitest-project -n " + jar_name + " -t "+ android_id + " -p " + workspace_path);return false;}// 创建build.xmlpublic void creatBuildXml() {execCmd("cmd /c android create uitest-project -n " + jar_name + " -t "+ android_id + " -p " + "\""+workspace_path+ "\"");}// 2---修改buildpublic 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 执行buildpublic void buildWithAnt() {if (System.getProperty("os.name").equals("Linux")) {execCmd("ant");return;}execCmd("cmd /c ant");}// 4---push jarpublic 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(); } } }

这篇关于在Eclipse里调试UiAutomator的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/756705

相关文章

IDEA如何实现远程断点调试jar包

《IDEA如何实现远程断点调试jar包》:本文主要介绍IDEA如何实现远程断点调试jar包的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录问题步骤总结问题以jar包的形式运行Spring Boot项目时报错,但是在IDEA开发环境javascript下编译

eclipse如何运行springboot项目

《eclipse如何运行springboot项目》:本文主要介绍eclipse如何运行springboot项目问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目js录当在eclipse启动spring boot项目时出现问题解决办法1.通过cmd命令行2.在ecl

Python MCPInspector调试思路详解

《PythonMCPInspector调试思路详解》:本文主要介绍PythonMCPInspector调试思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录python-MCPInspector调试1-核心知识点2-思路整理1-核心思路2-核心代码3-参考网址

Linux系统调试之ltrace工具使用与调试过程

《Linux系统调试之ltrace工具使用与调试过程》:本文主要介绍Linux系统调试之ltrace工具使用与调试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、ltrace 定义与作用二、ltrace 工作原理1. 劫持进程的 PLT/GOT 表2. 重定

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C++中实现调试日志输出

《C++中实现调试日志输出》在C++编程中,调试日志对于定位问题和优化代码至关重要,本文将介绍几种常用的调试日志输出方法,并教你如何在日志中添加时间戳,希望对大家有所帮助... 目录1. 使用 #ifdef _DEBUG 宏2. 加入时间戳:精确到毫秒3.Windows 和 MFC 中的调试日志方法MFC