背景:在Eclipse中不能直接运行Uiautomator工程,所以每次编写一份用例都要进行手动输入命令,很烦。调试起来不仅繁琐还浪费时间。网上找到一份快速调试的代码UiAutomatorHelper,可将这几步进行简化很方便(当然也可以使用bat文件,相比之下还是这个方便)。

create buildxml file:android create uitest-project -n <jar_name> -t <androidId> -p <path>

ant build xmlfile:ant build

push jarFile:adb  push <jar_path>  data/local/tmp

Run Test:adb shell uiautomator runtest <jar_name> --nohup -c <testClass.testName>

步骤:将UiAutomatorHelper.java放到工程目录下(与测试脚本同目录),在测试脚本中写个main方法。然后Run as ->java application即可

Display.java

package com.change.display;

import java.io.File;
import java.io.IOException;
import android.os.RemoteException;
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; public class Display extends UiAutomatorTestCase{
public static void main(String [] args ){
String jarName="ChangeFont";
String testClass="com.change.display.Display";
String testName="test1";
String androidId="1";
new UiAutomatorHelper(jarName, testClass, testName, androidId);
}
public void test1 () throws UiObjectNotFoundException, RemoteException, IOException{
//Device wake up
UiDevice.getInstance().wakeUp();
//sleep 3s
sleep(3000);
//Open the settings
Runtime.getRuntime().exec("am start -n com.android.settings/.Settings");
//Click on display
try{
UiObject display = new UiObject(new UiSelector().text("显示"));
display.click();
sleep(3000);
}catch(Exception e){
e.printStackTrace();
}
//Select font
UiObject fs = new UiObject(new UiSelector().text("字体大小"));
fs.clickAndWaitForNewWindow();
//Change font
UiObject size = new UiObject(new UiSelector().text("超大"));
size.click();
//Screen shot
sleep(3000);
Runtime.getRuntime().exec("screencap -p /sdcard/test.png");
//Enter Home interface
sleep(3000);
getUiDevice().pressHome();
getUiDevice().takeScreenshot(new File("/sdcard/test1.png"));
sleep(2000);
}
}

UiAutomatorHelper.java (正常流情况,未考虑异常及其他情况)

package com.change.display;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; public class UiAutomatorHelper {
private static String android_id="1";
private static String jar_name="";
private static String test_class="";
private static String test_name="";
private static String workspace_path;
  
public UiAutomatorHelper(String jarName,String testClass,String testName,String androidId) {
System.out.println("---------start--uiautomator--debug");
workspace_path = getWorkSpace();
System.out.println("WorkSpace : \n" + getWorkSpace()); jar_name = jarName;
test_class = testClass;
test_name = testName;
android_id = androidId; crateBuildXML();
System.out.println("----------create build.xml successful"); antBuildXML();
System.out.println("----------ant build.xml successful"); pushJarFile();
System.out.println("----------push jarFile successful");

     runTestCase();
System.out.println("**************************");
System.out.println("--------FINISH DEBUG------");
System.out.println("**************************");
}
// Create build.xml file
private void crateBuildXML() {
execCmd(" cmd /c android create uitest-project -n "+jar_name+" -t "+android_id+" -p "+workspace_path);
}
//Ant build.xml file
private void antBuildXML() {
execCmd("cmd /c ant build");
}
//Push jarFile to device
private void pushJarFile() {
String localPath = workspace_path +"\\bin\\"+ jar_name +".jar";
System.out.println("------jar包路径:"+localPath);
execCmd("adb push "+ localPath + " /data/local/tmp/");
}
//Run test case
private void runTestCase() {
String runString="adb shell uiautomator runtest ";
execCmd(runString+jar_name+".jar "+"--nohup -c "+test_class+"#"+test_name);
} public void execCmd(String cmd){
System.out.println("------execCmd: " + cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
//normal
InputStream input = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = "";
while ((line=reader.readLine()) != null) {
System.out.println(line);
}
//error
InputStream errorInput =p.getErrorStream();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));
String eline = "";
while ((eline=errorReader.readLine())!= null) {
System.out.println(eline);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public String getWorkSpace(){
//getCanonicalPath() and getAbsolutePath()
String pathFile = new File("").getAbsolutePath();
return pathFile;
}
}

注:若编译过程中出现:android不是内部命令,build.xml is a directory !请注意环境变量(SDK,ANT)是否配置正确

顺带附上原始UiAutomatorHelper.java代码

package com.change.display;

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 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()) { // 判断文件是否存在
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];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
}

附批处理实现快速调试:

运行测试暂未加入批处理中,可根据自身需求加入

@echo off

