Android UiAutomator 自动化测试一些代码实例---新手3
1.打开浏览器,打开百度实例
public void testBrowser() throws RemoteException, UiObjectNotFoundException{
//灭屏幕-亮屏幕--解锁
//灭屏
UiDevice.getInstance().sleep();
sleep(2000);
//判断是否亮屏
if(!UiDevice.getInstance().isScreenOn()){
//亮屏
UiDevice.getInstance().wakeUp();
}
//解锁
//UiDevice.getInstance().swipe(startX, startY, endX, endY, steps);
//点击home键
UiDevice.getInstance().pressHome();
//点击浏览器
UiObject uo=new UiObject(new UiSelector().text("浏览器"));
uo.click();
//点击浏览器输入框
UiObject uo1=new UiObject(new UiSelector().resourceId("com.android.browser:id/url"));
uo1.click();
UiDevice.getInstance().pressDelete();
//输入www.baidu.com
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_W);
//UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL);//点
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_PERIOD);//点
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_I);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_D);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_U);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_PERIOD);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_O);
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_M);
sleep(1000);
//回车确认
UiDevice.getInstance().pressEnter();
sleep(1000);
//旋转屏幕
UiDevice.getInstance().setOrientationLeft();
//返回为正常状态
sleep(2000);
UiDevice.getInstance().setOrientationNatural();
//截图
File storePath=new File("/sdcard/testshili.png");
UiDevice.getInstance().takeScreenshot(storePath);
}
2.一些实例:
//获取包名
System.out.println(UiDevice.getInstance().getCurrentPackageName());
//打开通知栏
UiDevice.getInstance().openNotification();
//打开快速设置
UiDevice.getInstance().openQuickSettings();
//获取xml布局文件 /data/local/tmp 存放位置
UiDevice.getInstance().dumpWindowHierarchy("abc.xml");
//[5,390][147,617]
UiDevice.getInstance().click(140, 600);
//20秒
UiDevice.getInstance().waitForIdle(20000);
//截图
public void testJieTu(){
File storePath=new File("/sdcard/test.png");
UiDevice.getInstance().takeScreenshot(storePath);
}
public void testKey() throws UiObjectNotFoundException{
//小写字母
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A);
//大写字母
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B,1);
//点击固定点
UiDevice.getInstance().click(200, 830);
//获取屏幕高和宽
int h=UiDevice.getInstance().getDisplayHeight();
int w=UiDevice.getInstance().getDisplayWidth();
UiDevice.getInstance().click(w/2, h/2);
//获取登录对象
UiObject uiobj=new UiObject(new UiSelector().resourceId("com.zhanglb.yijiebao:id/denglu"));
//获取矩形坐标
Rect r=uiobj.getBounds();
int x0=r.left;
int y0=r.top;
int x1=r.right;
int y1=r.bottom;
//矩形坐标终点
int centx=r.centerX();
int centy=r.centerY();
System.out.println("["+x0+":"+y0+"]");
System.out.println("["+x1+":"+y1+"]");
}
public void testDragAndSwipe(){
//[5,390][147,617]
// int startX, startY, endX, endY, steps;
// startX=(147-5)/2+5;
// startY=(617-390)/2+390;
// endX=startX;
// endY=startY+200;
// steps=10;
// //拖拽
// UiDevice.getInstance().drag(startX, startY, endX, endY, steps);
// sleep(2000);
// //拖拽
// UiDevice.getInstance().drag(startX, endY, endX, startY, steps);
// //获取屏幕高和宽 滑动
// int h=UiDevice.getInstance().getDisplayHeight();
// int w=UiDevice.getInstance().getDisplayWidth();
// UiDevice.getInstance().swipe(w-50, h/2, 50, h/2, 10);
//矩形数组滑动
Point p1=new Point();
Point p2=new Point();
Point p3=new Point();
Point p4=new Point();
p1.x=234;p1.y=345;
p2.x=334;p2.y=535;
p3.x=534;p3.y=345;
p4.x=234;p4.y=145;
Point[] pp={p1,p2,p3,p4};
UiDevice.getInstance().swipe(pp, 50);
}
//旋转屏幕
public void testXuanzhuan() throws RemoteException{
//向左旋转
UiDevice.getInstance().setOrientationLeft();
sleep(2000);
UiDevice.getInstance().setOrientationRight();
//判断是否为正常方向
if(UiDevice.getInstance().isNaturalOrientation()){
UiDevice.getInstance().setOrientationLeft();
}
//判断是否为正常状态,0 0 ;1 90;2 108;3 270
int a=UiDevice.getInstance().getDisplayRotation();
if(a==Surface.ROTATION_0){
}
}
Android UiAutomator 自动化测试一些代码实例---新手3的更多相关文章
- Android UiAutomator 自动化测试编译运行---新手2
1.首先打开eclipse创建java项目
- Android UiAutomator 自动化测试环境搭建---新手1
1.首先需要准备的工具有 1.java jdk 2. android开发工具 adt 3.ant 安装包(如果下载adt里面有) 2.首先安装java环境,jdk这个百度就可以了. 3.android ...
- 【转】Python + Android + Uiautomator自动化测试
1.首先来介绍下UIAutomator工具 UIAutomator是Android官方推出的安卓应用界面自动化测试工具,是最理想的针对APK进行自动化功能回归测试的利器. 2.UIAutomator测 ...
- Android UIAutomator自动化测试
描述:UiAutomator接口丰富易用,可以支持所有Android事件操作,事件操作不依赖于控件坐标,可以通过断言和截图验证正确性,非常适合做UI测试. UIAutomator不需要测试人员了解代码 ...
- 【项目实例】android开发游戏音效代码实例
//音效的音量 int streamVolume; //定义SoundPool 对象 private SoundPool soundPool; //定义HASH表 private HashMap< ...
- Appium python自动化测试系列之Android UIAutomator终极定位(七)
android uiautomator text定位 可能有人不知道为什么说android uiautomator是终极定位,而且android uiautomator和appium有什么关系呢?如果 ...
- 通过实例介绍Android App自动化测试框架--Unittest
1.为什么需要使用框架实现自动化测试 作为测试工程师,可能在代码能力上相比开发工程师要弱一点,所以我们在写脚本的时候就会相对容易的碰到更多的问题,如果有一个成熟的框架供给我们使用的话,可以帮助我们避免 ...
- Android UiAutomator
UiAutomator是一个做UI测试的自动化框架.<Android自动化测试框架>中已有详细介绍,这里就不再累赘了. 一.首先了解自动化测试流程 自动化需求分析 测试用例设计 自动化框架 ...
- UiAutomator自动化测试框架介绍
UiAutomator自动化测试框架介绍 环境搭建 1 必要条件 1.1 JDK 1.2 SDK(API高于15) 1.3 Eclipse 2 ...
随机推荐
- Android文件存储使用参考
可能遇到的问题 android系统自身自带有存储,另外也可以通过sd卡来扩充存储空间.前者好比pc中的硬盘,后者好移动硬盘. 前者空间较小,后者空间大,但后者不一定可用. 开发应用,处理本地数据存取时 ...
- ceph 块设备
数据的存储设备? 数据的存储有3种形式,1种是直接以二进制数据的形式存储在裸设备(包括块设备)上,另外一种是以文件的形式经过文件系统管理进行存储.第三种就是以对象的形式进行对象存储.本篇讨论围绕着块设 ...
- Asp.Net MVC 控制器
原文链接:http://www.asp.net/learn/mvc/ 这篇教程探索了ASP.NET MVC控制器(controller).控制器动作(controller action)和动作结果(a ...
- 像jq那样获取对象的js原生方法
使用过jq的童鞋非常喜欢jq获取对象的方法,只要$()就可以获取,在此我封装一个js获取对象的方法 [注意]只对chrome,Firefox,opera,Safari,ie8及ie8以上版本有效 fu ...
- mysql alter example
alter table `user_movement_log` AFTER `Regionid` (在哪个字段后面添加) ) default null; //主键 ) unsigned not nul ...
- HDU2955-Robberies
描述: The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usual ...
- Python: how to public a model
1, Create a folder fileFolder 2, create a file tester.py 3, create another file setup.py: The conten ...
- SQL Server Mysql 对null值理解的不同
在说到对null值的理解主要是用unique来体现的.也是说null在unique约束看来是一个值还是多个值的问题. 还是开始实验吧. MYSQL create table t(x int ,cons ...
- mysql alter table
准备: create table t(x int,y int); 用法 1: 修改列的数据类 alter table t modify column y nvarchar(32); 用法2: 给表加一 ...
- e.currentTarget
e.target总是指向点击的目标 e.currentTarget会指向这个点击标的冒泡对象 <div class="wrap"> wrap <div class ...