- UiObject 類介紹
代表一個組件對象,對象有許多模擬實際操作手機的方法與屬性 - 點擊與長按對象
(1)組件區域位置關係
(2)點擊長按相關 api
public void testClick() throws UiObjectNotFoundException{
UiObject clock=new UiObject(new UiSelector()
.resourceId("com.android.deskclock:id/analog_appwidget"));
//clock.click();
//clock.clickAndWaitForNewWindow();
// clock.clickBottomRight();
// sleep(3000);
// UiDevice.getInstance().pressBack();
// sleep(1000);
// clock.clickTopLeft();
// sleep(3000);
// UiDevice.getInstance().pressBack();
// sleep(1000);
//longclick
//clock.longClick();
UiDevice.getInstance().swipe(533, 612, 535, 615, 500);
}
- 拖拽與滑動文本
(1)拖動組件示意圖
(2)拖拽文本相關 api
public void testDrag() throws UiObjectNotFoundException{
// UiObject object1=new UiObject(new UiSelector().text("聯繫人"));
// UiObject object2=new UiObject(new UiSelector().text("圖庫"));
// //object1.dragTo(350,1704-500, 10);
// //object1.dragTo(object2, 30);
// object1.swipeUp(5);
UiObject recent=new UiObject(new UiSelector()
.resourceId("com.android.systemui:id/app_thumbnail_image"));
recent.swipeLeft(10);
}
- 輸入文本與清除文本
(1)輸入文本相關 API
public void testSetText() throws UiObjectNotFoundException{
// UiObject edit=new UiObject(new UiSelector()
// .resourceId("com.android.mms:id/embedded_text_editor"));
//
// edit.setText("qwertyuiop");
// sleep(2000);
// edit.clearTextField();
//接收者
UiObject r=new UiObject(new UiSelector()
.resourceId("com.android.mms:id/recipients_editor"));
UiObject w=new UiObject(new UiSelector().text("接收者"));
r.setText("kkkkkkkkkkkkk");
//將光標移動到行尾,使用backspace進行刪除
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END);
while(!w.exists()){
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL);
}
//將光標移動到行首,使用delete鍵來刪除
}
(2)輸入文本與清除文本實現步驟說明
輸入文本:清除文本—輸入文本
清除文本:長按一鍵刪除
5. 獲取對象的屬性與屬性的判斷
(1)獲取對象的屬性
public void testGet() throws UiObjectNotFoundException{
UiObject r=new UiObject(new UiSelector()
.resourceId("com.android.mms:id/recipients_editor"));
String rec=r.getText();
System.out.println("HINT:"+rec);
System.out.println("CLASS:"+r.getClassName());
System.out.println("PACKAGE_Name:"+r.getPackageName());
System.out.println("PACKAGE_Name:"+r.getPackageName());
System.out.println("DESC:"+r.getContentDescription());
System.out.println("RECT:"+r.getBounds().left);
assertEquals("接收者", rec);
}
(2)獲取父類與子類節點
public void testNode() throws UiObjectNotFoundException{
UiObject down=new UiObject(new UiSelector()
.resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item")
.index(3));
// UiObject download=down.getChild(new UiSelector()
// .resourceId("com.cyanogenmod.filemanager:id/navigation_view_item_name"));
//
// download.click();
UiObject music=down.getFromParent(new UiSelector()
.resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item").index(5));
music.click();
}
(3)屬性的判斷
public void testIs() throws UiObjectNotFoundException{
UiObject wlan=new UiObject(new UiSelector()
.resourceId("com.android.settings:id/switchWidget"));
if(!wlan.isChecked()){
wlan.click();
}
}
- 手勢的操作
(1) 手勢相關操作
(2)手勢相關操作 API
public void testGesture() throws UiObjectNotFoundException{
UiObject object=new UiObject(new UiSelector()
.resourceId("com.android.gallery3d:id/photopage_bottom_controls"));
//object.pinchIn(80, 20);
//object.pinchOut(80, 20);
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=195;
endPoint2.x=930;endPoint2.y=493;
object.performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, 50);
}
- 判斷對象是否存在
相關 API
public void testExist() throws UiObjectNotFoundException{
UiObject l=new UiObject(new UiSelector().text("聯繫人"));
if(l.exists()){
l.click();
}
UiObject create=new UiObject(new UiSelector().text("創建新聯繫人"));
if(create.waitForExists(5000)){
System.out.println("沒有任何聯繫人,需要創建");
create.click();
//創建步驟。。。。
}
}