rem 工程目录,Jar包名字,编译后的jar包位置,当前路径
set reso=G:\WorkSpace\DisplayCase
set jar=ChangeFont
set jarLoction=%reso%\bin\%jar%.jar
set dest=%~dp0 cd /d %reso% rem 一行实现
rem android create uitest-project -n %jar% -t 1 -p %reso% && ant build && adb push %jarLoction% /data/local/tmp/ &&copy %jarLoction% %dest% &pause echo ------create build.xml----------
call android create uitest-project -n %jar% -t 1 -p %reso%
echo,
echo ------ant build jar-------------
call ant build 1>nul
echo,
echo ------push jar to device--------
adb push %jarLoction% /data/local/tmp/
echo,
echo ------copy to current path------
copy %jarLoction% %dest% &pause

Android UiAutomator 快速调试的更多相关文章

  1. Android UiAutomator快速调试

    在测试类中添加主函数 public static void main(String[] args){ String jarName,testClass, testName,androidId; jar ...

  2. Uiautomator 快速调试

    UiAutomatorHelper使用      1.介绍:     他是一种可以快速调试的方法:其本身也是java问津相当于自动化脚本,查看该文件,其主要实现的功能如下         1.创建bu ...

  3. UiAutomator快速调试

    步骤: 1.打开浏览器,输入网址https://github.com,搜索uiautomatorhelper 2. 3                    . 4.打开eclipse,File-&g ...

  4. Android无线测试之—UiAutmator运行命令介绍与快速调试

    一.运行命令介绍: #Test.java package com.uiautomatortest; import android.os.Bundle; import android.os.Remote ...

  5. uiautomator手动调试与快速高度设置

    创建java工程:Demo1包名:com.bing.cn类名:Test测试用例:testDemo android create uitest-project -n Demo1 -t 7 -p E:\e ...

  6. UiAutomator--UiAutomatorHelper快速调试

    UiAutomatorHelper使用    1.介绍:他是一种可以快速调试的方法:其本身也是java问津相当于自动化脚本,查看该文件,其主要实现的功能如下 1.创建build:android cre ...

  7. Android Studio快速开发之道

    概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. P ...

  8. Android Studio快速开发之道(各种语法糖)

    现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. Post ...

  9. android的快速开发框架集合

    出自:http://blog.csdn.net/shulianghan/article/details/18046021 1.Afinal  (快速开发框架) 简介:http://www.oschin ...

随机推荐

  1. 电脑丢失api-ms-win-core-libraryloader-|1-1-1.dll怎么办

    电脑从win7升级到win10,到98%的时候提示说丢失.dll,如图,我是64位系统,怎么解决这个问题呢?在脚本之家下载了 放到system32中也没有用,在线等,谢谢! 用C:\Windows\S ...

  2. 第28月第22天 iOS动态库

    1. NIMSDK 在 5.1.0 版本之后已改为动态库,集成方式有所改变,若需要集成高于此版本的 SDK,只需要做以下步骤: 将下载的 SDK 拖动到 Targets -> General - ...

  3. Apache的域名配置

    配置独立域名有什么好处呢?我们在本地做程序开发,要同时用很多开源程序.CMS.框架,或者自己写的管理系统,那么给他们每一个都配置一个独立的域名,在测试的时候只要在浏览器输入设置好的域名就可以了,非常方 ...

  4. ubuntu14.04 Samba服务无法访问 网络名不再可用的问题

    参考链接 : https://blog.csdn.net/liuyixjtu/article/details/54575514

  5. sql-connectionStrings

    <connectionStrings> <add name="ClassReservatConnectionString" connectionString=&q ...

  6. shiro 和 跨域过滤器冲突

    在web.xml 中 cros要写在shiro 的配置前面,如下,不然会先过shiro 过滤器就产生跨域问题, 参考web.xml 的过滤器加载顺序 <!-- 跨域请求 --> <f ...

  7. maven(一) 一 修改仓库存放路径

    一.修改仓库存放路径 maven默认的仓库是在C盘下的,这样当重新装系统的时候,仓库就要重新建了.因此可以修改默认存放的位置. 修改仓库的地址在maven安装包(即apache-maven-bin)下 ...

  8. mysql 案例 ~ 主从复制延迟之并行复制

    一 概念说明   1 模型 并行复制是典型的生产者.消费者模式,Coordinator作为生产者,worker线程作为消费者.   2 Waiting for preceding transactio ...

  9. 20165234 2017-2018-2《Java程序设计》课程总结

    2017-2018-2<Java程序设计>课程总结 一.作业链接汇总 每周作业链接 预备作业一:我期望的师生关系 预备作业二:学习基础和C语言基础调查 预备作业三:Linux安装及学习 第 ...

  10. 使用git和github管理自己的项目---基础操作学习[转]

    原文: https://segmentfault.com/a/1190000003728094 我是通过看廖雪峰的git教程学习的,真的是极好的,以下是我学习过程中的总结,记录下来,方便自己参考以熟悉 ...