1. 程式人生 > >UI Automator 常用 API 整理

UI Automator 常用 API 整理

until tde current tex 調用 獲取包名 使用 eset found

主要類:

import android.support.test.uiautomator.UiDevice;

作用:設備封裝類,測試過程中獲取設備信息和設備交互。

import android.support.test.uiautomator.UiObject;

作用:所有控件抽象,用於表示一個Android控件。

import android.support.test.uiautomator.UiObjectNotFoundException;

作用:異常處理機制,在預期控件不存在時拋出。

import android.support.test.uiautomator.UiSelector;

作用:控制選擇器,利用控制屬性描述目標控件,用於控件匹配使用。

import android.support.test.uiautomator.Configurator;

所用:配置基類,用以控制測試過程的事件等超時、控件可見超時等。

import android.support.test.uiautomator.UiCollection;

作用:控件集合,用於控件遍歷。

import android.support.test.uiautomator.UiScrollable;

作用:滾動控件,當目標控件存在於屏幕之外時使用。

import android.support.test.uiautomator.UiWatcher;

作用:界面觀察者,用於處理彈窗中斷邏輯。


定位控件

import android.support.test.uiautomator.By;
作用:可以更簡潔的方式使用ByScelector 選擇器。
用法:
findObject(By.text(“text”))

import android.support.test.uiautomator.BySelector;
作用: BySelector 調用findObject 時匹配UI 元素
用法:
findObject(new BySelector().text(“text”))


By類常用定位方法

/**
 * 通過文本 text 定位控件
 * 例如: text = "hello world"
 */
device.findObject(By.text("text"));
device.findObject(By.textContains("llo wor"));
device.findObject(By.textStartsWith("hello"));
device.findObject(By.textEndsWith("world"));

/**
 * 通過內容描述 content-dec 定位控件
 * 例如: desc = "content-dec"
 */
device.findObject(By.desc("content-dec"));
device.findObject(By.descContains("tent"));
device.findObject(By.descStartsWith("content"));
device.findObject(By.descEndsWith("-dec"));

/**
 *  通過包名package定位控件
 *  例如: package = "com.android.calculator2"
 */
device.findObject(By.pkg("com.android.calculator2"));

/**
 *  通過資源名 resource 定位控件
 *  例如: resource = "com.android.calculator2:id/digit_0"
 */
device.findObject(By.res("com.android.calculator2:id/digit_0"));

/**
 *  通過類名 class定位控件
 *  例如: class = "android.widget.Button"
 */
device.findObject(By.clazz("android.widget.Button"));


UiDevice類常用方法

// home鍵
device.pressHome();

// back 鍵
device.pressBack();

// 顯示最近打開並置於後臺的App
device.pressRecentApps();

// 快速設置鍵
device.openQuickSettings();

// 打開通知
device.openNotification();

// 虛擬鍵盤,參考appium
device.pressKeyCode(17);

// 獲得屏幕高度和寬度
int x = device.getDisplayWidth();
int y = device.getDisplayHeight();
String xs =String.valueOf(x);
String ys =String.valueOf(y);
Log.e("xxxxxxxxxx", xs);
Log.e("yyyyyyyyyy", ys);

// 向下滑顯示通知欄
device.swipe(200,0,200, 300,180);
// 向左滑顯示右一屏
device.swipe(20,400,460, 400,180);

// 獲取當前應用活動名 和 包名
String activity = device.getCurrentActivityName();
String packagea = device.getCurrentPackageName();
Log.e("activity", activity);
Log.e("packagea", packagea);

// 休眠屏幕
device.sleep();
// 如果屏幕熄滅,點亮
device.wakeUp();


UiObject2類常用方法

UiObject2 element = device.findObject(By.text("text"));

//清除元素,針對輸入框
element.clear();

// 點擊
element.click();

// 長按
element.longClick();

// 獲取元素文本
element.getText();

//設置元素文本,相當於輸入
element.setText("new text");

//獲取元素scrollable屬性,判斷是否可滾動
element.isScrollable();

// 判斷兩個對象是否一致
UiObject2 element2 = device.findObject(By.text("text"));
element.equals(element2);

//獲取元素content_desc屬性
element.getContentDescription();

// 獲取包名 package 屬性
element.getApplicationPackage();

// 獲取元素的子元素集合
element.getChildren();

// 獲取元素的子元素的個數
element.getChildCount();

