1、 uiaotumator ui測试

Demo.java

package uiautomatorDemo1;

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";//jar包名
testClass="uiautomatorDemo1.Demo";//包名+类名
testName="testBrowser";//功能函数
androidId="1";
new UiAutomatorHelper(jarName, testClass, testName, 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")); } }

2、 UiAutomator高速调试

UiAutomatorHelper.java

package uiautomatorDemo1;

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

uiaotumator ui測试 高速调试的更多相关文章

  1. UI測试内容

    我们在实际工作其中,针对web应用程序,也就是常常所说的B/S系统,能够从例如以下方面来进行用户界面測试: 导航測试 导航描写叙述了用户在一个页面内操作的方式,在不同的用户接口控制之间,比如butto ...

  2. Android单元測试之JUnit

    随着近期几年測试方面的工作慢慢火热起来.常常看见有招聘測试project师的招聘信息.在Java中有单元測试这么一个JUnit 方式,Android眼下主要编写的语言是Java,所以在Android开 ...

  3. iOS自己主动化測试的那些干货

    前言 假设有測试大佬发现内容不正确.欢迎指正,我会及时改动. 大多数的iOS App(没有持续集成)迭代流程是这种 也就是说.測试是公布之前的最后一道关卡.假设bug不能在測试中发现,那么bug 就会 ...

  4. Java web測试分为6个部分

    1.功能測试 2.性能測试(包含负载/压力測试)3.用户界面測试 4. 兼容性測试 5.  安全測试  6.接口測试   1 功能測试 1.1 链接測试 链接測试可分为三个方面. 首先,測试全部链接是 ...

  5. CodedUI自己主动化測试及脱离VS独立执行

    在VS中可创建"编码的UI測试".可录制软件操作,再回放,最后还能够脱离VS独立执行. 在VS中执行測试 创建项目codeuitest,控件布局.例如以下图: 在button单击事 ...

  6. ESP8266学习笔记1:怎样在安信可全功能測试板上实现ESP-01的编译下载和调试

    近期调试用到了安信可的ESP-01模块,最终打通了编译下载调试的整个通道,有一些细节须要记录,方便兴许的开发工作. 转载请注明:http://blog.csdn.net/sadshen/article ...

  7. 【Android开发-6】了解内情,我们须要一些调试和測试手段

    前言:人生不可能十全十美,总会有些遗憾存在,经历过遗憾,我们才懂的什么是生活. 程序也一样.追求完美,就必定会有经历bug存在的时候. 经历过不断的bug磨练.我们技术才会不断的成长.对于调试bug, ...

  8. 腾讯云安装openvz,高速搭建測试环境

    CSDN送了腾讯云的測试资格,准备拿来作为cici的软件公布首页,想在上面做个demo,无奈没有设备环境,于是想要用openvz来虚拟一些vps: 第一步:选择腾讯云的os模板,centos6.3 第 ...

  9. 測试CPU支持指令集AVX,AVX2,SSE情况的代码【VS2010调试通过】

    完整代码例如以下所看到的 http://download.csdn.net/detail/vbskj/7723827 本人的測试结果 watermark/2/text/aHR0cDovL2Jsb2cu ...

随机推荐

  1. 使用 Bullet,BulletManager 在 XNA 中创建子弹攻击目标(十五)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  2. pythonic编程示例及简析

    1.列表 list[起始:结尾:增量] 值传递与地址传递 a = [2,1] b = a #地址传递 b = a[:] 值传递 a.sort() print a #[1,2] print b #[1, ...

  3. python实现算法: 多边形游戏 数塔问题 0-1背包问题 快速排序

    去年的算法课挂了,本学期要重考,最近要在这方面下点功夫啦! 1.多边形游戏-动态规划 问题描述: 多边形游戏是一个单人玩的游戏,开始时有一个由n个顶点构成的多边形.每个顶点被赋予一个整数值, 每条边被 ...

  4. maven学习(一)——maven的下载与安装

    一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1. ...

  5. idea调试查看mybitis发送的语句(引用)

    MyBatis 的配置文件 Configuration 中有相关属性,设属性即可,不用再放一个 log4j.properties 文件. <configuration> <setti ...

  6. ssh-add Could not open a connection to your authentication agent.

    ssh-add 报错Could not open a connection to your authentication agent. 需要执行以下代码 eval `ssh-agent -s` ssh ...

  7. nginx访问控制allow、deny(ngx_http_access_module)

    单看nginx模块名ngx_http_access_module,很多人一定很陌生,但是deny和allow相比没一个人不知道的,实际上deny和allow指令属于ngx_http_access_mo ...

  8. REDIS基础笔记

    Redis基础笔记 资源链接 简介 简介 安装 五种数据类型及相应命令 1. 字符串类型 2. 散列类型 3. 列表类型 4. 集合类型 5. 有序集合 其他 事务 SORT 生存时间 任务队列 发布 ...

  9. BZOJ-3190 [JLOI2013]赛车

    转成二元一次不等式组,然后半平面交. #include <cstdlib> #include <cstdio> #include <cmath> #include ...

  10. GDOI2018前夕 错误总结

    算法易错点 $FFT$ 1.注意精度,以及是否取整 2.注意$complex$类不要写错,复数乘法是这样的: complex operator *(const complex &b){retu ...