前言:android手機你們都很熟悉,操做有按鍵、觸摸、點擊、滑動等,各類操做方法能夠經過api的方法來實現。html
參考博文:http://blog.csdn.net/bear_w/article/details/50330565python
1.clickandroid
click(self):web
Clicks the element(點擊元素 )api
用法 element.click()session
driver.find_element_by_id('com.huawei.camera:id/shutter_button').click()
2.shakeapp
shake(self):ide
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):
用法 driver.swipe(x1,y1,x2,y2,500)
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)
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座標,根據實際調整相乘參數 <br> y1 = int(w_size[1] * 0.8) #獲取起始y座標,根據實際調整相乘參數 <br> 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軸從大變小<br>def swipeLeft(t): w_size = getSize() x1 = int(w_size[0] * 0.8) #獲取起始x座標,根據實際調整相乘參數 x2 = int(w_size[0] * 0.05) #獲取終點x座標,根據實際調整相乘參數<br> y1 = int(w_size[1] * 0.5) #獲取y座標,根據實際調整相乘參數<br> driver.swipe(x1,y1,x2,y1,t) #屏幕向右滑動,y軸不變,x軸從小變大 def swipeRight(t): w_size = getSize()<br> x1 = int(w_size[0] * 0.05) #獲取起始x座標,根據實際調整相乘參數<br> x2 = int(w_size[0] * 0.8) #獲取終點x座標,根據實際調整相乘參數<br> y1 = int(w_size[1] * 0.5) #獲取y座標,根據實際調整相乘參數<br> driver.swipe(x1,y1,x2,y1,t)<br> #調用向上滑動,滑動時間參數爲500ms swipeUp(500) sleep(2)<br>#調用向下滑動,滑動時間參數爲500ms swipeDown(500)<br>sleep(2) #調用向左滑動,滑動時間參數爲500ms swipeLeft(500) sleep(2) #調用向右滑動,滑動時間參數爲500ms swipeRight(500)
7.flick
flick(self, start_x, start_y, end_x, end_y):
driver,flick(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)
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):
用法 driver.keyevent(‘4’)
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
keyevent方法直接發送按鍵碼就可了,如返回鍵操做
下面是按鍵碼列表
電話鍵
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
9.send_keys
send_keys(self, *value):
用法 driver.element.send_keys(「中英」)
Simulates typing into the element【在元素中模擬輸入(開啓appium自帶的輸入法並配置了appium輸入法後,能夠輸入中英文)】 :Args: - value - A string for typing, or setting form fields. For setting file inputs, this could be a local file path. Use this to send simple key events or to fill out form fields:: form_textfield = driver.find_element_by_name('username') form_textfield.send_keys("admin") This can also be used to set file inputs. file_input = driver.find_element_by_name('profilePic') file_input.send_keys("path/to/profilepic.gif") # Generally it's better to wrap the file path in one of the methods # in os.path to return the actual path to support cross OS testing. # file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))
send_keys方法須要在配置Capabilities信息時打開模擬鍵盤unicodeKeyboard與resetKeyboard,以下面代碼舉例:
from appium import webdriver import time desired_caps = { 'platformName' : 'Android', 'deviceName' : '76P4C15813005463', 'platformVersion' : '5.1', #測試apk包名 'appPackage' : 'com.huawei.android.launcher', #測試apk的launcherActivity 'appActivity' : '.Launcher', #打開模擬鍵盤 'unicodeKeyboard' : True , 'resetKeyboard' : True, } #進入android系統launcher driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) time.sleep(5) #進入應用市場 driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'應用市場)]").click() #使用模擬鍵盤輸入'今日頭條' driver.find_element_by_id('com.huawei.appmarket:id/search_edit_text_view').send_keys(u"今日頭條") driver.find_element_by_id('com.huawei.appmarket:id/search_icon_view').click() time.sleep(5) driver.quit()
10. press_keycode
press_keycode(self, keycode, metastate=None):
用法 driver.press_ keycode(‘4’)
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 dr.keyevent(‘4’)與driver.press_ keycode(‘4’) 功能實現上同樣的,都是按了返回鍵
11. long_press_keycode
long_press_keycode(self, keycode, metastate=None):
用法 driver.long_press_keycode(‘4’)
Sends a long press of 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
12.tap
tap(self, positions, duration=None):
用法 driver.tap([(x,y),(x1,y1)],500)
Taps on an particular place with up to five fingers, holding for a certain time 【模擬手指點擊(最多五個手指),可設置按住時間長度(毫秒)】 :Args: - positions - an array of tuples representing the x/y coordinates of the fingers to tap. Length can be up to five. - duration - (optional) length of time to tap, in ms :Usage: driver.tap([(100, 20), (100, 60), (100, 100)], 500) 當控件沒法獲取時,那咱們就可使用座標用tap方法作點擊操做,並且tap方法能夠用於多點點擊。
driver.tap([(300,500)],10)
13.zoom
zoom(self, element=None, percent=200, steps=50):
用法 driver.zoom(element)
Zooms in on an element a certain amount (在元素上執行放大操做) :Args: - element - the element to zoom - percent - (optional) amount to zoom. Defaults to 200% :Usage: driver.zoom(element)
zoom方法用來模擬手機上的放大操做,主要是要肯定element,具體例子和下面pinch方法一塊兒講。
el=driver.find_element_by_class_name('android.widget.RelativeLayout') driver.zoom(el,150,30) #percent參數和steps能夠不寫,不寫保持默認數值
14.pinch
pinch(self, element=None, percent=200, steps=50):
用法 driver.pinch(element)
Pinch on an element a certain amount 在元素上執行模擬雙指捏(縮小操做) :Args: - element - the element to pinch - percent - (optional) amount to pinch. Defaults to 200% - steps - (optional) number of steps in the pinch action :Usage: driver.pinch(element)
pinch方法用來模擬手機上的縮小操做,主要是要肯定element,下面舉例進入圖庫查看圖片時放大和縮小圖片,使用uiautomatorviewer.bat工具來定位元素。
(1)進入圖庫,以下圖所示,圖庫控件text是惟一的,因此咱們採用by_name方法獲取元素。
driver.find_element_by_name('圖庫')
(2)選擇圖庫的一個文件夾,以下圖所示,選擇雜誌鎖屏,因爲該元素text也是惟一的,因此使用by_name方法獲取控件元素。
driver.find_element_by_name('雜誌鎖屏')
(3)選擇一張圖片,所下圖所示,下面圖片是一個整的佈局,沒有單獨分開的控件元素,因此咱們只能選擇使用tap方法點擊屏幕。
driver.tap([(300,500)],50)
(4)放大和縮小圖片,以下圖所示,整個圖片是一個佈局,並且只有class信息,因爲放大和縮小須要得到element,因此咱們使用class_name的
方法獲取整個佈局做爲元素。
el=driver.find_element_by_class_name('android.widget.RelativeLayout')
driver.zoom(el)
driver.pinch(el,200,50)
上面操做的具體腳本以下
from appium import webdriver
import time
desired_caps = {
'platformName' : 'Android',
'deviceName' : '76P4C15813005463',
'platformVersion' : '5.1',
#測試apk包名
'appPackage' : 'com.huawei.android.launcher',
#測試apk的launcherActivity
'appActivity' : '.Launcher',
}
#進入android系統launcher
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)
#進入圖庫
driver.find_element_by_name('圖庫').click()
driver.find_element_by_name('雜誌鎖屏').click()
driver.tap([(300,500)],50)
time.sleep(1)
el=driver.find_element_by_class_name('android.widget.RelativeLayout')
#放大圖片
driver.zoom(el)
time.sleep(5)
#縮小圖片
driver.pinch(el,200,50)
time.sleep(5)
driver.quit()
15.scroll
scroll(self, origin_el, destination_el):
用法 :driver.scroll(el1,el2)
Scrolls from one element to another 從元素origin_el滾動至元素destination_el :Args: - originalEl - the element from which to being scrolling - destinationEl - the element to scroll to :Usage: driver.scroll(el1, el2)
16. drag_and_drop
drag_and_drop(self, origin_el, destination_el):
用法 :driver.drag_and_drop()
Drag the origin element to the destination element 將元素origin_el拖到目標元素destination_el :Args: - originEl - the element to drag - destinationEl - the element to drag to
17. hide_keyboard
hide_keyboard(self, key_name=None, key=None, strategy=None):
用法 :driver.hide_keyboard()
Hides the software keyboard on the device. In iOS, use `key_name` to press a particular key, or `strategy`. In Android, no parameters are used. 隱藏鍵盤,iOS使用key_name隱藏,安卓不使用參數 :Args: - key_name - key to press - strategy - strategy for closing the keyboard (e.g., `tapOutside`)
17.lock
lock(self, seconds):
用法 :driver.lock()
Lock the device for a certain period of time. iOS only.
鎖屏一段時間 iOS專有
:Args:
- the duration to lock the device, in seconds
18.contexts
contexts(self):
用法 :driver.contexts()
Returns the contexts within the current session.
返回當前會話中的上下文,使用後能夠識別H5頁面的控件
:Usage:
driver.contexts
19. current_context
current_context(self):
用法 :driver.current_context()
Returns the current context of the current session.
返回當前會話的當前上下文
:Usage:
driver.current_context