上一篇介紹了TouchAction操做,可能不夠完善,今天在進行補充一個多點觸控操做的MultiActionandroid
MultiAction
MultiAction是多點觸控的操做的類,能夠模擬用戶的多點操做,主要包含加載add()和執行perform()兩個方法,大多數結合TouchAction模塊進行使用web
源碼:shell
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中json
# 建立一個touchAction action1=TouchAction(driver) # 添加一個座標 action1.press(el1).move_to(el2).release() # 經過add添加到MultiAction MultiAction(driver).add(action1, action2)
perform()
方法perform(self)執行操做app
# 建立一個touchAction action1=TouchAction(driver) # 添加一個座標 action1.press(el1).move_to(el2).release() # 經過add添加到MultiAction並執行 MultiAction(driver).add(action1, action2).perform()
小試牛刀
咱們經過MultiAction能夠完成對圖片的放大和縮小,以及對屏幕的放大和縮小。測試
寫到這裏,安靜在網上找到了一張屏幕分割圖,咱們能夠參考圖上的座標而後來完成放大和縮小編碼
咱們經過上圖進行完成對圖片的放大和縮小spa
放大圖片:code
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()
縮小圖片:orm
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()