Android UiAutomator环境配置与编译运行
2016-10-11 14:46
531 查看
一.新建测试工程
1 、打开Eclipse
2 、新建一个java工程,包
3 、增加build path
4 、 新建测试类,继成UiAutomatorTestCase
5 、 编写用例,方法名必须test开头
6 、 编译与运行
二.编译与运行测试代码
1.创建build文件
2.修改build文件
进入工作空间,打开build.xml文件,将第二行的help修改为build
3.开始编译
三.封装类
使用实例:
点击下载源代码
1 、打开Eclipse
2 、新建一个java工程,包
3 、增加build path
4 、 新建测试类,继成UiAutomatorTestCase
5 、 编写用例,方法名必须test开头
6 、 编译与运行
二.编译与运行测试代码
1.创建build文件
android create uitest-project -n <jar name> -t 1 -p <workspace path>
2.修改build文件
进入工作空间,打开build.xml文件,将第二行的help修改为build
3.开始编译
ant –buildfile <build.xml 文件路径>4.push文件
adb push <path_to_output_jar> /data/local/tmp/5.运行测试
adb shell uiautomator runtest <jar name>-c <包名>.<类名>[#test name]
三.封装类
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("*******************"); } // 运行步骤 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(); } } }
使用实例:
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 Demo extends UiAutomatorTestCase { public void testDevice(){ // UiDevice.getInstance().pressHome(); // getUiDevice().pressHome(); Demo2 d2=new Demo2(); d2.press(); } public static void main(String[] agrs){ String jarName,testClass,testName,androidId; jarName="Demo"; testClass="com.jikexueyuan.Demo"; testName="testGetPackage"; androidId="1"; new UiAutomatorHelper(jarName, testClass, testName dddc , androidId); } public void testPress(){ //UiDevice.getInstance().pressMenu(); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A,1); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B,1); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C,1); } public void testClick() throws UiObjectNotFoundException{ //UiDevice.getInstance().click(399, 355); // int h=UiDevice.getInstance().getDisplayHeight(); // int w=UiDevice.getInstance().getDisplayWidth(); // UiDevice.getInstance().click(w/2, h/2); UiObject clock=new UiObject(new UiSelector().resourceId("com.android.deskclock:id/analog_appwidget")); Rect r=clock.getBounds(); int x0=r.left; int y0=r.top; int x1=r.right; int y1=r.bottom; int centerx=r.centerX(); int centery=r.centerY(); System.out.println("["+x0+","+y0+"]"); System.out.println("["+x1+","+y1+"]"); } public void testDragAndSwipe(){ //[252,1704][444,1896] // int startX, startY, endX, endY, steps; // startX=(444-252)/2+252; // startY=(1896-1704)/2+1704; // endX=startX; // endY=startY-500; // steps=100; // UiDevice.getInstance().drag(startX, startY, endX, endY, steps); // int h=UiDevice.getInstance().getDisplayHeight(); // int w=UiDevice.getInstance().getDisplayWidth(); // UiDevice.getInstance().swipe(w-50, h/2, 50, h/2, 10); //277,318 746,335 784,814 221,840 Point p1=new Point(); Point p2=new Point(); Point p3=new Point(); Point p4=new Point(); p1.x=277;p1.y=318; p2.x=746;p2.y=335; p3.x=784;p3.y=814; p4.x=221;p4.y=840; Point[] pp={p1,p2,p3,p4}; UiDevice.getInstance().swipe(pp, 50); } public void testOrientation() throws RemoteException{ UiDevice.getInstance().setOrientationLeft(); UiDevice.getInstance().setOrientationRight(); if(UiDevice.getInstance().isNaturalOrientation()){ UiDevice.getInstance().setOrientationLeft(); } int a=UiDevice.getInstance().getDisplayRotation(); if(a==Surface.ROTATION_0){ } if(a==Surface.ROTATION_90){ } if(a==Surface.ROTATION_180){ } if(a==Surface.ROTATION_270){ } } public void testWakeupAndSleep() throws RemoteException{ if(!UiDevice.getInstance().isScreenOn()){ //UiDevice.getInstance().sleep(); UiDevice.getInstance().wakeUp(); } } public void testScreen(){ UiDevice.getInstance().takeScreenshot(new File("/sdcard/test1.png")); } public void testIdle(){ //[828,1704][1020,1896] UiDevice.getInstance().click(950, 1800); UiDevice.getInstance().waitForIdle(20000); } public void testGetPackage(){ String packageName=UiDevice.getInstance().getCurrentPackageName(); System.out.println(packageName); UiDevice.getInstance().openNotification(); sleep(3000); UiDevice.getInstance().openQuickSettings(); UiDevice.getInstance().dumpWindowHierarchy("n.xml"); } public void testBrowser() throws RemoteException{ //灭屏-》亮屏->解锁->单击浏览器->单击网址输入框->输入www.baidu.com->按回车键->旋转屏幕->截图 UiDevice.getInstance().sleep(); sleep(2000); if(!UiDevice.getInstance().isScreenOn()){ UiDevice.getInstance().wakeUp(); } sleep(2000); UiDevice.getInstance().swipe(536, 1496, 1080, 1496, 20); UiDevice.getInstance().pressHome(); UiDevice.getInstance().click(950, 1800); sleep(3000); UiDevice.getInstance().click(589, 151); sleep(2000); UiDevice.getInstance().pressDelete(); sleep(2000); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_PERIOD); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_I); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_D); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_U); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_PERIOD); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_O); sleep(500); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_M); sleep(500); UiDevice.getInstance().pressEnter(); sleep(2000); UiDevice.getInstance().setOrientationLeft(); sleep(2000); UiDevice.getInstance().takeScreenshot(new File("/sdcard/browser.png")); } }
点击下载源代码
相关文章推荐
- Android UiAutomator环境配置与编译运行
- cocos2d_x环境搭建过程和Android编译环境配置和项目创建运行过程
- 第1章 Java基本概念及环境配置——FAQ1.15 如何编译. 运行Java应用程序?
- Android编译环境配置
- 使用Python开发Android应用程序:第一节 在手机上配置Python运行环境
- android编译及环境配置
- Android开发环境搭建:SDK在线离线安装+在Eclipse中配置ADT+第一个Android程序运行
- 在 editplus里 配置 CSharp 的编译运行环境 error
- ubuntu 编译android2.2源码配置环境及jdk5(4)
- 让Python在Android系统上飞一会儿:第一节 在手机上配置Python运行环境
- Android系统开发编译环境配置
- ubuntu 编译android2.2源码配置环境及jdk5(6)
- Editplus配置java编译运行环境
- 在linux 上编译生成windows上运行的exe程序,交叉编译环境的配置( 平台:gentoo linux)
- 让Python在Android系统上飞一会儿:第一节 在手机上配置Python运行环境
- 【Android开发系列】Android开发第一步-----开发环境配置 + 运行sample
- android环境配置之后,编译时提示:The project cannot be built until build path errors are resolved 的解决方案
- ubuntu 编译android2.2源码配置环境及jdk5(1)
- ubuntu 下的java 环境的配置、简单的编译与运行
- Linux JDK配置Java编译、运行环境