python+Appium自动化:MultiAction多点触控

MultiAction

MultiAction 是多点触控的类,常用于模拟用户多点操作。

主要包含这add()还有perform()两个方法,模拟多点触控,需要导入TouchAction还有MultiAction两个模块。

from appium.webdriver.common.multi_action import MultiAction

from appium.webdriver.common.touch_action import TouchAction


加载:

方法add(self, *touch_actions)将TouchAction对象添加到MultiAction中

touch_actions 是一个或多个TouchAction对象,描述一个手指要执行的动作链

例子:
action1=TouchAction(driver)
action1.press(el1).move_to(el2).release()

action2=TouchAction(driver)

action2.press(el2).move_to(el1).release()

MultiAction(driver).add(action1, action2)

执行:

perform(self) 执行存储在对象中的操作
 
action1=TouchAction(driver)
action1.press(el1).move_to(el2).release()

action2=TouchAction(driver)

action2.press(el2).move_to(el1).release()

MultiAction(driver).add(action1, action2).perform()

 

场景:百度地图的缩放

python+Appium自动化:MultiAction多点触控

 代码:

# -*- coding: utf-8 -*-#
from appium import webdriver
import time
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction

desired_caps = {
               "platformName": "Android",
               "platformVersion": "5.1",
               "deviceName": "U4KF9HSK99999999",
               "appPackage": "com.baidu.BaiduMap",
               "appActivity": "com.baidu.baidumaps.MapsActivity",
               "unicodeKeyboard":True,
               "resetKeyboard":True,
               "noReset": True
                }
driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps)
driver.implicitly_wait(5)
#获取窗口尺寸

size=driver.get_window_size()
print(size)
x=size[‘width‘]
y=size[‘height‘]
time.sleep(2)
#缩放
def pinch():
    action1=TouchAction(driver)
    action2=TouchAction(driver)
    mul_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()
    #执行
    print("-----start pinch-----")
    mul_action.add(action1,action2)
    mul_action.perform()
#放大
def zoom():
    action1=TouchAction(driver)
    action2=TouchAction(driver)
    mul_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.2,y=y*0.2).wait(1000).release()
    #执行
    print("-----start zoom-----")
    mul_action.add(action1,action2)
    mul_action.perform()

if __name__=="__main__":

    for i in range(2):
        time.sleep(2)
        pinch()

    for i in range(2):
        time.sleep(2)
        zoom()
move_to是计算手机的偏移量,用负数控制滑动方向。