UiDevice为单例模式

1.获取设备

static UiDevice getInstance()

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 UiDevice getInstance(Instrumentation instrumentation)

Retrieves a singleton instance of UiDevice

2.按键与keycode

boolean pressBack()

Simulates a short press on the BACK button.

boolean pressDPadCenter()

Simulates a short press on the CENTER button.

boolean pressDPadDown()

Simulates a short press on the DOWN button.

boolean pressDPadLeft()

Simulates a short press on the LEFT button.

boolean pressDPadRight()

Simulates a short press on the RIGHT button.

boolean pressDPadUp()

Simulates a short press on the UP button.

boolean pressDelete()

Simulates a short press on the DELETE key.

boolean pressEnter()

Simulates a short press on the ENTER key.

boolean pressHome()

Simulates a short press on the HOME button.

boolean pressKeyCode(int keyCode)

Simulates a short press using a key code.

boolean pressKeyCode(int keyCode, int metaState)

Simulates a short press using a key code.

boolean pressMenu()

Simulates a short press on the MENU button.

boolean pressRecentApps()

Simulates a short press on the Recent Apps button.

boolean pressSearch()

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.获取坐标与坐标点击

boolean click(int x, int y)

Perform a click at arbitrary coordinates specified by the user

int

getDisplayHeight()

Gets the height of the display, in pixels.

int getDisplayWidth()

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.拖拽与滑动

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

Performs a swipe from one coordinate to another coordinate.

boolean swipe(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.

boolean swipe(Point[] segments, int segmentSteps)

Performs a swipe between points in the Point array.

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

steps int: 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.旋转屏幕

void setOrientationLeft()

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

void setOrientationNatural()

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

void setOrientationRight()

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

void freezeRotation()

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

void unfreezeRotation()

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

boolean isNaturalOrientation()

Check if the device is in its natural orientation.

int getDisplayRotation()

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

Surface

int ROTATION_0

Rotation constant: 0 degree rotation (natural orientation)

int ROTATION_180

Rotation constant: 180 degree rotation.

int ROTATION_270

Rotation constant: 270 degree rotation.

int ROTATION_90

Rotation constant: 90 degree rotation.

6.灭屏与唤醒屏幕

void wakeUp()

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

void sleep()

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

boolean isScreenOn()

Checks the power manager if the screen is ON.

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

7.截图

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

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

boolean takeScreenshot(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.等待空闲

void waitForIdle(long timeout)

Waits for the current application to idle.

void waitForIdle()

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

boolean waitForWindowUpdate(String packageName, long timeout)

Waits for a window content update event to occur.

9.包名

获取当前界面包名

String getCurrentPackageName()

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

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

void dumpWindowHierarchy(File dest)

Dump the current window hierarchy to a File.

void dumpWindowHierarchy(OutputStream out)

Dump the current window hierarchy to an OutputStream.

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

boolean openNotification()

Opens the notification shade.

boolean openQuickSettings()

Opens the Quick Settings shade.

 

Android UiAutomator UiDevice API的更多相关文章

  1. Android无线测试之—UiAutomator UiDevice API介绍八

    获取包名.开启通知栏.快速设置.获取布局文件的方法 一.包名.通知栏.快速设置.布局文件等相关知识: 1)包名:标示应用的符号,每个应用的名字 2)通知栏:从主界面的顶端向下拉,就可以打开通知栏 3) ...

  2. Android无线测试之—UiAutomator UiDevice API介绍七

    截图与等待空闲 一.截图和等待空闲相关知识: 1)图片缩放比例:例如图片从100像素缩小到50像素 2)图片质量:是指图片大小,质量越高图片越大,质量越低图片越小 3)File类:指的是一个文件或者一 ...

  3. Android无线测试之—UiAutomator UiDevice API介绍六

    一.灭屏和唤醒屏幕相关知识: 1)灭屏:按电源键将屏幕熄灭 2)唤醒屏幕:在灭屏状态下按电源键唤醒屏幕 二.灭屏与唤屏相关的API: 返回值 方法名 描述 void wakeUp() 模拟按电源键,如 ...

  4. Android无线测试之—UiAutomator UiDevice API介绍五

    屏幕旋转 一.屏幕旋转相关知识: 1)旋转方向:0度,90度(向左转),180度,270度(向右转) 2)重力感应器:重力感应器是旋转所依靠的 3)固定位置:指将屏幕方向固定在0度,90度或者180度 ...

  5. Android无线测试之—UiAutomator UiDevice API介绍四

    拖拽与滑动 一.概念介绍: 1)拖拽:将组建从一个坐标移动到另一个坐标 2)移动:从一二坐标点移动到另一个坐标点 3)步长:从一点滑动到另一点使用的时间 二.拖拽与滑动的相关API: 返回值 方法名 ...

  6. Android无线测试之—UiAutomator UiDevice API介绍三

    获取坐标与坐标点击 一.坐标相关的知识: 1)手机屏幕坐标:左上角开始到右下角结束 2)DP:设备独立像素,例如320像素显示到640像素上要拉伸一倍 3)Point:代表一个点(x,y),左上角的坐 ...

  7. Android无线测试之—UiAutomator UiDevice API介绍二

    按键与KEYCODE使用 一.手机常见按键: 1)HOME 主屏幕键 2) MENU 菜单键 3) BACK 返回键 4) VOLUME_UP 音量加键 5) VOLUME_DOWN 音量减键 6) ...

  8. Android无线测试之—UiAutomator UiDevice API介绍一

    UiDevice 类介绍 1.UiDevice 代表设备状态 2.UiDevice 为单例模式 获取UiDevice实例的方式: 1) UiDevice.getInstance() 2) getUiD ...

  9. 快学UiAutomator UiDevice API 详解

    一.按键使用 返回值 方法名 说明 boolean pressBack() 模拟短按返回back键 boolean pressDPadCenter() 模拟按轨迹球中点按键 boolean press ...

