Android 基于uiautomator测试(五) -- UiObject使用

  • UiObject类说明:

    • 功能:代表一个组件对象,对象有很多模拟实际操作手机的方法和属性
  • 点击与长按:

    • 组件区域位置关系:
      • rect对象代表一个矩形区域:[left,Top] [Right, Bottom]
  • 点击长按示例:

public void testK(){    
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiObject browserObject = new UiObject(new UiSelector().text("浏览器"));    
    try {        
        //这样电机的方位为中点        
        browserObject.click();        
        uiDevice.pressHome();        
        //延时打开5秒内打开正常        
        browserObject.clickAndWaitForNewWindow(5000);        
        uiDevice.pressHome();        
        //点击不同方位        
        //右上角        
        browserObject.clickBottomRight();        
        uiDevice.pressHome();        
        //点击左上角        
        browserObject.clickTopLeft();        
        //长按        
        browserObject.longClick();        
        //长按第二种实现,步长要大        
        uiDevice.swipe(533,612,  535,  612,300);    
    } catch (UiObjectNotFoundException e) {        
        e.printStackTrace();    
    }
}
  • 推拽与滑动示意图:

public void testL() throws UiObjectNotFoundException {    
    UiDevice uiDevice =     UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    //UiObject object = new UiObject(new UiSelector().text("联系人"));    
    //[216,1602][432,1878]    //拖拽到某一个位置    
    //UiObject object1 = new UiObject(new UiSelector().text("微信"));         
    //object.dragTo(350,1200, 10);    
    //拖拽到微信上    
    //object.dragTo(object1, 30);    
    //向左滑动,(同一类型的列表里面的id基本上相同)    
    UiObject object2 = new UiObject(new UiSelector().resourceId("com.android.systemui:id/task_view_thumbnail"));    
    object2.swipeLeft(10);
}


  • 输入与清除文本

  • 输入文本只能输入英文不能输入中文

  • 输入文本:清除文本->输入文本

  • 清除文本:长按->清除(有些编辑框会出错)

public void testQ() throws UiObjectNotFoundException {    
    UiDevice uiDevice =
    UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    //com.android.mms:id/recipients_editor_to    
    UiObject editObject = new UiObject(new
    UiSelector().resourceId("com.android.mms:id/recipients_editor_to"));    
    UiObject uiObject = new UiObject(new UiSelector().text("输入收件人"));    
    //无文字的情况下输入    
    //已有文字的情况下输入(先删除后输入)    
    editObject.setText("Hello wjx");    
    //editObject.clearTextField();    
    //将光标移动到行尾使用backspace键来删除    
    while(!uiObject.exists()) {        
        uiDevice.pressKeyCode(KeyEvent.KEYCODE_MOVE_END);        
        uiDevice.pressKeyCode(KeyEvent.KEYCODE_DEL);    
    }
}
  • 获取对象的属性:
UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject editObject = new UiObject(new 
UiSelector().resourceId("com.android.mms:id/recipients_editor_to"));
String text = editObject.getText();System.out.println(text);
System.out.println(editObject.getClassName());
System.out.println(editObject.getPackageName());
System.out.println(editObject.getContentDescription());
System.out.println(editObject.getBounds());assertEquals("输入收件人",text);


  • 获取父类与子类的节点:
 UiDevice uiDevice = 
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());        
UiObject down = new UiObject(new 
UiSelector().className("android.widget.RelativeLayout").index(0));        
UiObject doenload = down.getChild(new 
UiSelector().resourceId("com.sec.android.app.myfiles:id/text1"));
//        doenload.click();        
UiObject music = down.getFromParent(new 
UiSelector().resourceId("android.widget.RelativeLayout").index(2));        
music.click();

//打开开关
public void testR() throws UiObjectNotFoundException {    
    UiDevice uiDevice = 
    UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiObject uiObject = new UiObject(new 
    UiSelector().className("android.widget.Switch"));    
    if(!uiObject.isChecked()){        
        uiObject.click();    
    }
}

在这里插入图片描述
在这里插入图片描述

  • 手势的操作:

    • 两指手势
    • 多指手势
    • 两指合拢
    • 两指扩张
      ad1477425050820b10f57127882adee3.png
  • 双指扩张:

//双指扩张
public void testGesture() throws UiObjectNotFoundException {    
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiObject uiObject = new UiObject(new 
    UiSelector().className("android.widget.FrameLayout"));    
    //80%表示的是对角线的百分比    
    uiObject.pinchOut(80, 20);
}
  • 两点操作:
//两点手势,两个手指两条直线两个点
public void testTwoPoint() {    
    UiDevice uiDevice =     UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiObject uiObject = new UiObject(new  UiSelector().className("android.widget.FrameLayout"));    
    Point startPoint1, startPoint2, endPoint1, endPoint2;    
    startPoint1 = new Point();    
    startPoint2 = new Point();    
    endPoint1 = new Point();    
    endPoint2 = new Point();    
    startPoint1.x = 157;    
    startPoint1.y = 183;    
    startPoint2.x = 122;    
    startPoint2.y = 455;    
    endPoint1.x = 948;    
    endPoint1.y = 193;    
    endPoint2.x = 930;    
    endPoint2.y = 493;    
    uiObject.performTwoPointerGesture(startPoint1,startPoint2,endPoint1,endPoint2, 50);
}
  • 判断对象是否存在:
//判断对象是否存在
public void testExists() throws UiObjectNotFoundException {    
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());    
    UiObject uiObject = new UiObject(new UiSelector().text("信息"));    
    if(uiObject.exists()) {        
        uiObject.click();    
    }    
    UiObject uiObject1 = new UiObject(new UiSelector().text("轻敲以添加优先发件人")); 
    if(uiObject1.waitForExists(5000)){
        System.out.println("没有信息");    
    }
}


©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值