初学Uiautomator踩的坑

原始需求:进入一个之前连测试都没有的创业公司,希望搭一套app的UI自动测试框架。因为只需要测试安卓,并且要跨应用,选择了uiautomator.个人期望实现用例的执行,校验,输出报告,并且集成到Jenkins。

一.环境的搭建

JDK;

SDK;将platform_tools和tools添加到path

eclipse:安装adt插件

ANT;编译生成jar

二.执行步骤

1.创建java工程,add_external_jars:sdk/platform下的android.jar和uiautomator.jar以及用例中需要的其他jar包,

编写测试用例(基本思路:通过uiautomatorviewer查看控件,通过uiautomator提供的API实现人可以实现的一切对控件的操作)。具体编写用例踩的坑在下一节总结。

2.生成build.xml。name:命名生成jar的名字,-t android_id,通过android list可以查看安装的所有安卓版本,选择API LEVEL大于15的。path:指定project的路径

android create uitest-project -n <name> -t <android-sdk-ID> -p <path>


3.生成jar包


ant build


4.将jar包推到手机上


adb push VCMTestRpt.jar /data/local/tmp/VCMTestRpt.jar


5.在手机上执行jar包


adb shell uiautomator runtest XXX.jar -c pankageName.className#functionName


指定方法名:则只执行该方法


不指定方法名:则执行该类中所有以test开头的方法,按照字符顺序执行


三.编写用例踩坑

1.Uiautomator的不支持中文输入

原因:setText(字符串)无法输入非ASCII字符

解决方法:将输入的原始字符转换成Unicode文本,再通过中间转换来输入各种文字

Jutf7输入法:中文->Unicode->keycode->转换为中文

解决步骤:

1.1 https://github.com/sumio/uiautomator-unicode-input-helper下载zip包解压

1.2 eclipse中导入zip包中的UTF7IME工程,导出生成apk,并安装在测试机上,将UTF7输入法设置为默认输入法。

1.3 helper-library下相应源代码拷贝至测试工程中


1.4 如下方式实现输入中文

 

2.执行步骤繁杂,使用类UiAutomatorHelper实现编译jar,push jar,运行jar等一系列操作

UiautomatorHelper代码:

package com.checheyun.vcm.VcmUiTest;

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 = "";
	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--判断是否有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]; 
               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(); 

       } 

   } 

	

}

new一个对象实现执行用例的步骤

public static void main(String[] args){
		
		String android_id = "1";
		 //android list target id
		 String jar_name = "CaseRpt";
		 //生成jar的名字
		 String test_class = "com.checheyun.vcm.VcmUiTest.VCMuitestOutputRpt";
		 String test_name = "";
		 //方法名,当不指定方法名时,将执行所有以test开头的方法
		 new UiAutomatorHelper(jar_name,test_class,test_name,android_id);
		 
		 
	}

3.Uiautomator无测试报告,原本打算将每次的测试结果写入数据库,失败的地方截图,然后写个页面查看测试报告,后面引入Mongo的驱动包时报错,加上时间原因暂时放弃,仅输出一个txt显示测试结果

3.1 引入第三方库报错 

    有时间参考https://www.cnblogs.com/udld/p/6233887.html试试

3.2 截图

	//用于截屏,图片以用例编号+截屏时间命名
	public void screenshot(String caseno) {  
		Date a = new Date();  
		SimpleDateFormat b = new SimpleDateFormat("yyyyMMddHHmmss");  
		String c = b.format(a);  
		System.out.println(c);  
		File files = new File("/sdcard/testpic/"+caseno+"-"+c+".png");  
		   getUiDevice().takeScreenshot(files);  
		}

3.3 输出txt报告,每执行一个用例就把结果写入txt,结果没法去重排序

    解决方法:用例执行后结果仅写入内存,最后一个用例将用例编号去重排序后一起写入文件(使用treeMap自动实现排序和去重)

    定义一个Map,用来存储用例的执行结果

static Map<String,String> testResult=new TreeMap<String,String>();

    最后一个用例命名testzzz...用以执行将测试结果写入文档

public void testzzzlastcaseinput() throws IOException{

	FileOutputStream fos=new FileOutputStream(filepath,true);
	Set<String> set = testResult.keySet(); //key装到set中
    Iterator<String> it = set.iterator(); //返回set的迭代器 装的key值
    while(it.hasNext()){
    String key = (String)it.next();
    String value = (String)testResult.get(key);
    String s=key+":"+value+"\r\n";
    fos.write(s.getBytes());
	}
    //计算执行用例耗时
    Date dtend=new Date();
    long diff = dtend.getTime()-dt.getTime();
	int hh=(int) diff/3600000;
	int mm=(int)(diff-hh*3600000)/60000;
	int ss=(int)(diff-hh*3600000-mm*60000)/1000;
	int ms=(int) diff%1000;
	String s="执行耗时:"+hh+"时"+mm+"分"+ss+"秒"+ms+"毫秒";
	fos.write(s.getBytes());
    fos.close();
	}

3.4 拿着用例就开始写,用例间相同的操作步骤抽出来作为公用方法,思想偏向面向过程,后期用例维护成本很高,比如修改某一个页面或增加个中间页面,所有涉及这个页面的用例都要更改。一个测试类几千行,维护起来非常麻烦。

新思路:面向对象,每个页面写一个类。后面再搞了真累

四.集成Jenkins,参数化构建版本用例

使用git管理版本,配置简单就不写了

excute shell如下例:

cd /var/lib/jenkins/workspace/auto_test2.0
cp ./etc/.classpath .classpath
/usr/adt-bundle-linux-x86_64-20140624/sdk/tools/android create uitest-project -n VCMTestRpt -t 1 -p /var/lib/jenkins/workspace/auto_test2.0/
/usr/apache-ant-1.10.1/bin/ant build
cd /var/lib/jenkins/workspace/auto_test2.0/bin
/usr/adt-bundle-linux-x86_64-20140624/sdk/platform-tools/adb push VCMTestRpt.jar /data/local/tmp/VCMTestRpt.jar
/usr/adt-bundle-linux-x86_64-20140624/sdk/platform-tools/adb shell uiautomator runtest VCMTestRpt.jar -c com.checheyun.vcm.VcmUiTest



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值