// 獲取元素的class屬性
element.getClassName();

// 獲取元素的 resource-id 屬性
element.getResourceName();

// 將元素拖動到指定位置
Point desPoint = new Point();
desPoint.x = 200;
desPoint.y = 20;
element.drag(desPoint, 2000);

// 點擊並等待新窗口
element.clickAndWait(Until.newWindow(), 2000);


Configurator類

Configurator configurator = Configurator.getInstance();

//動作,設置延時, 默認3s
configurator.setActionAcknowledgmentTimeout(1000);

//鍵盤輸入,設置延時,默認0s
configurator.setKeyInjectionDelay(1500);

// 滾動,設置延時, 默認200ms
configurator.setScrollAcknowledgmentTimeout(2000);

// 空閑,設置延時,默認10s
configurator.setWaitForIdleTimeout(2500);

// 組件查找, 設置延時, 默認10s
configurator.setWaitForSelectorTimeout(3000);


UiWatcher 類用法

final UiObject2 ui = mDevice.findObject(By.text("Messenger"));
//註冊監聽器
mDevice.registerWatcher("testWatcher", new UiWatcher() {
    @Override
    public boolean checkForCondition() {
        if(mDevice.hasObject(By.text("Contact"))){
            ui.click();
            Log.i("testWatcher", "監聽器被觸發了");
            return true;
        }
        Log.i("testWatcher", "監聽器未被觸發");
        return false;
    }
});

//重置監聽器
mDevice.resetWatcherTriggers();

//移除監聽器
mDevice.removeWatcher("testWatcher");

//運行所有的監聽器
mDevice.runWatchers();


UiScrollable 類的常用方法

UiScrollable scroll  = new UiScrollable( new UiSelector()
        .scrollable(true));

// 以步長為5快速向後滑動
scroll.flingBackward();

//以步長為5快速向前滑動
scroll.flingForward();

// 是否允許滾動獲取具備UiSelector條件元素集合後, 再以text屬性的查找對象
scroll.getChildByText(
        new  UiSelector().resourceId("android:id/title"), "About emulated device", true);
// 是否允許滾動獲取具備UiSelector條件元素集合後, 再以content-desc屬性搜索子元素
scroll.getChildByDescription(new UiSelector().text("text"), "content-desc", true);

//通過實例查找子元素,資源id為"android:id/title"下的第1個實例
scroll.getChildByInstance(new UiSelector().resourceId("android:id/title"),0);

// 獲取執行搜索滑動過程中的最大滑動次數,默認常量為30
scroll.getMaxSearchSwipes();

//設置最大可掃動次數
scroll.setMaxSearchSwipes(50);

/**
 * 設置listview校準常量為0.15,即距離listview頂部15%和底部15%的區域不可滑動,只有控件中部70%的區域可滑動。
 * 當校準常量設置為0.5時,控件的可滑動區域為0,滑動的動作效果將和單擊的效果一樣。
 */
scroll.setSwipeDeadZonePercentage(0.15);
// 獲得校準常量,校準常量默認值為0.1(10%)
scroll.getSwipeDeadZonePercentage();

//設置滾動方向設置為水平滾動
scroll.setAsHorizontalList();
// 設置滾動方向設置為縱向滾動
scroll.setAsVerticalList();

// 滾動到某個元素上
scroll.scrollIntoView(new UiSelector().text("abc"));
//滾動到文本對象所在位置
scroll.scrollTextIntoView("abc");

//自定義最大滾動次數,滾動到開始/結束位置
scroll.scrollToBeginning(30);
scroll.scrollToEnd(30);
//以步長(速率)5滾動到列表底部,最多滾動10次。
scroll.scrollToEnd(10, 5);


UiCollection 類的常用方法

UiCollection  coll = new UiScrollable(new UiSelector()
.resourceId("android:id/title"));
// 查找元素下面子元素的數量
coll.getChildCount();
// 獲取元素集合,再以text屬性的查找對象
coll.getChildByText(new UiSelector().text("Display"), "Display");
// 獲取元素集合,再查找其下面第1個元素
coll.getChildByInstance(new UiSelector().text("Display"), 0);
// 獲取元素集合,再以content_desc屬性的查找對象
coll.getChildByDescription(new UiSelector().text("Display"), "content_desc");
// 獲得指定的子元素
coll.getChild(new UiSelector().text("aaa"));

UI Automator 常用 API 整理