ATOMac - 基於Python的Mac應用Ui自動化庫


ATOMacTest

一、緣 起

近期工作需要對一款Mac端應用實現常用功能的自動化操作,同事推薦ATOMac這款工具,這幾天簡單研究了下,同時也發現現網介紹ATOMac的資料非常有限,故在此記錄下ATOMac的一些簡單用法,僅供學習參考~

二、概 述

如標題,ATOMac是一個基於Python語言,通過Apple Accessibility API實現的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 安裝

  1. 由於該庫pip包已經很久沒更新了,直接pip安裝可能會報錯,Python2建議使用easy_install安裝
  2. 目前atomac 1.1.0不支持Python3,但是@daveenguyen這位大神已經在源碼庫做了Python3的兼容,所以需要直接從git倉庫安裝,詳細如下:
# Python2
sudo easy_install atomac

# Python3
pip3 install git+https://github.com/pyatom/pyatom/

3.2 常用功能

基礎的用法在官網有說明,這里就不再贅述,以下將以地圖為例,實現一些常用功能

3.2.1 需要用到

  1. 應用的bundle_id:打開應用內容 -> info.plist
  2. Accessibility Inspector:Xcode -> Open Developer Tools

Accessibility Inspector

3.2.1 代碼實現

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文件中可以找到很多已經定義好的方法
# 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='衛星')

# 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得到的坐標是元素左上角的坐標,需要根據實際大小得到元素中心點坐標
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, 輸入內容(輸入鍵盤字符,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 元素屬性對應說明

  1. ATOMac庫使用的元素屬性均在其屬性名(通過Accessibility Inspector查到)前面加AX,且首字母大寫,如下所示
ATOMac Accessibility Inspector
AXSize Size
AXRole Role
AXPosition Position
AXRoleDescription Type
AXValue Value
... ...
  1. 比較特殊的是: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

OK!

~
~
~

不積跬步,無以至千里


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM