在使用地图App中,我们经常需要对界面进行缩放操作来更加便利的查看位置。那么在Appium中怎样去模拟这类操作呢?
MultiAction 是多点触控的类,可以模拟用户多点操作。主要包含 add() 和 perform() 两个方法, MultiAction可以结合前面所学的 ActionTouch可以模拟出用户的多个手指滑动的操作效果;
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction
方法add(self, *touch_actions)将TouchAction对象添加到MultiAction中,稍后再执行。
a1 = TouchAction(driver)
a1.press(el1).move_to(el2).release()
a2 = TouchAction(driver)
a2.press(el2).move_to(el1).release()
MultiAction(driver).add(a1, a2)
perform(self) 执行存储在对象中的操作。
a1 = TouchAction(driver)
a1.press(el1).move_to(el2).release()
a2 = TouchAction(driver)
a2.press(el2).move_to(el1).release()
MultiAction(driver).add(a1, a2).perform()
Ps:是不是有点类似Python里面的多线程和多进程的使用。
安装启动百度地图Android app 进入地图后分别进行放大缩小操作
multi_action.py
from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction
desired_caps={}
desired_caps['platformName']='Android'
desired_caps['deviceName']='127.0.0.1:62025'
desired_caps['platforVersion']='5.1.1'
desired_caps['app']=r'C:\Users\Shuqing\Desktop\com.baidu.BaiduMap.apk'
desired_caps['appPackage']='com.baidu.BaiduMap'
desired_caps['appActivity']='com.baidu.baidumaps.WelcomeScreen'
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(5)
driver.find_element_by_id('com.baidu.BaiduMap:id/dj2').click()
driver.find_element_by_id('com.baidu.BaiduMap:id/byo').click()
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
def pinch():
action1=TouchAction(driver)
action2=TouchAction(driver)
zoom_action=MultiAction(driver)
action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).wait(1000).release()
action2.press(x=x*0.8,y=y*0.8).wait(1000).move_to(x=x*0.6,y=y*0.6).wait(1000).release()
print('start pinch...')
zoom_action.add(action1,action2)
zoom_action.perform()
def zoom():
action1=TouchAction(driver)
action2=TouchAction(driver)
zoom_action=MultiAction(driver)
action1.press(x=x*0.4,y=y*0.4).wait(1000).move_to(x=x*0.2,y=y*0.2).wait(1000).release()
action2.press(x=x*0.6,y=y*0.6).wait(1000).move_to(x=x*0.8,y=y*0.8).wait(1000).release()
print('start zoom...')
zoom_action.add(action1,action2)
zoom_action.perform()
if __name__ == '__main__':
for i in range(3):
pinch()
for i in range(3):
zoom()
http://appium.io/docs/cn/writing-running-appium/touch-actions/
https://stackoverflow.com/questions/38565116/zoom-action-in-android-using-appium-python-client