使用ATOMac进行Mac自动化测试

ATOMac简介

atomac是一个支持在mac上做自动化的python库,GitHub地址如下:

https://github.com/pyatom/pyatom

 

 

安装

# 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. 获取Window

automator = atomac.getAppRefByBundleId('com.apple.Automator')
window = automator.windows()[0]
print(window)

输出

<atomac.AXClasses.NativeUIElement AXWindow '未命名'>

 

3. 获取应用标题

print(window.AXTitle)

输出

未命名

 

4. 查看元素

atomac支持获取和操作大部分的元素,可以使用xcode提供的accessibility inspector快速查看各个元素

路径: Xcode -> Open Developer Tools -> Accessibility inspector

 

 

 

Atomac支持的元素类型有:

textAreas
textFields
buttons
windows
sheets
staticTexts
genericElements
groups
radioButtons
popUpButtons
rows
sliders

atomac所有的定位方法加上'R'字符,就变成了一个搜索方法(可以添加额外的搜索条件)

 

5. 获取元素

通过快照我们可以进行元素定位, 这里我们以关闭按钮为例

closeButton = window.buttonsR('关闭')[0]
print(closeButton)

输出:

<atomac.AXClasses.NativeUIElement AXButton '关闭'>

 

6. 条件搜索元素

atomac支持findFirstR方法,根据属性来进行元素搜索,例如

closeButton = window.findFirstR(AXRole='AXButton', AXTitle='关闭')

支持的属性可以在Accessibility inspector中查看

 

findFirstR方法返回首个匹配的元素, 如果没有找到匹配的元素则返回空列表

findAllR使用方法相同,返回所以匹配的元素列表,没有匹配的元素则返回空列表

 

7. 查看元素支持的属性

closeButton = window.findFirstR(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()

任何支持的操作都可以这样调用

 

posted @ 2020-07-27 10:03  周希  阅读(2532)  评论(1编辑  收藏  举报