标签:
UiDevice为单例模式,获取实例的方式有两种
getUiDevice().pressHome(); //在其它地方封装方法被调用时,会出现空指针异常
例如下列代码在Demo2中定义了一个press方法
public class Demo2 extends UiAutomatorTestCase { public void press() { getUiDevice().pressMenu(); sleep(2000); getUiDevice().pressHome(); } }
在Demo1中进行调用Demo2的press方法
public void testDevice() { Demo2 demo2 = new Demo2(); demo2.press(); //会抛出空指针异常!!!!!! }
返回值 | 方法名 | 描述 |
boolean | pressBack() | 模拟短按返回back键 |
boolean | pressDPadCenter() | 模拟按轨迹球中点按键 |
boolean | pressDPadDownr() | 模拟按轨迹球向下按键 |
boolean | pressDPadLeft() | 模拟按轨迹球向左按键 |
boolean | pressDPadRight() | 模拟按轨迹球向右按键 |
boolean | pressDPadUp() | 模拟按轨迹球向上按键 |
boolean | pressDelete() | 模拟短按delete键 |
boolean | pressEnter | 模拟短按回车键 |
boolean | pressHome() | 模拟短按HOME键 |
boolean | pressKeyCode(int keyCode, int metaState) | 模拟短按键盘代码keycode |
boolean | pressKeyCode(int keyCode) | 模拟短按键盘代码keycode |
boolean | pressMenu() | 模拟短按Menu键 |
boolean | pressRecentApps() | 模拟短按最近使用程序 |
boolean | pressSearch() | 模拟短按搜索键 |
列 | 激活状态 | 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().pressHome(); UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A); //输入小写字母a UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B); //输入小写字母b UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C); //输入小写字母c UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A, 1); //输入大写字母a UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B, 1); //输入大写字母b UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C, 1); //输入大写字母c }
【UiAutomator学习笔记】2. UiDevice API 详细介绍(1/2)
标签:
原文地址:http://www.cnblogs.com/WangKangReg/p/4379663.html