Uiautomator 2.0之UiObject2類學習小記


1. 基礎動作

1.1. 相關API介紹

 

API 說明
clear() 清楚編輯框內的內容
click() 點擊一個對象
clickAndWait(EventCondition<R> condition, long timeout) 點擊一個對象然后等待在超時的時間內條件滿足則通過,否則拋出異常
drag(Point dest, int speed) 自定義速度拖拽這個對象到指定位置
drag(Point dest) 拖拽這個對象到指定位置
longClick() 長按某個對象
scroll(Direction direction, float percent) 對該對象執行一個滾動操作
scroll(Direction direction, float percent, int speed) 自定義速度,對該對象執行一個滾動操作
setText(String text) 設置文本內容
legacySetText(String text) 通過發送keycode,設置文本內容

 

1.2 簡單示例

 1  @Test
 2     public void testCase05() throws InterruptedException {
 3         /**
 4          * 發現UiObject2中的setText()方法還是無法直接輸入中文,需要借助外部方法庫.
 5          */
 6         UiObject2 editText = mDevice.findObject(By.clazz(EditText.class));
 7         editText.setText(Utf7ImeHelper.e("測試"));
 8 
 9         UiObject2 appIcon = mDevice.findObject(By.text("聯系人"));
10         Point desPoint = new Point();
11         desPoint.x = 654;
12         desPoint.y = 1066;
13         appIcon.drag(desPoint, 2000);
14 
15         UiObject2 appBtn = mDevice.findObject(By.text("聯系人"));
16         appBtn.longClick();
17 
18 
19         UiObject2 listView = mDevice.findObject(By.res("android:id/list"));
20         listView.scroll(Direction.DOWN, 0.8f, 3000);
21 
22         UiObject2 smsBtn = mDevice.findObject(By.text("短信"));
23         smsBtn.clickAndWait(Until.newWindow(), 2000);
24     }

2. 手勢動作模擬

2.1 相關API

 

API 說明
pinchClose(float percent, int speed) 自定義速度執行收縮手勢
pinchClose(float percent) 執行收縮手勢
pinchOpen(float percent, int speed) 自定義速度執行展開手勢
pinchOpen(float percent) 執行展開手勢
fling(Direction direction) 執行一個掃動手勢,Direction代表為起點方向
fling(Direction direction, int speed) 自定義速度,執行一個掃動手勢
swipe(Direction direction, float percent, int speed) 執行一個滑動操作,可自定義滑動距離和速度
swipe(Direction direction, float percent) 執行一個滑動操作
setGestureMargin(int margin) 以像素為單位,設置手勢邊緣
setGestureMargins(int left, int top, int right, int bottom) 以像素為單位,設置手勢邊緣

 

2.2 簡單示例

 1 @Test
 2     public void testCase06() throws InterruptedException {
 3         UiObject2 pic = mDevice.findObject(By.res("com.miui.gallery:id/single_view_other"));
 4         pic.pinchOpen(0.8f, 2000);
 5         Thread.sleep(1000);
 6         pic.pinchClose(0.8f, 2000);
 7 
 8         UiObject2 contactList = mDevice.findObject(By.res("android:id/list"));
 9         contactList.fling(Direction.DOWN);
10         Thread.sleep(1000);
11         contactList.fling(Direction.UP,3000);
12 
13         UiObject2 listView = mDevice.findObject(By.res("android:id/list"));
14         listView.swipe(Direction.UP,0.5f, 3000);
15         Thread.sleep(1500);
16         listView.setGestureMargin(100);
17         listView.swipe(Direction.DOWN,0.5f, 3000);
18     }

 

3. 獲取層級與條件判斷

 

3.1相關API

 

API 說明
findObject(BySelector selector) 搜索在這個對象之下的所有元素,並返回第一個與搜索條件匹配的
findObjects(BySelector selector) 搜索在這個對象之下的所有元素,並返回所有與搜索條件匹配的
getChildCount() 返回這個對象直屬子元素的數量
getChildren() 返回這個對象下的直接子元素的集合
getParent() 返回該對象的父類
equals(Object object) 比較兩個對象是否相等
hashCode() 獲取對象的哈希碼
hasObject(BySelector selector) 返回該對象是否存在
recycle() 回收該對象
wait(UiObject2Condition<R> condition, long timeout) 等待條件被滿足
wait(SearchCondition<R> condition, long timeout) 等待條件被滿足

 

3.2 簡單示例

 1 @Test
 2     public void testCase07(){
 3         UiObject2 list = mDevice.findObject(By.res("android:id/list"));
 4         UiObject2 child = list.findObject(By.clazz(TextView.class));
 5         Log.i("testCase07", child.getText());
 6 
 7         List<UiObject2> lisCollect = mDevice.findObjects(By.clazz(TextView.class));
 8         int count = lisCollect.size();
 9         Log.i("testCase07", String.valueOf(count));
10         for (UiObject2 obj:lisCollect) {
11             Log.i("testCase07", obj.getText());
12         }
13 
14         List<UiObject2> childList = list.getChildren();
15         int childCount = childList.size();
16         Log.i("testCase07", String.valueOf(childCount));
17         for (UiObject2 obj:childList) {
18             Log.i("testCase07", obj.getText());
19         }
20         
21         UiObject2 childElement = mDevice.findObject(By.text("聯系人"));
22         childElement.getParent().click();
23     }

 

原創:http://blog.csdn.net/swordgirl2011/article/details/50993157


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM