手机测试行业自动化测试中,需要模拟长按操作,但是很多同学只知道长按屏幕界面,但是对于长按物理键(如电源键、音量键、还有其他公司手机自己自定义的键)不知道如何实现,uiautomator中的API介绍都是只讲了短按,百度中的方法都看起来很复杂,需要写个方法和类。
非物理按键的有:home键、菜单键、Back键(返回键)
一般常用的方法有:getUiDevice().pressHome();//home键 或者:getUiDevice().pressKEYCODE_3;//home键、键值自查,请看附件!
长按手机中的按钮用longclick,短按需要click,例:
UiObject paizhaojian=new UiObject(new UiSelector().resourceId("com.cn.filer:id/row"));//拍照键
paizhaojian.longClick();//长按拍照键连拍
UiObject paizhaojian=new UiObject(new UiSelector().resourceId("com.cn.filer:id/row"));//拍照键
paizhaojian.Click();//短按拍照键
物理按键的有:音量+/上键、音量-/下键、power键(电源键)、特有的物理按键;
按下多久,然后松开,如对讲机按PTT键对讲:
Runtime.getRuntime().exec("input keyevent --longpress 5 260");//按下PTT键不松,260为PTT键键值
sleep(11000);//实际长按10秒
UiDevice.getInstance().pressKeyCode(260);//松开PTT键
sleep(15000);
或者
Runtime.getRuntime().exec("sendevent /dev/input/event0 1 116 1");
Runtime.getRuntime().exec("sendevent /dev/input/event0 0 0 0");
sleep(3000);
Runtime.getRuntime().exec("sendevent /dev/input/event0 1 116 0");
Runtime.getRuntime().exec("sendevent /dev/input/event0 0 0 0");
这个参考的是:https://www.cnblogs.com/by-dream/p/5195121.html
短按单个物理键:
短按下物理键,如拨号键/挂断键,
Runtime.getRuntime().exec("input keyevent KEYCODE_ENDCALL");//短按挂断键,结束通话
长按单个物理键:
但是物理按键以上的方法不行,这里模拟物理按键我就只需要三行代码就可以实现,废话少说,直接上代码(只能长按单个物理键哦!)
//262是自定义的按键,其他按键(home、back、音量键的键值),百度或者抓log日志查看
Runtime.getRuntime().exec("input keyevent --longpress 5 262");
sleep(6000);
UiDevice.getInstance().pressKeyCode(262);
长按多个物理键:
要是向一起按多个按键怎么做呢?比如:同时按电源键+音量下键 就可以截屏,大家可以自己先想想。
我没有试过同时按住的方法,但是有人做过,参考其他人的博客这样描述:
仔细看看sendKey方法里头有两个KeyEvent,一个是down,一个是up。
这不就是按下和抬起吗?那中间加上延时不就是长按了吗?
多个按键,就一起发down,一起发up,就可以了
可是还没有实验成功。
大家可以通过uiautomator自带的截图api去实现,可以去看我写的另篇文章~~~后续摸索出来在更新,所以要随时关注和评论哦!
到底是不是这样写,我自己没有尝试过,最近没有时间编译,也没有遇到这样的需求,大家尝试去试试吧!
常见按键附件(百度也有):
键值;键名
3; Home键
82 menu键
4; Back返回
5; 接电话
6 挂电话
19; 方向键上
20;方向键下
21;方向键左
22;方向键右
23;OK确定键
24; 音量上
25;音量下
26; power键
203:PTT键
// key codes,键名对应着键值
public static final int KEYCODE_UNKNOWN = 0;
public static final int KEYCODE_SOFT_LEFT = 1;
public static final int KEYCODE_SOFT_RIGHT = 2;
public static final int KEYCODE_HOME = 3;
public static final int KEYCODE_BACK = 4;
public static final int KEYCODE_CALL = 5;
public static final int KEYCODE_ENDCALL = 6;
public static final int KEYCODE_0 = 7;
public static final int KEYCODE_1 = 8;
public static final int KEYCODE_2 = 9;
public static final int KEYCODE_3 = 10;
public static final int KEYCODE_4 = 11;
public static final int KEYCODE_5 = 12;
public static final int KEYCODE_6 = 13;
public static final int KEYCODE_7 = 14;
public static final int KEYCODE_8 = 15;
public static final int KEYCODE_9 = 16;
public static final int KEYCODE_STAR = 17;
public static final int KEYCODE_POUND = 18;
public static final int KEYCODE_DPAD_UP = 19;
public static final int KEYCODE_DPAD_DOWN = 20;
public static final int KEYCODE_DPAD_LEFT = 21;
public static final int KEYCODE_DPAD_RIGHT = 22;
public static final int KEYCODE_DPAD_CENTER = 23;
public static final int KEYCODE_VOLUME_UP = 24;
public static final int KEYCODE_VOLUME_DOWN = 25;
public static final int KEYCODE_POWER = 26;
public static final int KEYCODE_CAMERA = 27;
public static final int KEYCODE_CLEAR = 28;
public static final int KEYCODE_A = 29;
public static final int KEYCODE_B = 30;
public static final int KEYCODE_C = 31;
public static final int KEYCODE_D = 32;
public static final int KEYCODE_E = 33;
public static final int KEYCODE_F = 34;
public static final int KEYCODE_G = 35;
public static final int KEYCODE_H = 36;
public static final int KEYCODE_I = 37;
public static final int KEYCODE_J = 38;
public static final int KEYCODE_K = 39;
public static final int KEYCODE_L = 40;
public static final int KEYCODE_M = 41;
public static final int KEYCODE_N = 42;
public static final int KEYCODE_O = 43;
public static final int KEYCODE_P = 44;
public static final int KEYCODE_Q = 45;
public static final int KEYCODE_R = 46;
public static final int KEYCODE_S = 47;
public static final int KEYCODE_T = 48;
public static final int KEYCODE_U = 49;
public static final int KEYCODE_V = 50;
public static final int KEYCODE_W = 51;
public static final int KEYCODE_X = 52;
public static final int KEYCODE_Y = 53;
public static final int KEYCODE_Z = 54;
public static final int KEYCODE_COMMA = 55;
public static final int KEYCODE_PERIOD = 56;
public static final int KEYCODE_ALT_LEFT = 57;
public static final int KEYCODE_ALT_RIGHT = 58;
public static final int KEYCODE_SHIFT_LEFT = 59;
public static final int KEYCODE_SHIFT_RIGHT = 60;
public static final int KEYCODE_TAB = 61;
public static final int KEYCODE_SPACE = 62;
public static final int KEYCODE_SYM = 63;
public static final int KEYCODE_EXPLORER = 64;
public static final int KEYCODE_ENVELOPE = 65;
public static final int KEYCODE_ENTER = 66;
public static final int KEYCODE_DEL = 67;
public static final int KEYCODE_GRAVE = 68;
public static final int KEYCODE_MINUS = 69;
public static final int KEYCODE_EQUALS = 70;
public static final int KEYCODE_LEFT_BRACKET = 71;
public static final int KEYCODE_RIGHT_BRACKET = 72;
public static final int KEYCODE_BACKSLASH = 73;
public static final int KEYCODE_SEMICOLON = 74;
public static final int KEYCODE_APOSTROPHE = 75;
public static final int KEYCODE_SLASH = 76;
public static final int KEYCODE_AT = 77;
public static final int KEYCODE_NUM = 78;
public static final int KEYCODE_HEADSETHOOK = 79;
public static final int KEYCODE_FOCUS = 80; // *Camera* focus
public static final int KEYCODE_PLUS = 81;
public static final int KEYCODE_MENU = 82;
public static final int KEYCODE_NOTIFICATION = 83;
public static final int KEYCODE_SEARCH = 84;
public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
public static final int KEYCODE_MEDIA_STOP = 86;
public static final int KEYCODE_MEDIA_NEXT = 87;
public static final int KEYCODE_MEDIA_PREVIOUS = 88;
public static final int KEYCODE_MEDIA_REWIND = 89;
public static final int KEYCODE_MEDIA_FAST_FORWARD = 90;
public static final int KEYCODE_MUTE = 91;
---------------------------------完,不明白的给我留言·······················
作者:享有盛誉之名
转载请备注出处