背景
在使用地图App、图片放大缩小场景中,我们经常需要对界面进行缩放操作来更加便利的查看位置或者图片放大操作。那么在Appium中怎样去模拟这类操作呢?
在 Appium 里面提供了 MultiAction 类来进行放大、缩小操作。
放大、缩小原理图
MultiAction 简介
MultiAction 是多点触控的类,可以模拟用户多点操作。主要包含 add() 和 perform() 两个方法, MultiAction可以结合前面所学的 ActionTouch可以模拟出用户的多个手指滑动的操作效果。
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction
add 方法
方法add(self, *touch_actions: 'TouchAction')
将TouchAction
对象添加到MultiAction中,后面再执行
def add(self, *touch_actions: 'TouchAction') -> None:
"""Add TouchAction objects to the MultiAction, to be performed later.
Args:
touch_actions: one or more TouchAction objects describing a chain of actions to be performed by one finger
Usage:
| 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)
Returns:
`MultiAction`: Self instance
"""
for touch_action in touch_actions:
if self._touch_actions is None:
self._touch_actions = []
self._touch_actions.append(copy.copy(touch_action))
参数说明:
- touch_actions - 一个或多个TouchAction对象,描述一个手指要执行的动作
perform 方法
perform(self) 执行存储在对象中的操作。
def perform(self: T) -> T:
"""Perform the actions stored in the object.
Usage:
| 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()
Returns:
`MultiAction`: Self instance
"""
self._driver.execute(Command.MULTI_ACTION, self.json_wire_gestures)
# clean up and be ready for the next batch
self._touch_actions = []
return self
用法
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction
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()
实践—高德地图缩放
核心代码
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
def narrow():
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()
zoom_action.add(action1,action2)
zoom_action.perform()
def enlarge():
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()
zoom_action.add(action1,action2)
zoom_action.perform()
if __name__ == '__main__':
for i in range(5):
narrow()
for i in range(5):
enlarge()
以上内容纯属个人理解,如有不足,欢迎各位大神指正!
如果觉得文章不错,欢迎关注微信公众号,微信公众号定期推送相关测试技术文章