日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Mac下的UI自动化测试 (二)

發布時間:2023/12/31 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 Mac下的UI自动化测试 (二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面講一下Sikuli的重要概念,就是region,所謂region就是Sikuli在進行圖像識別的時候的一個區域,默認是整個屏幕。

當然,如果region選得太大的話,并且UI上存在相似的控件,那么就會造成圖像識別的錯誤。而且region選得過大也會使得代碼運行速度下降。

我在實際應用中,region選的是屏幕中間的工作區域,也就是除了最上方的global menu 和 system tray區域,和下方的dock區域,并且在被測程序啟動后,將其最大化,以占滿中間的工作區,防止其他應用的UI干擾測試運行。

下面的代碼就是根據AppleScript獲取當前屏幕大小,與dock大小,然后用這些值構造Region對象:

from sikuli import *
import helper

# Get width and heigth of screen by applescripts
width_of_screen = helper.get_bounds_of_screen()[0]
height_of_screen = helper.get_bounds_of_screen()[1]

# Height of top menu bar
height_of_top_menu_bar = 24

# Get height of dock by applescripts
height_of_dock = helper.get_height_of_dock()

# Region of screen, without top menu bar and dock
region = Region(0, height_of_top_menu_bar, width_of_screen,
                height_of_screen - height_of_top_menu_bar - height_of_dock)
print "region size is ({0}, {1}, {2}, {3})".format(region.x, region.y, region.w, region.h)

其中get_bounds_of_screen獲取屏幕大小,使用的是下面的AppleScript:

tell app "Finder" to get bounds of window of desktop

獲取dock的高度get_height_of_height_of_dock使用的是這個AppleScript:

tell application "System Events" to tell process "Dock"
    set dock_dimensions to size in list 1
    set dock_height to item 2 of dock_dimensions
end tell

然后計算出工作區大小,構造region對象:

region = Region(0, height_of_top_menu_bar, width_of_screen,
                height_of_screen - height_of_top_menu_bar - height_of_dock)

當然測試system tray的功能也是必不可少的,那么就需要再準備一個對應最上方global menu區域的region:

# Height of top menu bar
height_of_top_menu_bar = 24

# Region for top menu bar region
top_menu_bar_region = Region(
    0, 0, helper.get_bounds_of_screen()[0], height_of_top_menu_bar)

region構造完成之后,就可以使用它來進行click,doubleclick,drag&drop的操作了,傳遞的參數就是一個截圖:

在我的實際應用中,是將所有的截圖都放到一個統一的screenshots的sikuli文件中,其他模塊再去導入它:

這樣的好處是它就像一個描述UI的ID,Name或者是父子關系的一個xml文件一樣,當有UI變化的時候,不必牽連到其他模塊的修改,只需將screenshots模塊中對應的image更改即可。

另外,sikuli的圖像識別是有一個相似度的,如果不滿意,可以在sikuli IDE上點擊圖片進行修改:

下面的紅色高亮表示的就是識別到得區域:

由于sikuli的IDE的功能簡單,穩定性差,我在實際工作中是不會使用它來coding的,當你需要一個sikuli文件時,打開sikuli的IDE并new一個空文件并保存,這個文件就可以通過右鍵的“show package contents”打開它,看到它就是一個包含py文件和html文件的一個文件夾:

其中html文件的內容才是sikuli IDE中顯示的內容,但是實際運行時與這個html沒關系,使用的是py文件編譯后的java字節碼文件。

所以我們可以用任何編輯器編輯py文件,而不管html文件是什么內容,除非你想用這個IDE來方便的截圖與修改圖像匹配度。

下面的截圖是我用sublime text打開的一個項目,可以看到sikuli文件就是一個文件夾:

下面是一些我常用的AppleScript腳本:

1. 最大化(非全屏)一個app,需要用到dock的高度:

tell application "Finder"
    set screenResolution to bounds of window of desktop
end tell

set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution

tell application "System Events" to tell process "Dock"
    set dock_dimensions to size in list 1
    set dock_height to item 2 of dock_dimensions
end tell

tell application "System Events" to tell process "RealTimes"
    activate
    set position of window 1 to {0, 24}
    set size of window 1 to {screenWidth, screenHeight - dock_height - 24}
end tell

2. 通過AppleScript將Finder定位到一個路徑上去,并且將Finder窗口放到屏幕工作區域的右半邊:

#! /usr/bin/osascript

on run(arguments)
    set myDocumentFolder to POSIX path of (first item of arguments)

    tell application "Finder"
        close every window
        activate
        make new Finder window
        set toolbar visible of the front Finder window to false
        set statusbar visible of the front Finder window to false
        set the current view of front Finder window to icon view
        set the target of the front Finder window to (POSIX file myDocumentFolder)
        set screenResolution to bounds of window of desktop
    end tell

    set screenWidth to item 3 of screenResolution
    set screenHeight to item 4 of screenResolution

    tell application "System Events" to tell process "Dock"
        set dock_dimensions to size in list 1
        set dock_height to item 2 of dock_dimensions
    end tell

    tell application "Finder"
        activate
        set frontmost to true
        set bounds of the front Finder window to {screenWidth / 2, 24, screenWidth, screenHeight - dock_height}
    end tell 
end run

當要從一個Finder中選擇一個文件拖放到某個App中的時候,這個腳本就很有用了。

總結

以上是生活随笔為你收集整理的Mac下的UI自动化测试 (二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。