1. 程式人生 > 其它 >Airtest API精講之放大縮小pinch()

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
2defpinch(self,center=None,percent=0.5,duration=0.5,steps=5,in_or_out='in'):
3w,h=self.size_info['width'],self.size_info['height']
4ifisinstance(center,(list,tuple)):
5x0,y0=center
6elifcenterisNone:
7x0,y0=w/2,h/2
8else:
9raiseTypeError("centershouldbeNoneorlist/tuple,not%s"%repr(center))
10
11x1,y1=x0-w*percent/2,y0-h*percent/2
12x2,y2=x0+w*percent/2,y0+h*percent/2
13pinch_events=[]
14interval=float(duration)/(steps+1)
15#根據in還是out,設定雙指滑動的起始和結束座標
16ifin_or_out=='in':
17start_pos1_x,start_pos1_y=x1,y1
18start_pos2_x,start_pos2_y=x2,y2
19end_pos1_x,end_pos1_y=x0,y0
20end_pos2_x,end_pos2_y=x0,y0
21else:
22start_pos1_x,start_pos1_y=x0,y0
23start_pos2_x,start_pos2_y=x0,y0
24end_pos1_x,end_pos1_y=x1,y1
25end_pos2_x,end_pos2_y=x2,y2
26#開始定義pinch的操作
27pinch_events.extend([
28DownEvent((start_pos1_x,start_pos1_y),contact=0,pressure=self.default_pressure),
29DownEvent((start_pos2_x,start_pos2_y),contact=1,pressure=self.default_pressure)
30])
31foriinrange(1,steps):
32pinch_events.extend([
33SleepEvent(interval),
34MoveEvent((start_pos1_x+(end_pos1_x-start_pos1_x)*i/steps,
35start_pos1_y+(end_pos1_y-start_pos1_y)*i/steps),
36contact=0,pressure=self.default_pressure),
37MoveEvent((start_pos2_x+(end_pos2_x-start_pos2_x)*i/steps,
38start_pos2_y+(end_pos2_y-start_pos2_y)*i/steps),
39contact=1,pressure=self.default_pressure)
40])
41pinch_events.extend([
42SleepEvent(interval),
43MoveEvent((end_pos1_x,end_pos1_y),contact=0,pressure=self.default_pressure),
44MoveEvent((end_pos2_x,end_pos2_y),contact=1,pressure=self.default_pressure)
45])
46pinch_events.extend([UpEvent(contact=0),UpEvent(contact=1)])
47self.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-1000

0.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__="測試工程師小站"

fromairtest.core.apiimport*

#獲取當前手機裝置
dev=device()
#引數全部用預設
dev.touch_proxy.pinch()

放大

#-*-encoding=utf8-*-
__author__="測試工程師小站"

fromairtest.core.apiimport*

#獲取當前手機裝置
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')

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

關注微信公眾號即可在手機上查閱,並可接收更多測試分享~