天天看点

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();

模拟确认/回车键.

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
           

继续阅读