axscripttest

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package axscripttest provides an rsc.io/script bridge for macOS UI automation testing using the axuiautomation package. It enables declarative, txtar-based test scripts for accessibility-driven black-box testing of macOS applications.

Commands

  • app-activate: bring the application to the foreground
  • window-wait <title> [timeout]: wait for a window with the given title
  • menu-click <item>...: click a menu item by path
  • click role=R [title=T] [id=I]: find and click an element
  • type <text>: type text into the focused element
  • select <popup-title> <value>: select a value from a popup button
  • exists role=R [title=T] [id=I]: assert an element exists
  • value-wait <selectors> <contains> [timeout]: wait for an element's value
  • screenshot <file>: capture the main window to a PNG file

Conditions

  • [trusted]: true if the process has accessibility permissions
  • [app-running]: true if the target application is running

Accessibility Permissions

Scripts that begin with [!trusted] skip will silently pass when the test binary lacks accessibility permission. To trigger the system permission dialog, call EnsureTrusted from TestMain. This uses macgo to create an app bundle with a TCC identity so macOS can register and prompt for accessibility access:

var axprompt = flag.Bool("axprompt", false, "prompt for accessibility")

func TestMain(m *testing.M) {
    runtime.LockOSThread()
    flag.Parse()
    if *axprompt {
        if err := axscripttest.EnsureTrusted(); err != nil {
            log.Fatal(err)
        }
    }
    os.Exit(m.Run())
}

Then run with: go test -run TestFinder -axprompt

Thread Safety

The macOS Accessibility API must be called from the main OS thread. Use runtime.LockOSThread in TestMain before running any scripts.

Example

func TestFinder(t *testing.T) {
    axscripttest.RunFile(t, axscripttest.Config{App: "com.apple.finder"}, "testdata/finder.txt")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Engine

Engine returns a script engine and application configured for UI automation. The caller is responsible for closing the application when done.

func EnsureTrusted

func EnsureTrusted() error

EnsureTrusted sets up a TCC identity via macgo and triggers the system accessibility permission dialog if the process is not already trusted. Call this from TestMain before m.Run, since macgo may re-exec the process inside an app bundle:

func TestMain(m *testing.M) {
    runtime.LockOSThread()
    axscripttest.EnsureTrusted()
    os.Exit(m.Run())
}

func RunFile

func RunFile(t *testing.T, cfg Config, file string)

RunFile runs a txtar script file against the configured application. Scripts should use the [trusted] condition to skip gracefully when accessibility permissions are not available. Use EnsureTrusted in TestMain to trigger the system permission dialog.

Types

type Config

type Config struct {
	App     string        // bundle id, pid, or name
	Timeout time.Duration // default wait timeout (default 10s)
}

Config configures the script engine for a target application.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL