Airtest API精讲之放大缩小pinch()

上期回顾:Airtest API精讲之双指滑动two_finger_swipe()


以下基于
python3.8;airtestIDE1.2.11;airtest1.2.2;pocoui1.0.83

上次我们讲了two_finger_swipe(),如果你看了之前的解析,那pinch()也就顺理成章了。

老规矩开场白,我们今天要讲的是Airtest框架的pinch(),不是Poco框架的,一般我们说Airtest,其实应该指的是Airtest Project,具体这些概念的关系是什么,可以看之前文章:Airtest Project——UI自动化利器介绍

首先说明一点,某些模拟器如果你选择的连接参数中有"Use Adb Touch",可能将无法使用pinch(),不行就换真机吧。

源码解析

我们先来看下pinch()的源码(不想看源码的可以直接跳到后面的演示实例):

 1# 文件位置:your_python_path/site-packages/airtest/core/android/touch_methods/base_touch.py
2    def pinch(self, center=None, percent=0.5, duration=0.5, steps=5, in_or_out='in'):
3        w, h = self.size_info['width'], self.size_info['height']
4        if isinstance(center, (list, tuple)):
5            x0, y0 = center
6        elif center is None:
7            x0, y0 = w / 2, h / 2
8        else:
9            raise TypeError("center should be None or list/tuple, not %s" % repr(center))
10
11        x1, y1 = x0 - w * percent / 2, y0 - h * percent / 2
12        x2, y2 = x0 + w * percent / 2, y0 + h * percent / 2
13        pinch_events = []
14        interval = float(duration) / (steps + 1)
15        # 根据in还是out,设定双指滑动的起始和结束座标
16        if in_or_out == 'in':
17            start_pos1_x, start_pos1_y = x1, y1
18            start_pos2_x, start_pos2_y = x2, y2
19            end_pos1_x, end_pos1_y = x0, y0
20            end_pos2_x, end_pos2_y = x0, y0
21        else:
22            start_pos1_x, start_pos1_y = x0, y0
23            start_pos2_x, start_pos2_y = x0, y0
24            end_pos1_x, end_pos1_y = x1, y1
25            end_pos2_x, end_pos2_y = x2, y2
26        # 开始定义pinch的操作
27        pinch_events.extend([
28            DownEvent((start_pos1_x, start_pos1_y), contact=0, pressure=self.default_pressure),
29            DownEvent((start_pos2_x, start_pos2_y), contact=1, pressure=self.default_pressure)
30        ])
31        for i in range(1, steps):
32            pinch_events.extend([
33                SleepEvent(interval),
34                MoveEvent((start_pos1_x + (end_pos1_x - start_pos1_x) * i / steps,
35                           start_pos1_y + (end_pos1_y - start_pos1_y) * i / steps),
36                          contact=0, pressure=self.default_pressure),
37                MoveEvent((start_pos2_x + (end_pos2_x - start_pos2_x) * i / steps,
38                           start_pos2_y + (end_pos2_y - start_pos2_y) * i / steps),
39                          contact=1, pressure=self.default_pressure)
40            ])
41        pinch_events.extend([
42            SleepEvent(interval),
43            MoveEvent((end_pos1_x, end_pos1_y), contact=0, pressure=self.default_pressure),
44            MoveEvent((end_pos2_x, end_pos2_y), contact=1, pressure=self.default_pressure)
45        ])
46        pinch_events.extend([UpEvent(contact=0), UpEvent(contact=1)])
47        self.perform(pinch_events)

参数:
center:执行缩放的中心点,可以是list或tuple类型,默认为None
percent:以半个屏幕的百分比长度做为pinch动作的长度,默认值为0.5(具体算法看下面解释)
duration:时间间隔,默认0.5秒
steps:执行步数,默认5
in_or_out:in是缩小,out是放大,默认是in

源码解析:
第4-9行,判断center参数是否合法,并定义中心座标点x0,y0;如果是None,将以屏幕中心点为执行点。

第11-12行

x1, y1 = x0 - w * percent / 2, y0 - h * percent / 2
x2, y2 = x0 + w * percent / 2, y0 + h * percent / 2

根据0号座标点计算出1号、2号座标点。
举个例子,假如屏幕大小是10001000,center用默认值500,500,percent用默认值0.5,则 x1,y1 = 500-10000.5/2,500-1000*0.5/2=250,250
x2,y2 = 750,750

图片

第16-25行,根据in_or_out参数来定义1号、2号座标点到底是起点还是终点。如果是in,则1号、2号座标点是起点,0号座标点是终点,即同时从1、2号座标滑向0号座标;反之亦然。

第27-46行,逐步向缩放列表pinch_events添加两指按下、滑动、擡起操作,以上代码中contact=0代表第一根手指,contact=1代表第二根手指。具体流程的含义可以参考Airtest API精讲之双指滑动two_finger_swipe(),这里就不再重复了。

第47行self.perform(swipe_events)将缩放事件列表中的每个事件依次执行

看,是不是和two_finger_swipe()很像,two_finger_swipe()是两指平行操作,pinch()是两指相对操作。

 

演示实例

缩小

# -*- encoding=utf8 -*-
__author__ = "测试工程师小站"

from airtest.core.api import *

# 获取当前手机设备
dev = device()
# 参数全部用默认
dev.touch_proxy.pinch()

 

图片

放大

# -*- encoding=utf8 -*-
__author__ = "测试工程师小站"

from airtest.core.api import *

# 获取当前手机设备
dev = device()
# 从[550, 1100],按半个屏幕的0.4倍向外放大,持续3秒,分3步
dev.touch_proxy.pinch(center=[550, 1100], percent=0.4, duration=3, steps=3, in_or_out='out')

 

图片

 

---------------------------------------------------------------------------------

关注微信公众号即可在手机上查阅,并可接收更多测试分享~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章