Android UiAutomator 自动化测试编译运行---新手2

1.首先打开eclipse创建java项目

2.创建后,创建libs文件夹。导入包:android.jar和uiautomator.jar 。包的位置在adt的sdk目录下的platforms目录下有。

3.把包添加到项目中,右键bulid path添加

4.新建测试类,继承UiAutomatorTestCase

5.编写测试用例,必须已test开头. 如下实例:按下home键

import java.io.File;

import android.graphics.Point;
import android.graphics.Rect;
import android.os.RemoteException;
import android.view.KeyEvent;
import android.view.Surface;

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 Test extends UiAutomatorTestCase {
	public void testDemo(){
		//按下home键
		UiDevice.getInstance().pressHome();
	
	}
}

 

 6.编译运行:

  1.找到项目路径,并把项目设置为utf-8编码。

          

  2.打开cmd,cd到项目目录下。cd /d D:\wwww\qqqqq\AndroidUiAutomatorDemo

      3.创建build文件,输入命令:adnroid create uitest-project -n demos -t 1 -p  D:\wwww\qqqqq\AndroidUiAutomatorDemo

    说明:demos 为打包后apk名称  1为android list 查看的id 例如: 1 or "android-23" id号    -p 后面为项目创建目录

      4.修改build文件。刷新创建的项目,下面会出现build.xml文件多出三个。我们要修改build.xml文件中第二行 default='help'改为default='build'

       

     5.开始编译build.xml文件,生成apk安装包文件。apk文件生成在项目目录下的bin目录下面。

          ant -buildfile build.xml

     6.push文件到手机安装apk,也可以用qq或者其他方式安装

         adb push  D:\wwww\qqqqq\AndroidUiAutomatorDemo\bin\demos.jar /data/local/tmp/

    7.运行测试代码。

   adb shell uiautomator runtest demos.jar -c com.qqqq.demo.Test

       说明:demos.jar为apk文件  -c后面的为要执行哪一个测试类。为完整名称。包名+类名.比如以上实例。报名为:com.qqqq.demo ,类名为Test

 

  8.运行成功。

 

7.第六步的运行有些繁琐。如果要调试简单运行的话可以使用下面的方法。

   第一步,创建一个类UiAutomatorHelper   把下面的代码复制到新建的类中。

  

package com.kllays.demo;

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 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(); 

       } 

   } 

	

}

 

  第二步,使用上面的类。

  

public static void main(String[] args){
		String jarName="demos";     //apk安装包名称
		String testClass="com.qqqq.demo.Test";  //报名+类名
		String testName="testBrowser";  //关键,要测试的类以test开头的测试用例。
		String androidId="1";      //android id为在cmd下输入android list 查看到的id
		new UiAutomatorHelper(jarName, testClass, testName, androidId);
	}

 

      

 

转载于:https://www.cnblogs.com/kllay/p/5692007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值