恢弘志士之气,不宜妄自菲薄。这篇文章主要讲述Android UiAutomator 快速调试相关的知识,希望能为你提供帮助。
背景:在Eclipse中不能直接运行Uiautomator工程,所以每次编写一份用例都要进行手动输入命令创建build文件、ant编译文件、push文件与运行测试这四步。调试起来不仅繁琐还浪费时间。网上找到一份快速调试的代码UiAutomatorHelper,可将这几步进行简化很方便。
步骤:将UiAutomatorHelper.java放到工程目录下(与测试脚本同步目录),在测试脚本中写个main方法。然后Run as ->
java application即可
1 package com.change.display; 2 3 4 import java.io.IOException; 5 6 import android.os.RemoteException; 7 8 import com.android.uiautomator.core.UiDevice; 9 import com.android.uiautomator.core.UiObject; 10 import com.android.uiautomator.core.UiObjectNotFoundException; 11 import com.android.uiautomator.core.UiSelector; 12 import com.android.uiautomator.testrunner.UiAutomatorTestCase; 13 14 public class Display extends UiAutomatorTestCase{ 15public static void main(String [] arg ){ 16String jarName="ChangeFont"; 17String testClass="com.change.display.Display"; 18String testName="test1"; 19String androidId="1"; 20new UiAutomatorHelper(jarName, testClass, testName, androidId); 21} 22public void test1 () throws UiObjectNotFoundException, RemoteException, IOException{ 23//Device wake up 24UiDevice.getInstance().wakeUp(); 25//sleep 3s 26sleep(3000); 27//Open the settings 28Runtime.getRuntime().exec("am start -n com.android.settings/.Settings"); 29//Click on display 30try{ 31UiObject display = new UiObject(new UiSelector().text("显示")); 32display.click(); 33sleep(3000); 34}catch(Exception e){ 35e.printStackTrace(); 36} 37//Select font 38UiObject fs = new UiObject(new UiSelector().text("字体大小")); 39fs.clickAndWaitForNewWindow(); 40//Change font 41UiObject size = new UiObject(new UiSelector().text("超大")); 42size.click(); 43//Screen shot 44sleep(3000); 45Runtime.getRuntime().exec("screencap -p /sdcard/test.png"); 46//Enter Home interface 47sleep(3000); 48getUiDevice().pressHome(); 49} 50 }
顺带附上UiAutomatorHelper.java代码
文章图片
文章图片
1 package com.change.display; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.FileWriter; 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.io.InputStreamReader; 13 import java.io.OutputStreamWriter; 14 15 public class UiAutomatorHelper { 16 17// 以下参数需要配置,用例集id,用例id,安卓id 18private static String android_id = "3"; 19private static String jar_name = ""; 20private static String test_class = ""; 21private static String test_name = ""; 22 23// 工作空间不需要配置,自动获取工作空间目录 24private static String workspace_path; 25 26public static void main(String[] args) { 27 28} 29public UiAutomatorHelper() { 30workspace_path = getWorkSpase(); 31System.out.println("---工作空间:\t\n" + getWorkSpase()); 32} 33 34/** 35* 需求:UI工程调试构造器,输入jar包名,包名,类名,用例名 36* @param jarName 37* @param testClass 38* @param testName 39* @param androidId 40*/ 41public UiAutomatorHelper(String jarName, String testClass, String testName, 42String androidId) { 43System.out.println("-----------start--uiautomator--debug-------------"); 44workspace_path = getWorkSpase(); 45System.out.println("----工作空间:\t\n" + getWorkSpase()); 46 47jar_name = jarName; 48test_class = testClass; 49test_name = testName; 50android_id = androidId; 51runUiautomator(); 52System.out.println("*******************"); 53System.out.println("---FINISH DEBUG----"); 54System.out.println("*******************"); 55} 56/** 57* 需求:build 和 复制jar到指定目录 58* @param jarName 59* @param testClass 60* @param testName 61* @param androidId 62* @param isRun 63*/ 64public UiAutomatorHelper(String jarName, String testClass, String testName, 65String androidId,String ctsTestCasePath){ 66System.out.println("-----------start--uiautomator--debug-------------"); 67workspace_path = getWorkSpase(); 68System.out.println("----工作空间:\t\n" + getWorkSpase()); 69 70jar_name = jarName; 71test_class = testClass; 72test_name = testName; 73android_id = androidId; 74buildUiautomator(ctsTestCasePath); 75 76System.out.println("*******************"); 77System.out.println("---FINISH DEBUG----"); 78System.out.println("*******************"); 79 80} 81// 运行步骤 82private void runUiautomator() { 83creatBuildXml(); 84modfileBuild(); 85buildWithAnt(); 86if (System.getProperty("os.name").equals("Linux")) { 87pushTestJar(workspace_path + "/bin/" + jar_name + ".jar"); 88}else{ 89pushTestJar(workspace_path + "\\bin\\" + jar_name + ".jar"); 90} 91 92if (test_name.equals("")) { 93runTest(jar_name, test_class); 94return; 95} 96runTest(jar_name, test_class + "#" + test_name); 97} 98 99 100// 1--判断是否有build 101public boolean isBuild() { 102File buildFile = new File("build.xml"); 103if (buildFile.exists()) { 104return true; 105} 106// 创建build.xml 107execCmd("cmd /c android create uitest-project -n " + jar_name + " -t " 108+ android_id + " -p " + workspace_path); 109return false; 110} 111 112// 创建build.xml 113public void creatBuildXml() { 114execCmd("cmd /c android create uitest-project -n " + jar_name + " -t " 115+ android_id + " -p " + "\""+workspace_path+ "\""); 116} 117 118// 2---修改build 119public void modfileBuild() { 120StringBuffer stringBuffer = new StringBuffer(); 121try { 122File file = new File("build.xml"); 123if (file.isFile() & & file.exists()) { // 判断文件是否存在 124InputStreamReader read = new InputStreamReader( 125new FileInputStream(file)); 126BufferedReader bufferedReader = new BufferedReader(read); 127String lineTxt = null; 128while ((lineTxt = bufferedReader.readLine()) != null) { 129if (lineTxt.matches(".*help.*")) { 130lineTxt = lineTxt.replaceAll("help", "build"); 131// System.out.println("修改后: " + lineTxt); 132} 133stringBuffer = stringBuffer.append(lineTxt + "\t\n"); 134} 135read.close(); 136} else { 137System.out.println("找不到指定的文件"); 138} 139} catch (Exception e) { 140System.out.println("读取文件内容出错"); 141e.printStackTrace(); 142} 143 144System.out.println("-----------------------"); 145 146// 修改后写回去 147writerText("build.xml", new String(stringBuffer)); 148System.out.println("--------修改build完成---------"); 149} 150 151 152 153// 3---ant 执行build 154public void buildWithAnt() { 155if (System.getProperty("os.name").equals("Linux")) { 156execCmd("ant"); 157return; 158} 159execCmd("cmd /c ant"); 160} 161 162// 4---push jar 163public void pushTestJar(String localPath) { 164localPath="\""+localPath+"\""; 165System.out.println("----jar包路径: "+localPath); 166String pushCmd = "adb push " + localPath + " /data/local/tmp/"; 167System.out.println("----" + pushCmd); 168execCmd(pushCmd); 169} 170 171// 运行测试 172public void runTest(String jarName, String testName) { 173String runCmd = "adb shell uiautomator runtest "; 174String testCmd = jarName + ".jar " + "--nohup -c " + testName; 175System.out.println("----runTest:" + runCmd + testCmd); 176execCmd(runCmd + testCmd); 177} 178 179public String getWorkSpase() { 180File directory = new File(""); 181String abPath = directory.getAbsolutePath(); 182return abPath; 183} 184 185/** 186* 需求:执行cmd命令,且输出信息到控制台 187* @param cmd 188*/ 189public void execCmd(String cmd) { 190System.out.println("----execCmd:" + cmd); 191try { 192Process p = Runtime.getRuntime().exec(cmd); 193//正确输出流 194InputStream input = p.getInputStream(); 195BufferedReader reader = new BufferedReader(new InputStreamReader( 196input)); 197String line = ""; 198while ((line = reader.readLine()) != null) { 199System.out.println(line); 200saveToFile(line, "runlog.log", false); 201} 202//错误输出流 203InputStream errorInput = p.getErrorStream(); 204BufferedReader errorReader = new BufferedReader(new InputStreamReader( 205errorInput)); 206String eline = ""; 207while ((eline = errorReader.readLine()) != null) { 208System.out.println(eline); 209saveToFile(eline, "runlog.log", false); 210} 211} catch (IOException e) { 212e.printStackTrace(); 213} 214} 215/** 216* 需求:写如内容到指定的文件中 217* 218* @param path 219*文件的路径 220* @param content 221*写入文件的内容 222*/ 223public void writerText(String path, String content) { 224 225File dirFile = new File(path); 226 227if (!dirFile.exists()) { 228dirFile.mkdir(); 229} 230 231try { 232// new FileWriter(path + "t.txt", true) 这里加入true 可以不覆盖原有TXT文件内容 续写 233BufferedWriter bw1 = new BufferedWriter(new FileWriter(path)); 234bw1.write(content); 235bw1.flush(); 236bw1.close(); 237} catch (IOException e) { 238e.printStackTrace(); 239} 240} 241 242public void saveToFile(String text,String path,boolean isClose) { 243File file=new File("runlog.log"); 244BufferedWriter bf=null; 245try { 246FileOutputStream outputStream=new FileOutputStream(file,true); 247OutputStreamWriter outWriter=new OutputStreamWriter(outputStream); 248bf=new BufferedWriter(outWriter); 249bf.append(text); 250bf.newLine(); 251bf.flush(); 252 253if(isClose){ 254bf.close(); 255} 256} catch (FileNotFoundException e1) { 257e1.printStackTrace(); 258} catch (IOException e) { 259e.printStackTrace(); 260} 261 262 263} 264/** 265* 需求:编译和复制jar包指定文件 266* @param newPath 267*/ 268private void buildUiautomator(String newPath) { 269creatBuildXml(); 270modfileBuild(); 271buildWithAnt(); 272//复制文件到指定文件夹 273copyFile(workspace_path + "\\bin\\" + jar_name + ".jar", newPath); 274 275} 276/** 277* 复制单个文件 278* @param oldPath String 原文件路径 如:c:/fqf.txt 279* @param newPath String 复制后路径 如:f:/fqf.txt 280* @return boolean 281*/ 282public void copyFile(String oldPath, String newPath) { 283System.out.println("源文件路径:"+oldPath); 284System.out.println("目标文件路径:"+newPath); 285try { 286int bytesum = 0; 287int byteread = 0; 288File oldfile = new File(oldPath); 289if (oldfile.exists()) { //文件存在时 290InputStream inStream = new FileInputStream(oldPath); //读入原文件 291FileOutputStream fs = new FileOutputStream(newPath); 292byte[] buffer = new byte[1444]; 293int length; 294while ( (byteread = inStream.read(buffer)) != -1) { 295bytesum += byteread; //字节数 文件大小 296System.out.println(bytesum); 297fs.write(buffer, 0, byteread); 298} 299inStream.close(); 300} 301} 302catch (Exception e) { 303System.out.println("复制单个文件操作出错"); 304e.printStackTrace(); 305 306} 307 308} 309 }
UiAutomatorHelper【Android UiAutomator 快速调试】
推荐阅读
- Android减包 - 减少APK大小
- BaseProject快速构建自己的APP
- android自定义编解码器记录
- Netapp在模拟器中使用disk removeowner报错
- Android属性动画初步学习笔记
- Android 4.4 全套源代码及子模块源代码的下载方法
- hadoop2.4 支持snappy
- Android 透明度百分比对应的 十六进制
- Android开发之《异常处理》