六、UiObejct API 詳細介紹

一、點擊與長按

1.組件區域位置關係:

Rect 對象代表一個矩形區域:[left,Top][ARight,Bottom](即左上角圖標到右下角圖標)

2.點擊與長按相關API:

返回值 API 說明
boolean click() 點擊對象
boolean clickAndWaitForNewWindow(long time out) 點擊對象,等待新窗口出現,參數爲等待超時時常
boolean clickAndWaitForNewWindow() 點擊對象,等待新窗口出現(默認超時5.5s)
boolean clickBottomRight() 點擊對象的右下角
boolean clickTopLeft() 點擊對象的左上角
boolean longClick() 對對象執行長按操作
boolean longClickBottomRight() 長按對象的右下角
boolean longClickTopLeft() 長按對象的左上角
  • 例如:
複製代碼
public void testDemo1() throws UiObjectNotFoundException{

    UiObject clock=new UiObject(new UiSelector()
    .resourceId("com.android.deskclock:id/analog_appwidget"));

    clock.click();//點擊對象中點  
    UiDevice.getInstance().pressBack();

    clock.clickAndWaitForNewWindow(10000);//點擊對象,等待新窗口出現,時間爲10s
    UiDevice.getInstance().pressBack();

    clock.clickAndWaitForNewWindow();//點擊對象,等待新窗口出現,時間爲默認時間5.5s
    UiDevice.getInstance().pressBack();

    clock.clickBottomRight();//點擊右下角
    UiDevice.getInstance().pressBack();

    clock.clickTopLeft();//點擊左上角
    UiDevice.getInstance().pressBack();

    clock.longClick();//長按(如果想要增加長按時間可以使用swipe)

}
複製代碼

 

 

二、拖拽與滑動

//可以拖拽到一個點或一個組件上;可以上下左右滑動一段距離

1.拖拽與滑動相關API

返回值 API 說明
boolean dragTo(UiObject destObj,int steps) 拖拽對象到另一對象位置上
boolean dragTo(int destX,int destY,int steps) 拖拽對象到屏幕某個座標位置上
boolean swipeDown(int steps) 拖拽對象向下滑動
boolean swipeLeft(int steps) 拖拽對象向左滑動
boolean swipeRight(int steps) 拖拽對象向右滑動
boolean swipeUp(int steps) 拖拽對象向上滑動
  • 例如:
複製代碼
public void testDemo2() throws UiObjectNotFoundException, RemoteException{

    UiObject object1=new UiObject(new UiSelector().text("相機"));
    //將對象拖拽到指定座標
    object1.dragTo((128-64)/2+64,417-100, 20);
    //將對象拖拽到指定對象位置上
    UiObject object2=new UiObject(new UiSelector().text("短信"));
    object1.dragTo(object2, 5);//補償越短越容易出現兩個對象互換位置的情況


    //swipe(殺進程操作演示)
    UiDevice.getInstance().pressRecentApps();//點開最近使用程序員
    UiObject object3=new UiObject(new UiSelector()
    .resourceId("com.android.systemui:id/app_thumbnail_image"));
    object3.swipeLeft(10);//向左滑動關閉進程

}
複製代碼

 

三、輸入文本與清除文本

返回值 API 說明
boolean setText(String text) 在對象中輸入文本(只能輸入英文)
void clearTextFiled() 清除編輯框中的文本
  • 示例一:
複製代碼
public void testDemo3() throws UiObjectNotFoundException{

    UiObject setText=new UiObject(new UiSelector()
    .resourceId("com.android.mms:id/embedded_text_editor"));

    setText.setText("123abc");//輸入文本
    sleep(2000);
    setText.clearTextField();//清除文本
複製代碼

 


注意事項:

1.他的輸入步驟是先長按來選中要輸入的文本框,然後輸入文本,但是若在文本框已有文本的情況下輸入文本會丟失原文本,也就是說他會先長按要輸入的文本框,這樣就執行了全選操作,然後再進行輸入文本的操作,這樣原先的文本就沒有了

2.他的清除步驟是先長按來全選文本—然後點Del鍵

3.可能不同的輸入框會出現不同的情況,解決方法參考示例二


  • 示例二:
複製代碼
public void testDemo3() throws UiObjectNotFoundException{

    //獲取接收者
    UiObject i=new UiObject(new UiSelector()
    .resourceId("com.android.mms:id/recipients_editor"));

    UiObject i1=new UiObject(new UiSelector().text("接收者"));

    i.setText("iiiiiiiiii");//輸入文本"iiiiiiii"

    //將光標移動至行尾,然後循環刪除,直至文本框變爲默認的"接受者"爲止

    while (!i1.exists()){
        UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL);
    }
}
複製代碼

 

四、獲取對象屬性與屬性判斷

1.獲取對象的屬性

返回值 API 說明
Rect getBounds() 獲得對象矩形座標,矩形左上角座標與右下角座標
int getChildCount() 獲得下一級子類數量
String getClassName() 獲得對象類名屬性的類名文本
String getContentDescription() 獲得對象的描述屬性的描述文本
String getPackageName() 獲得對象包名屬性的包名文本
String getText() 獲得對象的文本屬性中的文本
Rect getVisibleBounds() 返回可見視圖的範圍,如果視圖部分是可見的,只有可見部分報告的範圍
  • 例如:
