多点触控 MultiAction

MultiAction

MultiAction是多点触控的类,可以模拟用户多点操作。主要包含add()和perform()两个方法,MultiAction可以结合前面所学的ActionTouch可以模拟出用户的多个手指滑动的效果。

1
2
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction

加载:

方法add(self,*touch_actions)将TouchAction对象添加到MultiAction中,稍后执行
参数:
touch_action 一个后多个TouchAction对象,描述一个手指要执行的动作链
用法:
1
2
3
4
5
a1 = TouchAction(driver)
a1.press(x,y).move_to(x,y).release()
a2 = TouchAction(driver)
a2.press(x,y).move_to(x,y).release()
MultiAction(driver).add(a1, a2)

执行

perform(self) 执行存储在对象中的操作。
用法:
1
2
3
4
5
a1 = TouchAction(driver)
a1.press(x,y).move_to(x,y).release()
a2 = TouchAction(driver)
a2.press(x,y).move_to(x,y).release()
MultiAction(driver).add(a1, a2).perform()

手机缩放图示理解

iBjtz9.md.png

实例

在百度APP中执行放大两次再缩小两次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from appium import webdriver
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction

desired_caps = {}
desired_caps['platformName']='Android'
desired_caps['deviceName']='127.0.0.1:62026'
desired_caps['platforVersion']='5.1.1'

desired_caps['app']=r'H:\测试学习\com.baidu.BaiduMap.apk'
desired_caps['appPackage']='com.baidu.BaiduMap'
desired_caps['appActivity']='com.baidu.baidumaps.WelcomeScreen'
desired_caps['noReset']='False'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

driver.implicitly_wait(10)
driver.find_element_by_id('com.baidu.BaiduMap:id/dj2').click()
driver.find_element_by_id('com.baidu.BaiduMap:id/byo').click()

def get_size():
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
return x, y

def pinch():
size = get_size()
x = size[0]
y = size[1]
action1 = TouchAction(driver)
action2 = TouchAction(driver)
pinch_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('pinch...')
pinch_action.add(action1, action2)
pinch_action.perform()

def zoom():
size = get_size()
x = size[0]
y = size[1]
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('zoom...')
zoom_action.add(action1, action2)
zoom_action.perform()

if __name__ == '__main__':
for i in range(2):
pinch()

for i in range(2):
zoom()