UiDevices代表设备状态
是单例模式
UIDevices功能
源码的介绍:
/**
* UiDevice provides access to state information about the device.
* You can also use this class to simulate user actions on the device,
* such as pressing the d-pad or pressing the Home and Menu buttons.
* @since API Level 16
*/
翻译下就是:
一堆的API,下面我们举几个例子
image.png
1. static UIDevice getInstance()
2. static UIDevice getInstance(Instrumentation instrumentation)
我们主要是使用功能第二个:
Instrumentation mInstrumentation=InstrumentationRegistry.getInstrumentation();
UIDevice mDevice=UiDevice.getInstance(mInstrumentation);
目前的机器都是智能机,没有KeyCode了
手机常用的按键
返回值 | 方法名 | 描述 |
---|---|---|
boolean | pressBack() | 手机中的back按键 |
boolean | pressDpadCenter() | 轨迹球中点按键 |
boolean | pressDpadDown() | 轨迹球向下按键 |
boolean | pressDpadLeft() | 轨迹球向左按键 |
boolean | pressDpadRight() | 轨迹球向右按键 |
boolean | pressDpadUp() | 轨迹球向上按键 |
boolean | pressDelete() | 删除 |
boolean | pressEnter() | 回车 |
boolean | pressHome() | Home按键 |
boolean | pressMenu() | Menu键 |
boolean | pressRecentApps() | 最近使用 |
boolean | pressSearch() | 搜索按键 |
boolean | pressKeyCode(int keyCode) | 发送KeyEvent |
boolean | pressKeyCode(int keyCode, int metaState) | 发送配有组合键(ALT,SHIFT)的KeyEvent |
键盘映射码
列 | 激活状态 | metaState |
---|---|---|
base | META_Key未激活 | 0 |
caps | SHIFT或者CAPS_LOCK被激活 | 1 |
fn | ALT被激活 | 2 |
caps_fn | ALT,SHIFT,CAPS_LOCK同时被激活 | 3 |
例子:
public void testPress() {
UiDevice.getInstance.pressKeyCode(KeyEvent.KEYCODE_A);
UiDevice.getInstance.pressKeyCode(KeyEvent.KEYCODE_A, 1)
}
点击屏幕的中点:
public void testClick() {
int h = UiDevice.getInstance().getDisplayHeight();
int w = UiDevice.getInstance().getDisplayWidth();
UiDevice.getInstance().click(w/2, h/2);
}
返回值 | 方法名 | 描述 |
---|---|---|
boolean | drag(int startX, int startY, int endX, int endY, int steps) | 拖动对象从一个坐标到另外一个坐标 |
boolean | swip(Point[] segments, int segmentSteps) | 在矩阵中滑动 |
boolean | swip(int startX, int startY, int endX, int endY, int steps) | 通过坐标滑动 |
steps:是指,分多少次完成这次动作。每次移动花费的时间是固定的,都为5ms。
segmentSteps:是指,点与点之间分多少次完成这次动作。同steps。
例子:
从下往上滑动
public void testDrag() {
//[115, 1130] [234, 1567]
int startX, startY, endX, endY, steps;
startX = (234-115)/2 + 115;
startY = (1576-1130)/2 + 1130;
endX = startX;
endY = starY-500;
steps = 100;
UiDevice.getInstance().drag(startX, startY, endX, endY, steps);
}
从右往左
@Test
public void FunctionKeyTest4(){
int h=mDevice.getDisplayHeight();
int w=mDevice.getDisplayWidth();
int left=200;
int right=w-200;
int step=50;
Log.i(TAG, "left = "+left+" Right = "+right);
//(1)From Right to Left
Log.i(TAG, "from ("+right+","+h/2+") to ("+left+","+h/2+")");
result=mDevice.swipe(right, h/2, left, h/2, step);
Log.i(TAG, "Swipe result (Right to Left) = "+result);
mDevice.waitForIdle(timeOut);
//(2)From Left to Right
Log.i(TAG, "from ("+left+","+h/2+") to ("+right+","+h/2+")");
result=mDevice.swipe(left, h/2, right, h/2, step);
Log.i(TAG, "Swipe result (Left to Right) = "+result);
mDevice.waitForIdle(timeOut);
//(3)Drag
Log.i(TAG, "Dragfrom (300,1000) to (700,1000)");
result=mDevice.drag(left, h/2, right, h/2, step);
Log.i(TAG, "Drag result (Left to Right) = "+result);
mDevice.waitForIdle(timeOut);
//(4)From Left to Right
Point[] pointArray=new Point[]{
new Point(200,h/2),new Point(300,h/2),new Point(400,h/2),new Point(500,h/2),new Point(600,h/2),new Point(700,h/2),
};
result=mDevice.swipe(pointArray, step);
Log.i(TAG, "Swipe by Array, result (Left to Right) = "+result);
mDevice.waitForIdle(timeOut);
//(5)Click
result=mDevice.click(300, 1000);
Log.i(TAG, "Click = "+result);
mDevice.waitForIdle(timeOut);
}
图案解锁,倒L
public void selectScreenLockPattern() throws UiObjectNotFoundException {
mSettingsMainPage.settingsListItem(mScreenLock);
enterSettingLockScreenScene();
mDevice.wait(Until.hasObject(ChooseScreenLockSelector), LAUNCH_CALL_PAGE);
find(patternScreenLockSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
if (mDevice.hasObject(SecureStartUpSelector)) {
find(SecureStartUpNoButtonSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
}
Point[] pointLockArray = new Point[]{
new Point(175, 645), new Point(357, 645), new Point(531, 645), new Point(531, 825), new Point(531, 1006)
};
int step = 50;
mDevice.swipe(pointLockArray, step);
find(setScreenLockNextSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
mDevice.swipe(pointLockArray, step);
find(setScreenLockConfirmSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
if(mDevice.hasObject(setScreenLockDoneSelector)){
find(setScreenLockDoneSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
}
if(mDevice.hasObject(setScreenQuestionSkipSelector)){
find(setScreenQuestionSkipSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
}
mDevice.wait(Until.hasObject(screenLockButtonSelector), LAUNCH_CALL_PAGE);
}
返回值 | 方法名 | 描述 |
---|---|---|
void | setOrientationLeft | 禁用传感器,模拟设备左转,固定位置 |
void | setOrientationNatural() | 禁用传感器,模拟设备转到默认的位置 |
void | setOrientationRight() | 禁用传感器,模拟设备右转,固定位置 |
void | unfreezeRotation() | 启用传感器和允许物理旋转 |
boolean | isNaturalOrientation() | 检查设备是否处于默认旋转的状态 |
int | getDisplayRotation() | 返回当前的旋转度, 0,90,180,270, 分别对应的值是:0,1,2,3 |
point | getDisplaySizeDp() | 以设备独立像素为单位,以Point类的形式,返回显示器的大小 |
int | getDisplayWidth() | 以物理像素为单位,获取显示器的宽度。 |
int | getDisplayHeight() | 以物理像素为单位获取显示器的高度 |
void | freezeRotation() | 禁用传感器,固定物理旋转在当前的旋转状态 |
public void testOrientation() {
UiDevice.getInstance().set
}
返回值 | 方法名 | 描述 |
---|---|---|
void | wakeUp() | 唤醒屏幕 |
void | sleep() | 休眠 |
boolean | isScreen() | 检查屏幕是否亮屏 |
@Test
public void testWakeUp() throws UiObjectNotFoundException, RemoteException {
if (!UiDevice.getInstance().isScreenOn()) {
UiDevice.getInstance().sleep();
UiDevice.getInstance().wakeUp();
//另外一种办法
mDevice.wakeUp();
mDevice.sleep();
mDevice.isScreenOn();
}
}
返回值 | 方法名 | 描述 |
---|---|---|
boolean | takeScreenshot(File, storePath) | 当前的窗口截图保存为png,file为路径 |
boolean | takeScreenshot(File, storePath, float, scale, int quality) | 当前的窗口截图为png,可以自定义缩放比例和图片质量 |
public void testScreenshot() {
UiDevice.getInstance().takeScreenshot(new file("/sdcard/test.png"))
}
返回值 | 方法名 | 描述 |
---|---|---|
void | getCurrentPackageName() | 获取当前APP Package的Name |
void | getCurrentActivityName() | 获取当前Activity的Name,可信度不高,废弃 |
void | getLauncherPackageName() | 获取启动器(Launcher)所在Package的Name |
void | getProductName() | 获取设备名称 |
void | dumpWindowHierarchy(String fileName) | 获取当前界面的布局文件,保存到/data/local/tmp下面 |
boolean | openNotification() | 打开通知栏 |
boolean | openQuickSettings() | 打开快速设置 |
public void testDemo() {
String packageName = UiDevice.getInstance().getCurrenPackageName()
String.out.println(packageName);
UiDevice.getInstance().openNotification();
UiDevice.getInstance().openQuickSettings();
UiDevice.getInstance().dumpWindowHierarchy("n.xml");
}