Android 基于Uiautomator 测试(四)--基本API

  • 获取屏幕坐标与坐标点击
    • 手机屏幕的坐标:从左上角开始到右下角
    • DP: 设备独立像素,例如,320像素显示到640像素上要拉升一倍,DP保证同样的控件在不同的涉殴击上显示的比例是一致的
    • Point:代表一个点
public void testC(){        
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    //点击屏幕图标
    uiDevice.click(130,214);       
    //点击中心点
    int height = uiDevice.getDisplayHeight();        
    int width = uiDevice.getDisplayWidth();    
    uiDevice.click(width/2,height/2);        
}    
//获取矩阵区域的坐标
UiObject bushu = new UiObject(new 
UiSelector().resourceId("com.sec.android.app.shealth:id/num_of_steps"));
try {    
    Rect  rect = bushu.getBounds();    
    int x0 = rect.left;    
    int y0 = rect.top;    
    int x1 = rect.right;    
    int y1 = rect.bottom;    
    System.out.println("[" + x0+","+y0+"]");    
    System.out.println("[" + x1+","+y1+"]");    
    uiDevice.click(rect.centerX(), rect.centerY());
} catch (UiObjectNotFoundException e) 
{    
    e.printStackTrace();
}


  • 拖拽与滑动:
    • 拖拽:将一个组件从坐标移动到另一个坐标处
    • 滑动:从一个坐标点移动到另一个坐标点
    • 步长:从一点到另一个点的使用的时间,一步长为5毫秒
public void testD(){        
    //        拖拽[216,1602][432,1878]         
    int startx, starty, endx, endy, steps;        
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());        
    startx = (432-216)/2+252;        
    starty = (1878-1602)/2+1602;        
    endx = startx;        
    endy = starty;        
    //时间越长,越慢        
    steps = 10;        
    uiDevice.drag(startx,starty, endx, endy, steps);                
    
    
    //滑动屏幕        
    int w = uiDevice.getDisplayWidth();        
    int h = uiDevice.getDisplayHeight();        
    uiDevice.swipe(w-50, h/2, 50, h/2, 10);
    //        画一个矩形  277,318  746,335  784,814  221,840        
    Point point1 = new Point();        
    Point point2 = new Point();        
    Point point3 = new Point();        
    Point point4 = new Point();        
    Point point5 = new Point();        
    point1.x = 277;        
    point1.y = 318;        
    point2.x = 746;        
    point2.y =  335;        
    point3.x = 784;       
    point3.y = 814;        
    point4.x = 221;        
    point4.y = 840;        
    point5.x = 277;        
    point5.y = 318;        
    Point[] points ={point1, point2,point3, point4,point5};        
    uiDevice.swipe(points, 50);    
}


  • 旋转屏幕:
    • 旋转方向:4个方向,分别为0,90, 180 ,270度
    • 重力感应器
    • 固定位置与物理旋转
public void testE() throws RemoteException {    
    //向左转并固定    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    if(uiDevice.isNaturalOrientation()){        
        uiDevice.setOrientationLeft();    
    }    
    
    //返回当前旋转度数    
    int a = uiDevice.getDisplayRotation();    
    if(a== Surface.ROTATION_0){
    
    }else if(a==Surface.ROTATION_90){
    
    }else if(a==Surface.ROTATION_180){
    
    }else if(a==Surface.ROTATION_270){ 
    }
}


  • 灭屏与唤醒屏幕:
public void testF() {    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    try {        
        if (uiDevice.isScreenOn()) { 
            uiDevice.sleep();        
         }        
         uiDevice.wakeUp();    
     } catch (RemoteException e) { 
        e.printStackTrace();    
      }
 }


  • 截图与等待空闲:
    • 图片缩放比例
    • 图片的质量
    • FIle类
    • 图片格式:PNG
    • 空闲状态:界面处于不动
    • 窗口更新事件:打开的过程叫做窗口更新,不同了就叫做窗口空闲
public void testG(){    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    uiDevice.takeScreenshot(new File("Download"));    
    uiDevice.pressHome();    
    //获取浏览器对象,并单击    
    UiObject browserObject = new UiObject(new UiSelector().text("浏览器"));    try {        
                browserObject.clickAndWaitForNewWindow();        
        //如果在2秒内没有打开就会出现异常 
        uiDevice.waitForIdle(2000);    
        } catch (UiObjectNotFoundException e) {        
        e.printStackTrace();    
        }
  }


  • 包名,通知栏,快速设置,布局文件等:
public void testH(){    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    //获取当前包名
    String name = uiDevice.getCurrentPackageName();     
    System.out.println(name);    
    //打开通知栏
    uiDevice.openNotification();  
    //打开快速设置
    uiDevice.openQuickSettings();
    //获取当前布局文件  
    uiDevice.dumpWindowHierarchy("n.xml");


    }
    导出布局文件:
   adb shell
   cd data/local/tmp
   ls
   cat n.xml
   adb pull /data/local/tmp/n/xml I:\



  • 例子:
public void testJ() throws RemoteException {    
    UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    if(!uiDevice.isScreenOn()) {        
        uiDevice.wakeUp();    
    }else {        
        uiDevice.sleep();        
        uiDevice.wakeUp();    
    }   
    //滑屏解锁    
    uiDevice.swipe(0, 1496, 1080, 1496,200);    
    uiDevice.pressHome();    
    UiObject browserObject = new UiObject(new UiSelector().text("浏览器"));    
    try {        
        browserObject.clickAndWaitForNewWindow();       
        //如果在2秒内没有打开就会出现异常        
        uiDevice.waitForIdle(2000);    
    } catch (UiObjectNotFoundException e) {        
        e.printStackTrace();    
    }    
    uiDevice.click(546, 141);    
    uiDevice.pressDelete();    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_W);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_W);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_W);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_PERIOD);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_B);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_A);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_I);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_D);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_U);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_PERIOD);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_C);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_O);    
    uiDevice.pressKeyCode(KeyEvent.KEYCODE_M);    
    uiDevice.pressEnter();    
    uiDevice.setOrientationLeft();    
    uiDevice.takeScreenshot(new File("/sdcard/brower.png"));}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值