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