zoukankan      html  css  js  c++  java
  • 自写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("-------------脚本执行完成--------------");
    		}
    	}
    
    


  • 相关阅读:
    有一种努力叫“凌晨四点”
    编程思想
    小记
    团队精神与集体主义
    变量起名
    软件项目估量方法
    戏说QQ
    压力说
    AngularJS指令基础(一)
    Leetcode 1021. Best Sightseeing Pair
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708674.html
Copyright © 2011-2022 走看看