Android UiAutomator UiDevice API

UiDevice为单例模式

 

1.获取设备

static UiDevicegetInstance()

This method is deprecated. Should use getInstance(Instrumentation) instead. This version hides UiDevice's dependency on having an Instrumentation reference and is prone to misuse.

static UiDevicegetInstance(Instrumentation instrumentation)

Retrieves a singleton instance of UiDevice

 

2.按键与keycode

booleanpressBack()

Simulates a short press on the BACK button.

booleanpressDPadCenter()

Simulates a short press on the CENTER button.

booleanpressDPadDown()

Simulates a short press on the DOWN button.

booleanpressDPadLeft()

Simulates a short press on the LEFT button.

booleanpressDPadRight()

Simulates a short press on the RIGHT button.

booleanpressDPadUp()

Simulates a short press on the UP button.

booleanpressDelete()

Simulates a short press on the DELETE key.

booleanpressEnter()

Simulates a short press on the ENTER key.

booleanpressHome()

Simulates a short press on the HOME button.

booleanpressKeyCode(int keyCode)

Simulates a short press using a key code.

booleanpressKeyCode(int keyCode, int metaState)

Simulates a short press using a key code.

booleanpressMenu()

Simulates a short press on the MENU button.

booleanpressRecentApps()

Simulates a short press on the Recent Apps button.

booleanpressSearch()

Simulates a short press on the SEARCH button.

注意:

例子:

 UiDevice.getInstance(Instrumentation instrumentation).pressKeyCode(keyEvent.KEYCODE_A);//输入a

 UiDevice.getInstance(Instrumentation instrumentation).pressKeyCode(keyEvent.KEYCODE_A,1);//输入A

 Android KEYCODE键值对应大全 : http://blog.csdn.net/u012839224/article/details/37764193

3.获取坐标与坐标点击

 

booleanclick(int x, int y)

Perform a click at arbitrary coordinates specified by the user

int

getDisplayHeight()

Gets the height of the display, in pixels.

intgetDisplayWidth()

Gets the width of the display, in pixels.

另可通过UiObject来获取该对象的坐标

Rect z = UiObject.getBounds();

int x0 = z.left;

int y0 = z.top;

int x1 = z.right;

int y1 = z.bottom;

4.拖拽与滑动

booleandrag(int startX, int startY, int endX, int endY, int steps)

Performs a swipe from one coordinate to another coordinate.

booleanswipe(int startX, int startY, int endX, int endY, int steps)

Performs a swipe from one coordinate to another using the number of steps to determine smoothness and speed.

booleanswipe(Point[] segments, int segmentSteps)

Performs a swipe between points in the Point array.

Steps 越大,拖拽/滑动速度越慢

stepsint: is the number of move steps sent to the system

Each step execution is throttled to 5ms per step. So for a 100 steps, the swipe will take about 1/2 second to complete.

5.旋转屏幕

voidsetOrientationLeft()

Simulates orienting the device to the left and also freezes rotation by disabling the sensors.

voidsetOrientationNatural()

Simulates orienting the device into its natural orientation and also freezes rotation by disabling the sensors.

voidsetOrientationRight()

Simulates orienting the device to the right and also freezes rotation by disabling the sensors.

voidfreezeRotation()

Disables the sensors and freezes the device rotation at its current rotation state.

voidunfreezeRotation()

Re-enables the sensors and un-freezes the device rotation allowing its contents to rotate with the device physical rotation.

booleanisNaturalOrientation()

Check if the device is in its natural orientation.

intgetDisplayRotation()

Returns the current rotation of the display, as defined in Surface

Surface

intROTATION_0

Rotation constant: 0 degree rotation (natural orientation)

intROTATION_180

Rotation constant: 180 degree rotation.

intROTATION_270

Rotation constant: 270 degree rotation.

intROTATION_90

Rotation constant: 90 degree rotation.

 

6.灭屏与唤醒屏幕

voidwakeUp()

This method simulates pressing the power button if the screen is OFF else it does nothing if the screen is already ON.

voidsleep()

This method simply presses the power button if the screen is ON else it does nothing if the screen is already OFF.

booleanisScreenOn()

Checks the power manager if the screen is ON.

模拟点击电源键,手机灭屏与亮屏

7.截图

booleantakeScreenshot(File storePath, float scale, int quality)

Take a screenshot of current window and store it as PNG The screenshot is adjusted per screen rotation

booleantakeScreenshot(File storePath)

Take a screenshot of current window and store it as PNG Default scale of 1.0f (original size) and 90% quality is used The screenshot is adjusted per screen rotation

storePath: 存储路径,文件后缀必须为.png

scale: 1.0f 为原图大小

quality: 质量压缩,取值范围为0-100

 

8.等待空闲

voidwaitForIdle(long timeout)

Waits for the current application to idle.

voidwaitForIdle()

Waits for the current application to idle. //默认10s

booleanwaitForWindowUpdate(String packageName, long timeout)

Waits for a window content update event to occur.

9.包名

获取当前界面包名

StringgetCurrentPackageName()

Retrieves the name of the last package to report accessibility events.

获取当前页面的布局文件,保存到/data/local/tmp/下

voiddumpWindowHierarchy(File dest)

Dump the current window hierarchy to a File.

voiddumpWindowHierarchy(OutputStream out)

Dump the current window hierarchy to an OutputStream.


打开通知栏,打开快速设置

booleanopenNotification()

Opens the notification shade.

booleanopenQuickSettings()

Opens the Quick Settings shade.

 

 

转载于:https://www.cnblogs.com/biang/p/6347688.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值