使用ATOMac進行Mac自動化測試
ATOMac簡介
atomac是一個支援在mac上做自動化的python庫,GitHub地址如下:
安裝
# Python2 sudo easy_install atomac # Python3 pip3 install git+https://github.com/pyatom/pyatom/
使用
1. 啟動程式
import atomac atomac.launchAppByBundleId('com.apple.Automator')
查看bundleID的方法
在應用程式->右鍵選擇包內容->Contents->Info.plist
2. 查看app資訊
automator = atomac.getAppRefByBundleId('com.apple.Automator') print(automator)
輸出
<atomac.AXClasses.NativeUIElement AXApplication '自動操作'>
3. 獲取應用標題
window = automator.windows()[0] print(window.AXTitle)
輸出
未命名
atomac支援獲取和操作大部分的元素,可以使用xcode提供的accessibility inspector快速查看各個元素
路徑: Xcode -> Open Developer Tools -> Accessibility inspector
4. 獲取元素快照列表
window = automator.windows()[0] sheet = window.sheets()[0] print(sheet)
輸出:
<atomac.AXClasses.NativeUIElement AXSheet '表單'>
windows是atomac的一種定位方法,用來獲取window元素,這裡我們獲取到了最頂層窗口的元素,然後再用sheets定位方法來獲取當前window的元素快照(sheet)
atomac所有的定位方法加上’R’字元,就變成了一個搜索方法(可以添加額外的搜索條件),例如上面的方法我們可以直接改為:
sheet = automator.sheetsR()[0]
5. 通過快照獲取元素
通過快照我們可以進行元素定位, 這裡我們以關閉按鈕為例
closeButton = sheet.buttons('關閉')[0] print(closeButton)
輸出:
<atomac.AXClasses.NativeUIElement AXButton '關閉'>
支援的元素類型查詢方法有:
textAreas
textFields
buttons
windows
sheets
staticTexts
genericElements
groups
radioButtons
popUpButtons
rows
sliders
6. 條件搜索元素
atomac支援findFirst方法,根據屬性來進行元素搜索,例如
closeButton = sheet.findFirst(AXRole='AXButton', AXTitle='關閉')
支援的屬性可以在Accessibility inspector中查看
findFirst和findFirstR方法返回首個匹配的元素, 如果沒有找到匹配的元素則返回None
同時還有findAll和findAllR使用方法相同,返回所以匹配的元素列表,沒有匹配的元素則返回空列表
7. 查看元素支援的屬性
closeButton = sheet.findFirst(AXRole='AXButton', AXTitle='關閉') print(closeButton.getAttributes())
輸出
['AXRole', 'AXHelp', 'AXEnabled', 'AXWindow', 'AXSize', 'AXTitle', 'AXRoleDescription', 'AXTopLevelUIElement', 'AXFocused', 'AXParent', 'AXPosition', 'AXFrame', 'AXIdentifier']
查看屬性值
print(closeButton.AXTitle)
輸出
關閉
8. 查看元素支援的操作
print(closeButton.getActions())
輸出
['Press']
9. 元素操作
closeButton.Press()
任何支援的操作都可以這樣調用