複製代碼
public void testDemo4() throws UiObjectNotFoundException{

    // 獲取信息欄的好友框
    UiObject i=new UiObject(new UiSelector().resourceId("com.android.mms:id/recipients_editor"));

    String rec=i.getText();//獲取文本
    System.out.println("Text is : "+rec);//輸出文本
    assertEquals("接收者", rec);//對比文本(預期值,實際值)

    System.out.println("Class name is  : "+i.getClassName());//輸出並輸出類名
    System.out.println("Child cound is : "+i.getChildCount());//輸出下一級子類數量
    System.out.println("Bound left is : "+i.getBounds().left);//輸出對象矩形左座標
    System.out.println("Content description is : "+i.getContentDescription());//輸出對象的描述屬性的描述文本
    System.out.println("Package name is : "+i.getPackageName());//輸出對象包名
    System.out.println("Visible bounds right is  : "+i.getVisibleBounds().right);//輸出可見範圍的右邊視圖

}
複製代碼

 

2.獲取父類與子類節點

返回值 API 說明
UiObject getChild(UiSelector selector) 獲得對象的子類對象,可以遞歸獲取子孫當中某個對象
UiObject getFromPrent(UiSelector selector) 從父類獲取子類,按照UiSelector獲取兄弟類(遞歸)
  • 例如:
複製代碼
public void testDemo5() throws UiObjectNotFoundException{
    //getChild()
    //定位父類
    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();
    sleep(2000);
    UiDevice.getInstance().pressBack();


    //getFromParent()
    定位兄弟父類
    UiObject down =new UiObject(new UiSelector()
    .resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item")
    .index(3));
    //定位要點擊的父類的兄弟類
    UiObject music=down.getFromParent(new UiSelector()
    .resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item")
    .index(5));
    //點擊
    music.click();
}
複製代碼

 

3.屬性的判斷

返回值 API 說明
boolean isCheckable() 檢查對象的checkable屬性是否爲true
boolean isChecked() 檢查對象的checked屬性是否爲true
boolean isClickable() 檢查對象的clickable屬性是否爲true
boolean isEnabled() 檢查對象的enabled屬性是否爲true
boolean isFocusable() 檢查對象的focusable屬性是否爲true
boolean isFocused() 檢查對象的focused屬性是否爲true
boolean isLongClickable() 檢查對象的longclickable屬性是否爲true
boolean isScrollable() 檢查對象的scrollable屬性是否爲true
boolean isSelected() 檢查兌現給的selected屬性是否爲true
  • 例如:
複製代碼
public void testDemo6() throws UiObjectNotFoundException{

    //找到對象
    UiObject wlan=new UiObject(new UiSelector()
    .resourceId("com.android.settings:id/switchWidget")
    .instance(0));
    //如果wlan.isCheckable()爲false則開啓
    if(!wlan.isCheckable()){
        wlan.click();
    }
}
複製代碼

 

五、手勢的操作

1.手勢操作相關API

返回值 API 說明
boolean performMultiPointerGesture(pointerCoords[]…touches) 執行單手指觸控手勢,可定義任意手勢與形狀
boolean performTwoPointerGesture(Point startPoint1,Point startPoint2,Point endPoint1,Point endPoint2,int steps) 執行任意兩個手指指控手勢,模擬兩個手指手勢
boolean pinchIn(int percent,int steps) 手勢操作,兩點向內收縮(百分比,步數)
boolean pinchOut(int percent,int steps) 手勢操作,兩點向外張開(百分比,步數)
  • 例如:
複製代碼
public void testDemo7() throws UiObjectNotFoundException{

    //選中被操作目標
    UiObject web=new UiObject(new UiSelector().className("android.widget.FrameLayout"));
    web.pinchOut(80, 20);//以20步的速度兩點向外擴張80%(也就是放大)
    sleep(2000);
    web.pinchIn(80, 20);//以20步的速度兩點向內擴張80%(也就是縮小)
    //聲明變量
    Point startPoint1,startPoint2,endPoint1,endPoint2;  
    startPoint1 = new Point();
    startPoint2=new Point();
    endPoint1=new Point();
    endPoint2=new Point();
    //自定義起點區域
    startPoint1.x=15;startPoint1.y=156;
    startPoint2.x=30;startPoint2.y=173;
    //自定義終點區域
    endPoint1.x=215;endPoint1.y=156;
    endPoint1.x=230;endPoint1.y=173;
    //模擬兩個手指從左向右滑動
    web.performTwoPointerGesture(startPoint1,startPoint2,endPoint1,endPoint2,20);

}
複製代碼

 

六、判斷對象是否存在

1.判斷對象是否存在相關API

返回值 API 說明
boolean waitForExists(long timeout) 等待對象出現
boolean waitUntilGone(long timeout) 等待對象消失
boolean exists() 檢查對象是否存在
  • 例如:
複製代碼
public void testDemo8() throws UiObjectNotFoundException{

    //點擊聯繫人--判斷是否有聯繫人,沒有則創建聯繫人

    //獲取聯繫人對象
    UiObject LianXiRen=new UiObject(new UiSelector().text("聯繫人"));
    //判斷聯繫對象是否存在,如果存在則點擊
    if(LianXiRen.exists()){
        LianXiRen.click();
    }
    UiObject create=new UiObject(new UiSelector().text("創建新聯繫人"));
    //等待5秒判斷是否有[創建新聯繫人]按鈕,如果有則說明界面內沒有聯繫人
    if(create.waitForExists(5000)){
        System.out.println("沒有聯繫人,需要創建");
        create.click();
    }
    //開始創建步驟...

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章