前言: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