上期回顧: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')
---------------------------------------------------------------------------------
關注微信公衆號即可在手機上查閱,並可接收更多測試分享~