自写UiAutomator 调试类
package sms_test;
import java.lang.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import android.R.plurals;
import android.R.string;
import common.LengthConverter;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class Helper {
private static String android_id = ""; //id号
private static String jar_name = ""; //类名
private static String test_class = ""; //包名加类名
private static String test_name = ""; //测试用例名
private static String project = ""; //项目名
private static String sdkpath = "set ANDROID_HOME=\"D:\\Android_SDK_7.0\\Android_SDK_6.0\""; //SDK路径
private static String sdktoolspath = "\"D:\\Android_SDK_7.0\\Android_SDK_6.0\\tools\""; //SDKTools路径
//工作空间路径
private static String workspace_path;
private static String Logname;
public static void main(String [] args)throws Exception,IOException{
new Helper("1", "SMS_Test", "sms_test.SMS_Test", "SMS_test",3);
}
public Helper (String Android_id,String Jar_name,String Tese_class,String Project ,int j)throws Exception,IOException{ //构造方法
android_id = Android_id;
jar_name = Jar_name;
test_class = Tese_class;
project = Project;
workspace_path = getWorkSpase();
for(int i=0;i<j;i++){
Logname = "log"+i+".txt";
RunningSteps(Logname,i);
}
}
public void execmd(String cmd,String logname){ //cmd并且输出信息到控制台 并且输出log
System.out.println("----execCmd: " + cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
Collection<String> list = new ArrayList<String>();
//正确输出流
InputStream input = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
input));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
list.add(line);
}
writer(list, Logname);
//错误输出流
InputStream errorInput = p.getErrorStream();
Collection<String>errorlist= new ArrayList<String>();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(
errorInput));
String eline = "";
while ((eline = errorReader.readLine()) != null) {
System.out.println(eline);
errorlist.add(eline);
}
writer(errorlist, "error"+Logname);
} catch (IOException e) {
e.printStackTrace();
System.out.println("------cmd执行报错-------");
}
}
/*
* 输出流
*/
public void writer(Collection<String> list,String filelog)throws IOException{
try{
File file = new File(workspace_path+"\\"+filelog);
Iterator<String> it = list.iterator();
FileWriter fw = new FileWriter(file,true);
BufferedWriter bufw = new BufferedWriter(fw);
while(it.hasNext()){
bufw.write(it.next());
bufw.newLine();
}
bufw.flush();
bufw.close();
fw.close();
}catch(IOException e){
e.printStackTrace();
System.out.println("输出流报错");
}
}
public String getWorkSpase(){ //获取工作空间路径
File file = new File("");
String abPath = file.getAbsolutePath();
return abPath;
}
public void Bale (String logname){ //ant打包
System.out.println("-----------正在执行ant编译-----------");
execmd("cmd /c cd /d "+sdktoolspath+ " & android create uitest-project -n "+project+" -t 1 -p"+" \""+workspace_path+"\"",logname);
execmd("cmd /c "+sdkpath+" & "+" cd /d "+"\""+workspace_path+"\"" +" & "+ "ant build",logname );
}
public void push (String logname){ //将jar文件push到手机
System.out.println("-----------正在将jar包push到手机-----------");
execmd("cmd /c adb push "+workspace_path+"\\bin\\"+project+".jar"+" /data/local/tmp/",logname);
}
public String[] RunTheScript(String logname){ //执行脚本
System.out.println("-------------正在执行脚本--------------");
String [] Start_time_and_end_time = new String[2];
Date date = new Date();
String Start = String.format("%tT",date);
Start_time_and_end_time [0] = Start ;
execmd("cmd /c adb shell uiautomator runtest "+project+".jar --nohup -c "+test_class,logname);
String end = String.format("%tT", date);
Start_time_and_end_time [1] = end;
return Start_time_and_end_time;
}
/*
* 集合log结果
*/
public Map<String, String> result(String logname,String[] time)throws IOException,Exception{
Map<String, String> map = new HashMap<String,String>();
try{
File resultfile = new File(workspace_path+"\\"+logname);
FileReader fileReader = new FileReader(resultfile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
/*
* 1 用例名
* 2 运行结果
* 3 异常输出
* 4开始时间
* 5结束时间
*/
// String testname = "INSTRUMENTATION_STATUS: test="; //匹配测试名
String p1 = "INSTRUMENTATION_STATUS: class="; //匹配包名和类名
String p2 = "Tests run:";//运行结果
String p3 = " com.android.uiautomator.core.UiObjectNotFoundException";
String reder= null;
while((reder = bufferedReader.readLine())!=null){
if(reder.startsWith(p1)){
map.put("01", reder.substring(1,reder.length()));
}
if(reder.startsWith(p2)){
map.put("02", reder.substring(1,reder.length()));
}
if(reder.startsWith(p3)){
map.put("03", reder.substring(1, reder.length()));
}
}
map.put("04", time[0]);
map.put("05", time[1]);
}catch(IOException e1){
e1.printStackTrace();
}catch(Exception e2){
e2.printStackTrace();
}
return map;
}
public void exportXls(String name,Map<String, String> map,int i)throws Exception,IOException,WriteException {
Set<String> setkey = map.keySet();
Iterator<String> it = setkey.iterator();
File file = new File(workspace_path+"\\"+name+".xls");
WritableFont font = new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.NO_BOLD);
WritableCellFormat wf = new WritableCellFormat(font);
wf.setAlignment(Alignment.CENTRE);
wf.setVerticalAlignment(VerticalAlignment.CENTRE);
WritableWorkbook book; //先声明WritableWorkbook (打开的Excel文件)
if(file.exists()){ //判断文件是否存在
Workbook book_1 = Workbook.getWorkbook(file); //Excel存在,获得Excel文件
WritableWorkbook bWorkbook= Workbook.createWorkbook(file,book_1);// 打开一个Excel的副本,并且指定数据写回到原文件
book = bWorkbook; //把打开的Excel副本传回
}else{
WritableWorkbook book_2 = Workbook.createWorkbook(file); //Excel不存在,创建文件,并且打开
book = book_2; //把打开的Excel传出
}
WritableSheet sheet1; //声明 一个WritableSheet (工作表)
if((book.getSheet(0))!=null){ //获取0位置上的工作表,并且判断是否不等于null。
WritableSheet sheet = book.getSheet(0); //不等于null,工作表存在,获取0位置上的工作表。
sheet1 =sheet; //把获取的工作表传出
}else {
WritableSheet sheet = book.createSheet("sheet1", 0); //等于null,工作表不存在,创建0位置上的工作表。
sheet1 = sheet; // 把获取的工作表传出
Label label1 = new Label(0,0,"用例名",wf); //输入第一行的标题栏信息
Label label2 = new Label(1,0,"运行结果",wf);
Label label3 = new Label(2,0,"异常输出",wf);
Label label4 = new Label(3,0,"开始时间",wf);
Label label5 = new Label(4,0,"结束时间",wf);
sheet1.addCell(label1);
sheet1.addCell(label2);
sheet1.addCell(label3);
sheet1.addCell(label4);
sheet1.addCell(label5);
}
while (it.hasNext()){ //输入内容
String str = (String)it.next();
String text = (String)map.get(str);
if(str.equals("01")){
Label label1_1 = new Label(0,1+i,text,wf);
sheet1.addCell(label1_1);
}else if(str.equals("02")){
Label label2_1 = new Label(1,1+i,text,wf);
sheet1.addCell(label2_1);
}else if(str.equals("03")){
Label label3_1 = new Label(2,1+i,text,wf);
sheet1.addCell(label3_1);
}else if(str.equals("04")){
Label label4_1 = new Label(3,1+i,text,wf);
sheet1.addCell(label4_1);
}else if(str.equals("05")){
Label label5_1 = new Label(4,1+i,text,wf);
sheet1.addCell(label5_1);
}
}
book.write();
book.close();
}
public void RunningSteps(String logname,int i)throws Exception,IOException{ //运行步骤
if(i==0){
Bale(Logname);
push(Logname);
}
String[] time =RunTheScript(Logname);
Map<String, String> map = result(Logname,time);
exportXls("测试结果",map,i);
System.out.println("-------------脚本执行完成--------------");
}
}
自写UiAutomator 调试类的更多相关文章
- js 一个自写的 监测类
自从认识了jQuery后,很多页面加载入口,都放在document.ready里面.但是有时候这个觉得ready加载太慢, 这个[监测类 ]就开始产生了 效果类似这个. 每10毫秒检查一次,直到加载了 ...
- Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
#29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...
- 其于OpenXml SDK写的帮助类
/// <summary> /// 其于OpenXml SDK写的帮助类 /// </summary> public static class OpenXmlHelper { ...
- ItcastOA_设计BaseDao_设计DAO接口和实现类_写DAO实现类中的方法内容
3. 基础功能 3.1. 设计BaseDao接口与BaseDaoImpl类 每个实体都应有一个对应的Dao,他封装了对这个实体的数据库操作.例 实体Dao接口实现类 ================= ...
- Controller类的方法上的RequestMapping一定要写在Controller类里吗?
转载请标明出处: https://blog.csdn.net/forezp/article/details/80069961 本文出自方志朋的博客 使用Spring Cloud做项目的同学会使用Fei ...
- 写shell工具类,一个常用实例
简述: 当我们常用到某些指令时,我们就需要将这个命令进行封装.封装的设计和扩展,因人而异.但为了每个人都能够了解到这个命令,常需要写出这个类的help. 关键字: 函数.getopts 函数 通过自定 ...
- JAVA一个文件写多个类
JAVA一个文件写多个类,并且是同级类,需注意: 在一个.java文件中可以有多个同级类, 其修饰符只可以public/abstract/final/和无修饰符 public修饰的只能有一个,且必须 ...
- 30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)是什么体验?
30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)是什么体验? 前言 在本篇文章当中首先给大家介绍三个工具Semaphore, CyclicBa ...
- C#写日志工具类
代码: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System ...
随机推荐
- java程序控制
一.软件生命周期 什么是软件: 软件是程序员根据需求用计算机的语言去编写一系列的指令的集合,能让计算机快速去自动执行.1.1做市场调研----->调研报告书 ======项目经理 1.2可行性分 ...
- kafka_2.11-0.10.0.1生产者producer的Java实现
转载自:http://blog.csdn.net/qq_26479655/article/details/52555283 首先导入包 将kafka目录下的libs中的jar包导入 用maven建立 ...
- elasticsearch 常用命令(一)
索引 搜索 mapping 分词器 1.创建索引 http://192.168.65.131:9200/smartom_index?pretty 2.查看索引: http://192.168.65.1 ...
- 生成OSIDAAuto.OPCServer失败
来源:https://pisquare.osisoft.com/message/13441 A failuare of generating OSIDAAuto.OPCServer dorislipe ...
- sqlserver触发器执行顺序【未经验证】
exec sp_settriggerorder @triggername = 'tr_customer_1', @order = 'first', @stmttype = 'insert',@name ...
- mongodb 安装(官方说明链接)
这里面有很全的 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ 先截个图吧,都是标准的流程,按照操作,是可以安 ...
- git add , git commit 添加错文件 撤销
1. git add 添加 多余文件 这样的错误是由于, 有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤 ...
- python中pip和pygame的安装
1.安装pip和pygame都很简单.首先咱们来安装pip,官网https://pypi.python.org/pypi/pip#download,下载pip的压缩文件,并将其解压. 我们在cmd的p ...
- DS树+图综合练习--二叉树之最大路径
题目描述 给定一颗二叉树的逻辑结构(先序遍历的结果,空树用字符‘0’表示,例如AB0C00D00),建立该二叉树的二叉链式存储结构 二叉树的每个结点都有一个权值,从根结点到每个叶子结点将形成一条路径, ...
- 廖雪峰Java1-4数组操作-2数组排序
冒泡排序法 将第一个值和后面的值,挨个比较,如果手里的值比序列的值小,就交换数据,拿新的数字继续比较,直到最后. 再将第二个值和后面的值,挨个比较. 循环往复,排序完成. int[] ns = {28 ...