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){
            
        }
    }

原文地址:https://www.cnblogs.com/kllay/p/5692094.html