上一篇介绍了TouchAction操作,可能不够完善,今天在进行补充一个多点触控操作的MultiAction
MultiAction
MultiAction是多点触控的操作的类,可以模拟用户的多点操作,主要包含加载add()和执行perform()两个方法,大多数结合TouchAction模块进行使用
源码:
class MultiAction(object): def __init__(self, driver, element=None): self._driver = driver self._element = element self._touch_actions = [] def add(self, *touch_actions): """将TouchAction对象添加到MultiAction中,稍后执行。 参数: - touch_actions -一个或多个TouchAction对象,描述一个手指要执行的操作链 :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) """ for touch_action in touch_actions: if self._touch_actions is None: self._touch_actions = [] self._touch_actions.append(copy.copy(touch_action)) def perform(self): """执行存储在对象中的操作. :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() """ self._driver.execute(Command.MULTI_ACTION, self.json_wire_gestures) # clean up and be ready for the next batch self._touch_actions = [] return self
add()
方法add(self, *touch_actions)将TouchAction对象添加到MultiAction中
# 创建一个touchAction action1=TouchAction(driver) # 添加一个坐标 action1.press(el1).move_to(el2).release() # 通过add添加到MultiAction MultiAction(driver).add(action1, action2)
perform()
方法perform(self)执行操作
# 创建一个touchAction action1=TouchAction(driver) # 添加一个坐标 action1.press(el1).move_to(el2).release() # 通过add添加到MultiAction并执行 MultiAction(driver).add(action1, action2).perform()
小试牛刀
我们通过MultiAction可以完成对图片的放大和缩小,以及对屏幕的放大和缩小。
写到这里,安静在网上找到了一张屏幕分割图,我们可以参考图上的坐标然后来完成放大和缩小
我们通过上图进行完成对图片的放大和缩小
放大图片:
from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.common.multi_action import MultiAction from appium import webdriver import time import os desired_caps = { ‘platformName‘: ‘Android‘, # 测试版本 ‘deviceName‘: ‘emulator-5554‘, # 设备名 ‘platformVersion‘: ‘5.1.1‘, # 系统版本 "appPackage": "com.android.gallery", # app包名 "appActivity": "com.android.camera.GalleryPicker", # 启动launch Activity "noReset": True, # 不清空数据 "unicodeKeyboard": True, # 使用Unicode编码方式发送字符串 "resetKeyboard": True, # 键盘隐藏起来 } driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps) time.sleep(10) # 通过adb shell input 进入相册中 adb = ‘adb shell input tap 150 200‘ os.popen(adb) time.sleep(3) os.popen(adb) # 获取设备的分别率 size=driver.get_window_size() print(size) x=size[‘width‘] y=size[‘height‘] time.sleep(5) # 调用touchAction模块 action1 = TouchAction(driver) action2 = TouchAction(driver) # 调用MultiAction模块 m_action=MultiAction(driver) action1.press(x=x*0.5,y=y*0.4).wait(1000).move_to(x=x*0.5,y=y*0.2).wait(1000).release() action2.press(x=x*0.5,y=y*0.6).wait(1000).move_to(x=x*0.5,y=y*0.7).wait(1000).release() time.sleep(10) # 添加到MultiAction中, m_action.add(action1,action2) # 执行 m_action.perform()
缩小图片:
from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.common.multi_action import MultiAction from appium import webdriver import time import os desired_caps = { ‘platformName‘: ‘Android‘, # 测试版本 ‘deviceName‘: ‘emulator-5554‘, # 设备名 ‘platformVersion‘: ‘5.1.1‘, # 系统版本 "appPackage": "com.android.gallery", # app包名 "appActivity": "com.android.camera.GalleryPicker", # 启动launch Activity "noReset": True, # 不清空数据 "unicodeKeyboard": True, # 使用Unicode编码方式发送字符串 "resetKeyboard": True, # 键盘隐藏起来 } driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps) time.sleep(10) # 通过adb shell input 进入相册中 adb = ‘adb shell input tap 150 200‘ os.popen(adb) time.sleep(3) os.popen(adb) # 获取设备的分别率 size=driver.get_window_size() print(size) x=size[‘width‘] y=size[‘height‘] time.sleep(5) # 调用touchAction模块 action1 = TouchAction(driver) action2 = TouchAction(driver) # 调用MultiAction模块 m_action=MultiAction(driver) action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.2,y=y*0.2).wait(1000).release() action2.press(x=x*0.8,y=y*0.8).wait(1000).move_to(x=x*0.2,y=y*0.2).wait(1000).release() time.sleep(10) # 添加到MultiAction中, m_action.add(action1,action2) # 执行 m_action.perform()