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 安装
- 由于该库
pip
包已经很久没更新了,直接pip
安装可能会报错,Python2建议使用easy_install
安装 - 目前
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 需要用到
- 应用的bundle_id:打开应用内容 -> info.plist
- Accessibility Inspector:Xcode -> Open Developer Tools
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 元素属性对应说明
-
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
......