前言: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):
用法 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