1. 程式人生 > 其它 >Airtest API精講之雙指滑動two_finger_swipe()

Airtest API精講之雙指滑動two_finger_swipe()

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

之前我們講Airtest API精講之swipe()的時候,有提到一個引數fingers,當給其賦值為2時,就是兩個手指滑動。當時給的例子是橫滑,可以出現上下兩條線,但如果是豎滑,兩條線就會重疊成一條線,只是有個先後罷了。今天給大家介紹一個two_finger_swipe(),讓我們可以自定義出更符合自己要求的雙指滑動。

老規矩開場白,我們今天要講的是Airtest框架的two_finger_swipe(),不是Poco框架的,一般我們說Airtest,其實應該指的是Airtest Project,具體這些概念的關係是什麼,可以看之前文章:

Airtest Project——UI自動化利器介紹

首先說明一點,某些模擬器如果你選擇的連線引數中有"Use Adb Touch",可能將無法使用two_finger_swipe(),不行就換真機吧。

原始碼解析

我們先來看下two_finger_swipe()的原始碼(不想看原始碼的可以直接跳到後面的演示例項):

# 檔案位置:your_python_path/site-packages/airtest/core/android/touch_methods/base_touch.py
def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50)):
""" Perform two finger swipe action minitouch protocol example:: d 0 0 0 50 d 1 1 0 50 c m 0 20 0 50 m 1 21 0 50 c m 0 40 0 50 m 1 41 0 50 c m 0 60 0 50 m 1 61 0 50 c m 0 80 0 50 m 1 81 0 50 c m 0 100 0 50 m 1 101 0 50 c u 0 u 1 c Args: tuple_from_xy: start point tuple_to_xy: end point duration: time interval for swipe duration, default is 0.8 steps: size of swipe step, default is 5 offset: coordinate offset of the second finger, default is (0, 50) Returns: None
""" from_x, from_y = tuple_from_xy to_x, to_y = tuple_to_xy # 根據偏移量計算第二個手指的座標 from_x2, from_y2 = (min(max(0, from_x + offset[0]), self.size_info['width']), min(max(0, from_y + offset[1]), self.size_info['height'])) to_x2, to_y2 = (min(max(0, to_x + offset[0]), self.size_info['width']), min(max(0, to_y + offset[1]), self.size_info['height'])) swipe_events = [DownEvent(tuple_from_xy, contact=0, pressure=self.default_pressure), DownEvent((from_x2, from_y2), contact=1, pressure=self.default_pressure), ] interval = float(duration) / (steps + 1) for i in range(1, steps + 1): move_events = [ SleepEvent(interval), MoveEvent((from_x + ((to_x - from_x) * i / steps), from_y + (to_y - from_y) * i / steps), contact=0, pressure=self.default_pressure), MoveEvent((from_x2 + (to_x2 - from_x2) * i / steps, from_y2 + (to_y2 - from_y2) * i / steps), contact=1, pressure=self.default_pressure), ] swipe_events.extend(move_events) swipe_events.extend([UpEvent(contact=0), UpEvent(contact=1)]) self.perform(swipe_events)

引數:
tuple_from_xy:開始座標
tuple_to_xy:結束座標
duration:時間間隔,預設0.8秒
steps:執行步數,預設5
offset:第二根手指偏移量,預設(0, 50)

如果你之前看過swipe_along()的原始碼解析,會發現這兩個方法的實現是如此之像,就是多了一根手指的處理。以上程式碼中contact=0代表第一根手指,contact=1代表第二根手指

第40-41行

from_x, from_y = tuple_from_xy
to_x, to_y = tuple_to_xy

分別定義開始/結束的座標x,y變數(第一根手指)

第43-46行

from_x2, from_y2 = (min(max(0, from_x + offset[0]), self.size_info['width']),
            min(max(0, from_y + offset[1]), self.size_info['height']))
to_x2, to_y2 = (min(max(0, to_x + offset[0]), self.size_info['width']),
        min(max(0, to_y + offset[1]), self.size_info['height']))

根據第一根手指開始/結束的座標x,y變數計算出第二根手指開始/結束的座標

第47行,向滑動事件列表swipe_events分別加入兩根手指的按下事件

第51-60行,向滑動事件列表swipe_events依次加入等待事件、兩根手指的分段移動事件

第61行,給滑動事件列表swipe_events追加兩個手指的抬起事件

第62行self.perform(swipe_events)將滑動事件列表中的每個事件依次執行

因為和swipe_along()原始碼極其相似,想看更詳細的原始碼解析的,可以去看下之前swipe_along()的文章Airtest API精講之連續滑動swipe_along()

從原始碼可知第二根手指的預設偏移量是(0, 50),所以假如你給的橫滑的開始/結束座標為(0,100),(100,100),那麼算出來的第二根手指的開始/結束座標就是(0,150),(100,150),即兩條相距50垂直距離的平行線。

相應的,假如你給的豎滑的開始/結束座標為(100,100),(100,200),用預設偏移量(0, 50),那麼算出來的第二根手指的開始/結束座標就是(100,150),(100,150),即兩條部分重疊的豎線。所以如果你想要兩條平行的豎線,偏移量就要是(50, 0)

演示例項

畫兩條橫向平行線

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

from airtest.core.api import *

# 獲取當前手機裝置
dev = device()
# 從[150, 1000]到[700, 1000],其餘引數全部用預設
dev.touch_proxy.two_finger_swipe([150, 1000],[700, 1000])

畫兩條豎向平行線

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

from airtest.core.api import *

# 獲取當前手機裝置
dev = device()
# 從[250, 550]到[250, 700],持續10秒,分5步,第2根手指偏移量(200,0)
dev.touch_proxy.two_finger_swipe([250, 550],[250, 700], duration=10, steps=5, offset=(200, 0))

遇到問題

我用的小米手機,MIUI12,每次執行完畢後,第二根手指會多一段距離的慣性滑動。諮詢過官方說是雖然顯示多一段,但實際那一段沒有執行;可是我自己試的應該是執行了的(雙指下滑的時候相簿歪了)。假如你要用到two_finger_swipe(),使用過程中也真的多執行了一段,並且影響到了功能實現,那麼可以通過調整duration、steps、offset的大小,也許可以解決問題。

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

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