Skip to main content

Automated Testing on macOS

Project description

mac-ui-automation: Automated Testing on macOS

license pypi style

This library is a fork of [atomacos]. It was created to provide a release with type hints support because there has not been a release since 2024

macuiauto is a library to enable GUI testing of macOS applications via the Apple Accessibility API. macuiauto has direct access to the API via pyobjc. It's fast and easy to use to write tests.

Why macuiauto is needed

In the beginning, I was a fan of atomac, then I found out that it didn't support python3, so I discovered atomacos, thanks to atomacos for adding support for python3. But when I used Atomacos, I found some inconveniences.

  1. The method names of atomacos use camel case naming. If I remember correctly, python should recommend using lowercase and underscores to name methods and variables.
  2. atomacos does not support type hints, so I often need to check whether my function name is written correctly. This is critical to development efficiency and development experience.

Based on the above two points, I modified some implementations of atomacos to solve the above two problems

Getting started

Requirements

  • macOS
  • pyobjc
  • Systemwide Accesibility enabled

If you experience issues, please open a ticket in the issue tracker.

Enabling Accessibility

Check the checkbox:

System Settings > Privacy & Security > Accessibility > [The app that execute the python script]

Failure to enable this will result in AXErrorAPIDisabled exceptions during some module usage.

Installing

For release

$ pip install macuiauto

For pre-release

$ pip install --pre macuiauto

Usage Examples

Once installed, you should be able to use it to launch an application:

>>> import macuiauto
>>> macuiauto.launch_app_by_bundle_id('com.apple.Automator')

This should launch Automator.

Next, get a reference to the UI Element for the application itself:

>>> automator = macuiauto.get_app_ref_by_bundle_id('com.apple.Automator')
>>> automator
<macuiauto.AXClasses.NativeUIElement AXApplication Automator>

Now, we can find objects in the accessibility hierarchy:

>>> window = macuiauto.windows()[0]
>>> window.title
u'Untitled'
>>> sheet = window.sheets()[0]

Note that we retrieved an accessibility attribute from the Window object - AXTitle. macuiauto supports reading and writing of most attributes. Xcode's included Accessibility Inspector can provide a quick way to find these attributes.

There is a shortcut for getting the sheet object which bypasses accessing it through the Window object. macuiauto can search all objects in the hierarchy:

>>> sheet = automator.sheets_r()[0]

There are search methods for most types of accessibility objects. Each search method, such as windows, has a corresponding recursive search function, such as windows_r.

The recursive search finds items that aren't just direct children, but children of children. These search methods can be given terms to identify specific elements. Note that * and ? can be used as wildcard match characters in all search methods:

>>> close = sheet.buttons('Close')[0]

There are methods to search for UI Elements that match any number of criteria. The criteria are accessibility attributes:

>>> close = sheet.find_first(AXRole='AXButton', AXTitle='Close')

find_first and find_first_r return the first item found to match the criteria or None. find_all and find_all_r return a list of all items that match the criteria or an empty list([]).

Objects are fairly versatile. You can get a list of supported attributes and actions on an object:

>>> close.get_attributes()
[u'AXRole', u'AXRoleDescription', u'AXHelp', u'AXEnabled', u'AXFocused',
u'AXParent', u'AXWindow', u'AXTopLevelUIElement', u'AXPosition', u'AXSize',
u'AXTitle']
>>> close.title
u'Close'
>>> close.AXTitle
u'Close'
>>> close.get_actions()
[u'Press']

Performing an action is as natural as:

>>> close.press()
>>> close.Press()

Any action can be triggered this way.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

macuiauto-3.2.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

macuiauto-3.2.1-py2.py3-none-any.whl (19.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file macuiauto-3.2.1.tar.gz.

File metadata

  • Download URL: macuiauto-3.2.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for macuiauto-3.2.1.tar.gz
Algorithm Hash digest
SHA256 461e0191754154792a67fe1b4c2ab0f2df5bc460a1fb09ae824a177bc14b8350
MD5 d73d92668e515ad6ba5ca5d66bab9ae7
BLAKE2b-256 b1f39549bfb3c6d159502df37cd265a4db33f1620a3a1c19306099bcc8934f8a

See more details on using hashes here.

File details

Details for the file macuiauto-3.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: macuiauto-3.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for macuiauto-3.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ec800c988f173cb86cb0f3b7810e693dfc434096751bf4f462a1d658ce189956
MD5 b488882359229e9e3417841559dff51b
BLAKE2b-256 32da2f614dbf6445c8181e8a137963fec4cd6d0995af841c070c6336b2a01bf9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page