博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android UiAutomator UiDevice API
阅读量:5821 次
发布时间:2019-06-18

本文共 4494 字,大约阅读时间需要 14 分钟。

UiDevice为单例模式

 

1.获取设备

static  ()

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

static  (Instrumentation instrumentation)

Retrieves a singleton instance of UiDevice

 

2.按键与keycode

boolean ()

Simulates a short press on the BACK button.

boolean ()

Simulates a short press on the CENTER button.

boolean ()

Simulates a short press on the DOWN button.

boolean ()

Simulates a short press on the LEFT button.

boolean ()

Simulates a short press on the RIGHT button.

boolean ()

Simulates a short press on the UP button.

boolean ()

Simulates a short press on the DELETE key.

boolean ()

Simulates a short press on the ENTER key.

boolean ()

Simulates a short press on the HOME button.

boolean (int keyCode)

Simulates a short press using a key code.

boolean (int keyCode, int metaState)

Simulates a short press using a key code.

boolean ()

Simulates a short press on the MENU button.

boolean ()

Simulates a short press on the Recent Apps button.

boolean ()

Simulates a short press on the SEARCH button.

注意:

例子:

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

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

 : http://blog.csdn.net/u012839224/article/details/37764193

3.获取坐标与坐标点击

 

boolean (int x, int y)

Perform a click at arbitrary coordinates specified by the user

int

()

Gets the height of the display, in pixels.

int ()

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 (int startX, int startY, int endX, int endY, int steps)

Performs a swipe from one coordinate to another coordinate.

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

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

void ()

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

void ()

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

void ()

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

void ()

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

boolean ()

Check if the device is in its natural orientation.

int ()

Returns the current rotation of the display, as defined in 

int

Rotation constant: 0 degree rotation (natural orientation)

int

Rotation constant: 180 degree rotation.

int

Rotation constant: 270 degree rotation.

int

Rotation constant: 90 degree rotation.

 

6.灭屏与唤醒屏幕

void ()

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

void ()

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

boolean ()

Checks the power manager if the screen is ON.

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

7.截图

boolean (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 (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 (long timeout)

Waits for the current application to idle.

void ()

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

boolean (String packageName, long timeout)

Waits for a window content update event to occur.

9.包名

获取当前界面包名

String ()

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

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

void (File dest)

Dump the current window hierarchy to a .

void (OutputStream out)

Dump the current window hierarchy to an .

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

boolean ()

Opens the notification shade.

boolean ()

Opens the Quick Settings shade.

 

 

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

你可能感兴趣的文章
让自己的网站实现在线编辑office文档
查看>>
IOS开发之功能模块--给任意的UIView添加点击事件
查看>>
php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天...
查看>>
[Android] 如何查看apk需要支持的Android版本
查看>>
mysql创建数据库
查看>>
R 语言 Windows 环境 安装与Windows下制作R的package--Rtools
查看>>
【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print
查看>>
Create rolling monthly, weekly and daily Logstash indices
查看>>
Scala进阶之路-Spark本地模式搭建
查看>>
利用pom配置实现静态文件拷贝
查看>>
vim中文手册
查看>>
DB2 创建 TOOLSDB数据库
查看>>
终于把空岛的故事看完了
查看>>
深入浅出 Barriers 实现(一)
查看>>
VS2015 + Cordova Html5开发使用Crosswalk Web引擎
查看>>
Lua语言如何调用自己编写的C DLL -- 转
查看>>
Embedded linux primer
查看>>
stm8s + si4463 寄存器配置
查看>>
C++ STL标准模板库(list)
查看>>
做一个通用的XML序列化,支持所有类型
查看>>