自写UiAutomator 调试类

来源:互联网 发布:上海病假单淘宝交易 编辑:程序博客网 时间:2024/06/08 16:27
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并且输出信息到控制台 并且输出logSystem.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("-------------脚本执行完成--------------");}}


原创粉丝点击