2018年10月18日 星期四

10-失去你擁有世界又如何 (MonkeyImage.getSubImage)

通常檢查裝置是否在預期的情境時並不會去比較整個螢幕畫面,因為咱們只在意畫面上最關鍵的部分——可以代表預期影格的最有力的特徵,這主要是為避免誤判,另一方面也是為了加速畫面的比較。雖然 MonkeyImage.sameAs() 無法指定比較的範圍,但咱們可以從 MonkeyImage 物件提取出適當的畫面後再以 MonkeyImage.sameAs() 進行比較,這便是 MonkeyImage.getSubImage (tuple rect) 的作用。

參數:
rect 提取畫面的範圍,形式為 (x, y, width, height),單位為像素。x 由螢幕最左邊往右從 0 開始遞增;y 則由螢幕最上方往下從 0 開始遞增。

# 匯入所需模組
from com.android.monkeyrunner import MonkeyDevice, MonkeyImage, MonkeyRunner

# 連接 Android 裝置
device = MonkeyRunner.waitForConnection()

# 取得 5 秒前的整個畫面
img_full_previous = device.takeSnapshot()

# 取得 5 秒前的 (20, 30) 到 (20 + 10 - 1, 30 + 40 - 1) 即 (29, 69) 的子畫面
img_sub_previous = img_full_previous.getSubImage((20, 30, 10, 40))

MonkeyRunner.sleep(5)

# 取得目前整個畫面
img_full_current = device.takeSnapshot()

# 取得目前畫面的 (20, 30) 到 (20 + 10 - 1, 30 + 40 - 1) 即 (29, 69) 的範圍
img_sub_current = img_full_current.getSubImage((20, 30, 10, 40))

# 比對現在畫面和 5 秒前的畫面
is_same = img_sub_current.sameAs(img_sub_previous, 1.0)

# 如果畫面已經改變
if not is_same:
    # 執行相關測試動作

沒有留言:

張貼留言