前言:android手机大家都很熟悉,操作有按键、触摸、点击、滑动等,各种操作方法可以通过api的方法来实现。
参考博文:http://blog.csdn.net/bear_w/article/details/50330565
1.click
click(self):
Clicks the element(点击元素 )
用法 element.click()
driver.find_element_by_id('com.huawei.camera:id/shutter_button').click()
2.shake
shake(self):
Shake the device(摇一摇手机 )
用法 driver.shake()
driver.shake()
3.close
close(self):
Closes the current window(关闭当前窗口 )
用法: driver.close()
driver.close()
4.quit
quit(self):
Quits the driver and closes every associated window(退出脚本运行并关闭每个相关的窗口连接 )
用法: driver.quit()
driver.quit()
5.size
size(self):
The size of the element【获取元素的大小(高和宽)】
new_size["height"] = size["height"]
new_size["width"] = size["width"]
用法 driver.element.size
driver.get_window_size()['width'] driver.get_window_size()['height']
函数的写法
#创建一个size方法获取手机屏幕大小x,y的函数
def getSize():
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
return (x, y)
#调取函数
w_size=getSize()
6. swipe
swipe(self, start_x, start_y, end_x, end_y, duration=None):
Swipe from one point to another point, for an optional duration(从A点滑动至B点,滑动时间为毫秒)
:Args: - start_x - x-coordinate at which to start (滑动起点x坐标)
- start_y - y-coordinate at which to start(滑动起点y坐标)
- end_x - x-coordinate at which to stop(滑动终点x坐标)
- end_y - y-coordinate at which to stop(滑动终点y坐标)
- duration - (optional) time to take the swipe, in ms.(滑动时间设定,单位ms,默认5ms)
:Usage: driver.swipe(start_x, start_y, end_x, end_y,duration)
用法 driver.swipe(x1,y1,x2,y2,500)
swipe方法需要确定滑动的起点和终点坐标,由于不同手机的分辨率有可能不同,如果指定一个固定的坐标,在其他手机上不一定适用,所以最好结合上面的size方法来获取手机屏幕大小,使用相对坐标定位滑动。
android系统的坐标系,左上角是坐标原点,水平方向是x轴,垂直方向是y轴,如下图所示:
下面代码是结合size方法对四个方向滑动举例:
#size方法获取屏幕大小 def getSize(): x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] return (x, y) #屏幕向上滑动,x轴不变,y轴从大变小 def swipeUp(t): w_size = getSize() x1 = int(w_size[0] * 0.5) #获取x坐标,根据实际调整相乘参数
y1 = int(w_size[1] * 0.8) #获取起始y坐标,根据实际调整相乘参数
y2 = int(w_size[1] * 0.2) #获取终点y坐标,根据实际调整相乘参数 driver.swipe(x1, y1, x1, y2,t) #屏幕向下滑动,x轴不变,y轴从小变大 def swipeDown(t): w_size = getSize() x1 = int(w_size[0] * 0.5) #获取x坐标,根据实际调整相乘参数 y1 = int(w_size[1] * 0.2) #获取起始y坐标,根据实际调整相乘参数 y2 = int(w_size[1] * 0.8) #获取终点y坐标,根据实际调整相乘参数 driver.swipe(x1, y1, x1, y2,t) #屏幕向左滑动,y轴不变,x轴从大变小
def swipeLeft(t): w_size = getSize() x1 = int(w_size[0] * 0.8) #获取起始x坐标,根据实际调整相乘参数 x2 = int(w_size[0] * 0.05) #获取终点x坐标,根据实际调整相乘参数
y1 = int(w_size[1] * 0.5) #获取y坐标,根据实际调整相乘参数
driver.swipe(x1,y1,x2,y1,t) #屏幕向右滑动,y轴不变,x轴从小变大 def swipeRight(t): w_size = getSize()
x1 = int(w_size[0] * 0.05) #获取起始x坐标,根据实际调整相乘参数
x2 = int(w_size[0] * 0.8) #获取终点x坐标,根据实际调整相乘参数
y1 = int(w_size[1] * 0.5) #获取y坐标,根据实际调整相乘参数
driver.swipe(x1,y1,x2,y1,t)
#调用向上滑动,滑动时间参数为500ms swipeUp(500) sleep(2)
#调用向下滑动,滑动时间参数为500ms swipeDown(500)
sleep(2) #调用向左滑动,滑动时间参数为500ms swipeLeft(500) sleep(2) #调用向右滑动,滑动时间参数为500ms swipeRight(500)
7.flick
flick(self, start_x, start_y, end_x, end_y):
Flick from one point to another point(按住A点后快速滑动至B点)
:Args: - start_x - x-coordinate at which to start(滑动起点x坐标)
- start_y - y-coordinate at which to start(滑动起点y坐标)
- end_x - x-coordinate at which to stop(滑动终点x坐标)
- end_y - y-coordinate at which to stop(滑动终点y坐标)
:Usage: driver.flick(100, 100, 100, 400)
用法:driver.flick(start_x,start_y,end_x,end_y)
flick方法和swipe方法一样需要确定起点和终点坐标,只是没有时间参数,相对坐标的获取可以参考swipe方法
大概坐标的快速滑动
driver.flick(100,100,100,600)
结合size方法的快速滑动
#size方法获取屏幕大小 def getSize(): x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] return (x, y) #快速向上滑动,x轴不变,y轴从大变小 def flickUp(): w_size = getSize() x1 = int(w_size[0] * 0.5) #获取x坐标,根据实际调整相乘参数 y1 = int(w_size[1] * 0.8) #获取起始y坐标,根据实际调整相乘参数 y2 = int(w_size[1] * 0.2) #获取终点y坐标,根据实际调整相乘参数 driver.flick(x1, y1, x1, y2) #调用快速向上滑动 flickUp()
8. keyevent
keyevent(self, keycode, metastate=None):
Sends a keycode to the device. Android only. Possible keycodes can be found in http://developer.android.com/reference/android/view/KeyEvent.html【发送按键码(安卓仅有),按键码可以上网址中找到 】
:Args:
- keycode - the keycode to be sent to the device
- metastate - meta information about the keycode being sent
用法 :driver.keyevent(‘4’)
keyevent方法直接发送按键码就可以了,如返回键操作
driver.keyevent('4') #返回键操作
下面是按键码列表
电话键
KEYCODE_CALL (拨号键) : 5
KEYCODE_ENDCALL (挂机键) : 6
KEYCODE_HOME (按键Home) : 3
KEYCODE_MENU (菜单键) : 82
KEYCODE_BACK (返回键) : 4
KEYCODE_SEARCH (搜索键) : 84
KEYCODE_CAMERA (拍照键) : 27
KEYCODE_FOCUS (拍照对焦键) :80
KEYCODE_POWER (电源键) : 26
KEYCODE_NOTIFICATION (通知键) : 83
KEYCODE_MUTE (话筒静音键) : 91
KEYCODE_VOLUME_MUTE (扬声器静音键) : 164
KEYCODE_VOLUME_UP (音量增加键) : 24
KEYCODE_VOLUME_DOWN (音量减小键) : 25
控制键
KEYCODE_ENTER (回车键) : 66
KEYCODE_ESCAPE (ESC键) : 111
KEYCODE_DPAD_CENTER (导航键 确定键) : 23
KEYCODE_DPAD_UP (导航键 向上) : 19
KEYCODE_DPAD_DOWN (导航键 向下) : 20
KEYCODE_DPAD_LEFT (导航键 向左) : 21
KEYCODE_DPAD_RIGHT (导航键 向右) : 22
KEYCODE_MOVE_HOME (光标移动到开始键) : 122
KEYCODE_MOVE_END (光标移动到末尾键) : 123
KEYCODE_PAGE_UP (向上翻页键) : 92
KEYCODE_PAGE_DOWN (向下翻页键) : 93
KEYCODE_DEL (退格键) : 67
KEYCODE_FORWARD_DEL (删除键) : 112
KEYCODE_INSERT (插入键) : 124
KEYCODE_TAB (Tab键) : 61
KEYCODE_NUM_LOCK (小键盘锁) : 143
KEYCODE_CAPS_LOCK (大写锁定键) : 115
KEYCODE_BREAK (Break/Pause键) : 121
KEYCODE_SCROLL_LOCK (滚动锁定键) : 116
KEYCODE_ZOOM_IN (放大键) : 168
KEYCODE_ZOOM_OUT (缩小键) : 169
基本
KEYCODE_0 (按键'0') : 7
KEYCODE_1 (按键'1') : 8
KEYCODE_2 (按键'2') : 9
KEYCODE_3 (按键'3') : 10
KEYCODE_4 (按键'4') : 11
KEYCODE_5 (按键'5') : 12
KEYCODE_6 (按键'6') : 13
KEYCODE_7 (按键'7') : 14
KEYCODE_8 (按键'8') : 15
KEYCODE_9 (按键'9') : 16
KEYCODE_A (按键'A') : 29
KEYCODE_B (按键'B') : 30
KEYCODE_C (按键'C') : 31
KEYCODE_D (按键'D') : 32
KEYCODE_E (按键'E') : 33
KEYCODE_F (按键'F') : 34
KEYCODE_G (按键'G') : 35
KEYCODE_H (按键'H') : 36
KEYCODE_I (按键'I' ) : 37
KEYCODE_J (按键'J') : 38
KEYCODE_K (按键'K') : 39
KEYCODE_L (按键'L' ) : 40
KEYCODE_M (按键'M') : 41
KEYCODE_N (按键'N') : 42
KEYCODE_O (按键'O') : 43
KEYCODE_P (按键'P') : 44
KEYCODE_Q (按键'Q') : 45
KEYCODE_R (按键'R' ) : 46
KEYCODE_S (按键'S') : 47
KEYCODE_T (按键'T') : 48
KEYCODE_U (按键'U') : 49
KEYCODE_V (按键'V') : 50
KEYCODE_W (按键'W') : 51
KEYCODE_X (按键'X') : 52
KEYCODE_Y (按键'Y') : 53
KEYCODE_Z (按键'Z') : 54