随机推荐

  1. 二进制搭建kubernetes多master集群【二、配置flannel网络】

    上一篇我们已经搭建etcd高可用集群,参考:二进制搭建kubernetes多master集群[一.使用TLS证书搭建etcd集群] 此文将搭建flannel网络,目的使跨主机的docker能够互相通信 ...

  2. com.liuyang.exception.DaoException: java.sql.SQLException: Incorrect string

    错误是在JUnit测试的时候产生的,但是实际加入数据也会产生这样的情况,主要是数据库内部的编码方式不支持当前的编码方式导致的冲突,解决方法就是在建立数据库之前,要查看当前的数据库的编码方式,方法和更改 ...

  3. int最大值+1为什么是-2147483648最小值-1为什么是2147483647

    今天一个新手学编程就问到这个问题,很多人第一次学编程肯定会遇到这个问题,大部分都知道是溢出之类的,用源码和补码就很容易说明 int i = -2147483648 ;这是不允许的 VS里报的错 err ...

  4. AdapterViewFlipper功能 自动播放的图片库

    案例中有"上一个""下一个""自动播放",但是我觉得可以更加完善一下,点击自动播放,按钮变成"停止播放",在按" ...

  5. Oracle实例名,服务名等概念区别与联系

    数据库名.实例名.数据库域名.全局数据库名.服务名 , 这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水.我们现在就来把它们弄个明白. 一.数据库名 什么是数 ...

  6. python技巧31[python中使用enum][转]

    以下几种方法来模拟enum:(感觉方法一简单实用) # way1 class Directions:     up = 0     down = 1     left = 2     right =3 ...

  7. QGIS+GH + MapServer

    拒绝描图,如何利用GH+QGIS完爆场地底图?http://www.sohu.com/a/251004986_657084 拒绝描图--爬取OSM数据绘制底图 所用软件 RHINO+GH\QGIS\G ...

  8. ZUFE2389: Occult的卡片升级计划(DP) 2016-05-17 23:12 113人阅读 评论(0) 收藏

    2389: Occult的卡片升级计划 Description Occult喜欢玩一款手游,手游中有很多好看的卡片.他想要升级这些卡. 现在他有m块经验石,目标是强化一张卡片,卡片最开始的时候是0级, ...

  9. C++ 中的运算符的优先级

    C++ 中的运算符的优先级 直接看图

  10. Alwayson--使用证书创建高可用性组

    --场景: --有服务器SQLNode11,SQLNODE21,SQLNODE31三台在同一故障转移群集SQLNode01中 --的数据库服务器,安装SQL SERVER 2012 并配置启动alwa ...