天天看點

UIAutomator之---UiDeviceUiDevice

UiDevice

  • UiDevice
    • API
    • Example

UiDevice是用來與手機、模拟器等測試裝置進行互動,主要有以下功能:

  • 擷取裝置資訊
  • 發送操作指令,比如按鍵等
  • 截圖、儲存布局檔案等

本文整理了UiDevice的API,并對常用的API進行舉例說明。

API

Return Type Function and Description
void

clearLastTraversedText();

清除最後一次周遊得到的文本.

boolean

click(int x, int y);

點選螢幕上的(x, y)坐标,坐标原點為螢幕左上角.

boolean

drag(int startX, int startY, int endX, int endY, int steps);

拖拽,起始坐标為 (startX, startY),終點為(endX, endY);步長為steps.

boolean

click(int startX, int startY, int endX, int endY, int steps);

拖拽,起始坐标為 (startX, startY),終點為(endX, endY);步長為steps

void

dumpWindowHierarchy(File dest);

Ddump目前布局檔案到/data/local/tmp/目錄.

void

dumpWindowHierarchy(OutputStream out);

dump目前布局檔案到一個輸出流.

UiObject2

findObject(BySelector selector);

傳回符合selector的第一個控件,如果沒有比對到控件,則傳回

null

.
UiObject

findObject(UiSelector selector);

傳回符合selector的第一個控件,如果沒有比對到控件,則傳回

null

.
List

findObjects(BySelector selector);

傳回所有符合selector的控件.

void

freezeRotation();

禁用傳感器,并且保持目前螢幕方向不變.

String

getCurrentPackageName();

擷取目前界面包名.

int

getDisplayHeight();

擷取螢幕顯示的高度,機關為像素.

int

getDisplayRotation();

擷取目前界面的方向.

Point

getDisplaySizeDp()

擷取顯示尺寸大小.

int

getDisplayWidth();

擷取螢幕顯示的寬度,機關為像素.

static UiDevice

getInstance(Instrumentation instrumentation);

擷取UiDevice的執行個體.

String

getLastTraversedText();

得到最後一次周遊得到的文本.

String

getLauncherPackageName();

擷取預設加載的包名.

String

getProductName();

擷取裝置的工廠名.

boolean

hasAnyWatcherTriggered();

檢查已經注冊的UiWatcher是否被觸發.

boolean

hasObject(BySelector selector);

檢查目前界面是否包含與selector比對的控件.

boolean

hasWatcherTriggered(String watcherName);

檢查已經注冊名為watcherName的UiWatcher是否被觸發.

boolean

isNaturalOrientation();

檢查螢幕是否是預設的顯示方向.

boolean

isScreenOn();

檢查螢幕是否亮.

boolean

openNotification();

打開通知.

boolean

openQuickSettings();

打開快速設定.

R

performActionAndWait(Runnable action, EventCondition condition, long timeout);

執行一個action,并且等待condition的到來.

boolean

pressBack();

模拟傳回鍵.

boolean

pressDPadCenter();

模拟 Center 鍵.

boolean

pressDPadDown();

模拟向下按鍵.

boolean

pressDPadLeft();

模拟向左按鍵.

boolean

pressDPadRight();

模拟向右按鍵.

boolean

pressDPadUp();

模拟向上按鍵.

boolean

pressDelete();

模拟删除鍵.

boolean

pressEnter();

模拟确認/Enter鍵.

boolean

pressHome();

模拟Home鍵.

boolean

pressKeyCode(int keyCode);

以keyCode模拟一個按鍵.

boolean

pressKeyCode(int keyCode, int metaState);

以keyCode模拟一個按鍵.

boolean

pressMenu();

模拟菜單鍵.

boolean

pressRecentApps();

打開最近應用界面.

boolean

pressSearch();

模拟搜尋按鍵.

void

registerWatcher(String name, UiWatcher watcher);

注冊一個UiWatcher.

void

removeWatcher(String name);

取消注冊一個UiWatcher.

void

resetWatcherTriggers();

重置一個已被觸發的UiWatcher.

void

runWatchers();

運作所有已被注冊的watcher.

void setCompressedLayoutHeirarchy(
void

setOrientationLeft();

模拟螢幕向左轉,并且關閉傳感器、保持螢幕方向不變.

void

setOrientationNatural();

設定螢幕預設方向,并且關閉傳感器、保持螢幕方向不變.

void

setOrientationRight();

模拟螢幕向右轉,并且關閉傳感器、保持螢幕方向不變.

void

sleep();

螢幕亮時,等價于短按一次關機鍵;螢幕滅時,該方法不起作用.

boolean

swipe(int startX, int startY, int endX, int endY, int steps);

滑動,起始坐标為 (startX, startY),終點為(endX, endY); 步長為steps.

boolean

swipe(Point[] segments, int segmentSteps);

滑動一系列點,常用作模拟自由動作.

boolean

takeScreenshot(File storePath, float scale, int quality);

為目前界面截圖并儲存為PNG格式圖檔.

boolean

takeScreenshot(File storePath);

為目前界面截圖并儲存為PNG格式圖檔.

void

unfreezeRotation();

重新啟用傳感器,

R

wait(SearchCondition condition, long timeout);

等待一個condition.

void

waitForIdle(long timeout);

等待目前app空閑

void

waitForIdle();

等待目前視窗處于空閑狀态、預設10s

boolean

waitForWindowUpdate(String packageName, long timeout);

待視窗内容更新.

void

wakeUp();

模拟短按開關,

Example

//擷取UiDevice執行個體
    UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    //判斷螢幕是否亮,如果不是則亮屏
    if (!mDevice.isScreenOn()) {
        Log.i(TAG, "screen is off");
        mDevice.wakeUp();
    } else {
        Log.i(TAG, "screen is on");
    }

    //以id找到比對的控件并點選
    mDevice.findObject(new UiSelector().resourceId("com.star.uiautomator:id/add")).click();

    //擷取螢幕的寬、高以及尺寸大小
    Log.i(TAG, "Screen height is: " + mDevice.getDisplayHeight());
    Log.i(TAG, "Screen weight is: " + mDevice.getDisplayWidth());
    Point displaySizeDp = mDevice.getDisplaySizeDp();
    Log.i(TAG, "Screen size is: " + displaySizeDp.x + "-" + displaySizeDp.y);

    // 擷取包名
    Log.i(TAG, "Package name is: " + mDevice.getCurrentPackageName());

    //執行各類按鍵
    mDevice.pressBack();  // 傳回
    mDevice.pressHome();  // 傳回桌面鍵
    mDevice.pressMenu();  // 菜單
    mDevice.pressRecentApps();  // 打開最近使用的應用
           

點亮螢幕的情況下Log輸出如下:

I/TestRunner: started: uiDeviceText(com.star.uiautomator.UiAutomatorTest)
I/uiDeviceText: screen is on
I/uiDeviceText: Screen height is: 2392
I/uiDeviceText: Screen weight is: 1440
I/uiDeviceText: Screen size is: 411-731
I/uiDeviceText: Package name is: com.star.uiautomator
           

繼續閱讀