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 ...
随机推荐
- Neo4j数据库简单
作为世界上先进的地图数据库,Neo4j如今,公司已成为许多互联网的首选.Neo4j它是基于java开源地图数据库开发,另外一个NoSQL数据库.Neo4j在保证对数据关系的良好刻画的同一时候.还支持传 ...
- Android 读取Assets中图片
bgimg0 = getImageFromAssetsFile("Cat_Blink/cat_blink0000.png"); * * 从Assets中读取图片 */ privat ...
- .Net 数组去除重复项
string str = "1/1/12/13/15/16/15//"; ] { '/' }, StringSplitOptions.RemoveEmptyEntries); Li ...
- Replace不区分大小写
private string ReplaceNoCase(string text, string oldValue, string newValue) { return System.Text.Reg ...
- asp.net 下载文件(图片、word、excel等)
string filePath = Server.MapPath("~/excel.xlsx"); if (File.Exists(filePath)) { FileStream ...
- Excel Sheet Row Numbers
Given the sequence S1 = {a,b,c,d,…,x,y,z,aa,ab,ac…. } and given that this sequence corresponds (term ...
- isalpha函数,判断字符是否是字母
头文件:<iostream> or <cctype> 在c语言中<ctype.h> 功能:判断一个字符是否是英文字符,是大写返回1,是小写返回2,不是英文字符返 ...
- 原生js写的一个当前年份日期星期和时间的显示
话不多说,所有代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...
- PHP合并数组+与array_merge的区别分析
主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 1)键名为数字时,array_merge()不会覆盖掉原来的值,但+合并数组则会把最先出现的值作为最终结果返回,而把后面 ...
- 各种排序算法(C语言)
#include <stdlib.h> #include <stdio.h> void DataSwap(int* data1, int* data2) { int temp ...