ATOMacTest
一、緣 起
近期工作需要對一款Mac端應用實現(xiàn)常用功能的自動化操作爆哑,同事推薦ATOMac這款工具面哼,這幾天簡單研究了下,同時也發(fā)現(xiàn)現(xiàn)網(wǎng)介紹ATOMac
的資料非常有限乔妈,故在此記錄下ATOMac
的一些簡單用法,僅供學習參考~
二氓皱、概 述
如標題路召,ATOMac
是一個基于Python語言,通過Apple Accessibility API
實現(xiàn)的Mac端應用Ui自動化控制庫波材,下面是官方的說明:
We are pleased to introduce the first Python library to fully enable GUI testing of Mac applications via the Apple Accessibility API. This library was created out of desperation. Existing tools such as using appscript to send messages to accessibility objects are painful to write and slow to use. ATOMac has direct access to the API. It's fast and easy to use to write tests.
三股淡、使 用
3.1 安裝
- 由于該庫
pip
包已經(jīng)很久沒更新了,直接pip
安裝可能會報錯廷区,Python2建議使用easy_install
安裝 - 目前
atomac 1.1.0
不支持Python3唯灵,但是@daveenguyen這位大神已經(jīng)在源碼庫做了Python3
的兼容,所以需要直接從git
倉庫安裝隙轻,詳細如下:
# Python2
sudo easy_install atomac
# Python3
pip3 install git+https://github.com/pyatom/pyatom/
3.2 常用功能
基礎的用法在官網(wǎng)有說明埠帕,這里就不再贅述,以下將以地圖為例玖绿,實現(xiàn)一些常用功能
3.2.1 需要用到
- 應用的bundle_id:打開應用內(nèi)容 -> info.plist
- Accessibility Inspector:Xcode -> Open Developer Tools
3.2.1 代碼實現(xiàn)
import atomac
from time import sleep
from atomac.AXKeyCodeConstants import *
bundle_id = 'com.apple.Maps'
# bs = atomac.AXClasses.AXKeyCodeConstants.BACKSPACE
# part 1, 啟動應用并獲取應用信息
atomac.launchAppByBundleId(bundle_id)
sleep(2)
ato = atomac.getAppRefByBundleId(bundle_id)
print(ato)
# part 2, 獲取當前應用windows
cur_win = ato.windows()[0]
print(cur_win)
# part 3, 查找元素
# findFirst搞监,返回第一個匹配的元素
# findFirstR,遞歸查找镰矿,返回第一個匹配的元素(當查找的元素Parent非標準窗口時使用)
# 在AXClasses.py文件中可以找到很多已經(jīng)定義好的方法
# dt = cur_win.radioButtonsR('地圖')[0] # 也可以
dt = cur_win.findFirstR(AXRole='AXRadioButton', AXTitle='地圖')
gj = cur_win.findFirstR(AXRole='AXRadioButton', AXTitle='公交')
wx = cur_win.findFirstR(AXRole='AXRadioButton', AXTitle='衛(wèi)星')
# part 4, 元素屬性所有
attr = dt.getAttributes()
# 元素某一個屬性
dt_title = dt.AXTitle
print(attr, dt_title)
# part 5, 點擊/切到公交
# Method 1琐驴,唯一定位元素后,使用Press方法
print(gj)
gj.Press()
# Method 2秤标,定位元素坐標并鼠標點擊
# 注意AXPositon得到的坐標是元素左上角的坐標绝淡,需要根據(jù)實際大小得到元素中心點坐標
dt_position = dt.AXPosition
dt_size = dt.AXSize
coord = (dt_position[0] + dt_size[0] / 2, dt_position[1] + dt_size[1])
print(coord)
dt.clickMouseButtonLeft(dt_position)
# part 6, 輸入內(nèi)容(輸入鍵盤字符,US_keyboard)
s1 = cur_win.findFirstR(AXRole='AXTextField', AXRoleDescription='搜索文本欄')
# s1 == s2
# s2 = cur_win.textFieldsR('搜索文本欄')[0]
s1_p = s1.AXPosition
s1_s = s1.AXSize
s1.tripleClickMouse((s1_p[0] + s1_s[0] / 2, s1_p[1] + s1_s[1] / 2))
s1.sendKeys('7983')
# part 7, 輸入鍵盤上的修飾符
sleep(1)
s1.sendKeys([BACKSPACE])
# 回車
s1.sendKeys([RETURN])
3.2.4 元素屬性對應說明
-
ATOMac
庫使用的元素屬性均在其屬性名(通過Accessibility Inspector
查到)前面加AX
苍姜,且首字母大寫牢酵,如下所示
ATOMac | Accessibility Inspector |
---|---|
AXSize | Size |
AXRole | Role |
AXPosition | Position |
AXRoleDescription | Type |
AXValue | Value |
... | ... |
- 比較特殊的是:AXRoleDescription - Type
參考
https://github.com/pyatom/pyatom
https://zhuanlan.zhihu.com/p/30385931
http://python-atomac.blogspot.com/p/atomac-usage.html
https://blog.csdn.net/sinat_40766770/article/details/91048760
常見錯誤及解決辦法
window獲取失敗,報:IndexError: list index out of range
報錯:
? ATOMacTest git:(master) python3 ATOMacTest.py
<atomac.AXClasses.NativeUIElement <No role!> ''>
Traceback (most recent call last):
File "ATOMacTest.py", line 21, in <module>
cur_win = ato.windows()[0]
IndexError: list index out of range
原因:
ATOMac控制Mac需要提前在隱私里面添加信任衙猪,如下圖所示
AttributeError: 'NoneType' object has no attribute 'getAttributes'
報錯:
? ATOMacTest git:(master) python ATOMacTest.py
<atomac.AXClasses.NativeUIElement AXApplication u'\u5730\u56fe'>
<atomac.AXClasses.NativeUIElement AXWindow u'\u5730\u56fe'>
Traceback (most recent call last):
File "ATOMacTest.py", line 34, in <module>
attr = dt.getAttributes()
AttributeError: 'NoneType' object has no attribute 'getAttributes'
原因:
ATOMacTest.py
這個demo腳本當時是基于python3的ATOMac庫寫的馍乙,用python2運行會報錯
ATOMac的版本如下
目前筆者用的ATOMac的版本如下布近,僅供參考
# python2
? ATOMacTest git:(master) pip show atomac
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Name: atomac
Version: 1.1.0
......
# python3
? ATOMacTest git:(master) pip3 show atomac
Name: atomac
Version: 1.2.0
......