Skip to content

automation ​

go
import "github.com/tangoui/tango/automation"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package automation drives a running Tango application from Go tests, like Playwright drives a browser: it starts the program (a development build: -tags tango_dev, which Launch adds when it builds a package) with the automation control channel turned on, finds windows, locates nodes of self-drawn windows through their semantics tree, clicks, types, waits, evaluates JavaScript in WebView windows, takes screenshots and quits the app at the end of the test.

func TestAdd(t *testing.T) {
	app := automation.Launch(t, ".", "-data", filepath.Join(t.TempDir(), "ledger.json"))
	win := app.Window("记账本")
	win.Find(a11y.RoleButton, "记一笔").Click()
	win.Find(a11y.RoleSpinButton, "金额").SetValue("88.8")
	win.Find(a11y.RoleButton, "保存").Click()
	win.FindContains(a11y.RoleRow, "打车回家").Wait()
	win.SaveScreenshot(filepath.Join(t.TempDir(), "ledger.png"))
}

Launch skips the test unless TANGO_E2E is set (and with -short), so `go test ./...` never opens windows. See docs/automation.md for the security model (development builds only, loopback, token), hot reload (unsupported) and CI.

Every method fails the test (t.Fatal) when the app reports an error or a wait times out; call them from the test goroutine.

常量 ​

go
const (
	KindCanvas  = wire.KindCanvas  // self-drawn (package ui)
	KindWebView = wire.KindWebView // WebView page
)

Window kinds.

go
const (
	MatchExact    = wire.MatchExact
	MatchContains = wire.MatchContains
	MatchRegexp   = wire.MatchRegexp
)

Name matching of Query.Match.

go
const DefaultTimeout = 10 * time.Second

DefaultTimeout is how long waits (Locator, Window, Eval) last unless Options.Timeout or SetTimeout says otherwise.

go
const EnvE2E = "TANGO_E2E"

EnvE2E must be set for Launch to run (it skips the test otherwise).

go
const EnvLaunchBackground = client.EnvLaunchBackground

EnvLaunchBackground is set to 1 for the launched program: on macOS it starts without activating and shows its windows behind the others. Pass EnvLaunchBackground+"=0" in Options.Env to launch it in front.

类型 ​

App ​

go
type App struct {
	// contains filtered or unexported fields
}

App is a running application under test.

func Connect ​

go
func Connect(t testing.TB, addr, token string) *App

Connect attaches to an app that is already running with automation on (addr and token as the app wrote them to TANGO_AUTOMATION_TOKEN_FILE or printed them). The app is not quit at the end of the test; the connection is closed.

func Launch ​

go
func Launch(t testing.TB, target string, args ...string) *App

Launch builds and starts target with args and connects to it. target is a Go package (".", "./examples/ledger", an import path), built with `go build -tags tango_dev` into a temporary directory, or the path of an executable, which must have been built with -tags tango_dev. The test is skipped unless TANGO_E2E is set, and with -short. The program is asked to quit (then killed) in t.Cleanup.

func LaunchOptions ​

go
func LaunchOptions(t testing.TB, target string, opts Options) *App

LaunchOptions is Launch with options.

func (*App) Exited ​

go
func (a *App) Exited() bool

Exited reports whether the program has exited (always false for Connect).

func (*App) Output ​

go
func (a *App) Output() string

Output returns what the program printed so far (the last 256 KiB); empty for Connect.

func (*App) Quit ​

go
func (a *App) Quit()

Quit asks the app to quit and waits until the program exits (killing it after 10s).

func (*App) SetTimeout ​

go
func (a *App) SetTimeout(d time.Duration)

SetTimeout changes the default wait of later calls (Locators created before keep theirs unless they had none).

func (*App) Timeout ​

go
func (a *App) Timeout() time.Duration

Timeout returns the default wait.

func (*App) Window ​

go
func (a *App) Window(title string) *Window

Window waits for a window titled title and returns it.

func (*App) WindowMatching ​

go
func (a *App) WindowMatching(what string, match func(WindowInfo) bool) *Window

WindowMatching waits for a window for which match reports true; what describes it in the failure message.

func (*App) Windows ​

go
func (a *App) Windows() []WindowInfo

Windows lists the open windows.

Error ​

go
type Error = wire.Error

Error is an error reported by the app (Code is one of the Code* constants of the protocol: "timeout", "hot_reload", ...).

Locator ​

go
type Locator struct {
	// contains filtered or unexported fields
}

Locator finds a node of a self-drawn window's semantics tree. It is a description, not a node: every method looks the node up again in the current tree, waiting up to the timeout for it to appear (Exists and Count do not wait), so it survives re-renders and works before the node exists. Methods returning *Locator return a refined copy.

func (*Locator) All ​

go
func (l *Locator) All() []a11y.Node

All returns every node matching now (Nth ignored).

func (*Locator) Bounds ​

go
func (l *Locator) Bounds() a11y.Rect

Bounds waits for the node and returns its rectangle (logical pixels from the top-left of the window's content).

func (*Locator) Click ​

go
func (l *Locator) Click()

Click activates the node: the a11y Press action when it supports it, otherwise a pointer click at the center of its bounds.

func (*Locator) ClickPointer ​

go
func (l *Locator) ClickPointer()

ClickPointer clicks the center of the node's bounds with the pointer (real hit testing, hover and press states) instead of the a11y action.

func (*Locator) Collapse ​

go
func (l *Locator) Collapse()

Collapse closes it.

func (*Locator) Count ​

go
func (l *Locator) Count() int

Count returns how many nodes match now (Nth ignored).

func (*Locator) Decrement ​

go
func (l *Locator) Decrement()

Decrement steps it down.

func (*Locator) Dismiss ​

go
func (l *Locator) Dismiss()

Dismiss closes a dialog, popup or message.

func (*Locator) Exists ​

go
func (l *Locator) Exists() bool

Exists reports whether the node exists now (no waiting beyond the first tree of the window). Only a timeout means "no": any other error, such as code "busy" (the window's UI thread did not answer), fails the test.

func (*Locator) Expand ​

go
func (l *Locator) Expand()

Expand opens the node (combobox, tree item, collapse panel).

func (*Locator) First ​

go
func (l *Locator) First() *Locator

First is Nth(0); Last is Nth(-1).

func (*Locator) Focus ​

go
func (l *Locator) Focus()

Focus moves the keyboard focus to the node.

func (*Locator) Has ​

go
func (l *Locator) Has(s a11y.State) bool

Has waits for the node and reports whether it has every state of s.

func (*Locator) Increment ​

go
func (l *Locator) Increment()

Increment steps a slider or spin button up.

func (*Locator) Last ​

go
func (l *Locator) Last() *Locator

Last picks the last match.

func (*Locator) Name ​

go
func (l *Locator) Name() string

Name waits for the node and returns its accessible name.

func (*Locator) Node ​

go
func (l *Locator) Node() a11y.Node

Node waits for the node and returns a copy of it.

func (*Locator) Nth ​

go
func (l *Locator) Nth(i int) *Locator

Nth picks the i-th match in reading order (0 the first; negative counts from the end, -1 the last).

func (*Locator) Perform ​

go
func (l *Locator) Perform(act a11y.Action, arg string) a11y.Node

Perform waits for the node and asks it to perform act (with arg for a11y.SetValue), as an assistive technology would; it returns once the frame that handled it has run.

func (*Locator) Query ​

go
func (l *Locator) Query() Query

Query returns the locator's query.

func (*Locator) ScrollIntoView ​

go
func (l *Locator) ScrollIntoView()

ScrollIntoView scrolls the node's ancestors so it is visible.

func (*Locator) SetValue ​

go
func (l *Locator) SetValue(v string)

SetValue replaces the node's value (text fields, number inputs, sliders) with the a11y SetValue action.

func (*Locator) String ​

go
func (l *Locator) String() string

func (*Locator) Timeout ​

go
func (l *Locator) Timeout(d time.Duration) *Locator

Timeout sets how long this locator waits.

func (*Locator) Type ​

go
func (l *Locator) Type(text string)

Type focuses the node and sends text as typed (committed) input.

func (*Locator) Value ​

go
func (l *Locator) Value() string

Value waits for the node and returns its value.

func (*Locator) Wait ​

go
func (l *Locator) Wait() a11y.Node

Wait is Node: it waits until the node exists.

func (*Locator) WaitGone ​

go
func (l *Locator) WaitGone()

WaitGone waits until no node matches.

func (*Locator) WaitState ​

go
func (l *Locator) WaitState(s a11y.State) a11y.Node

WaitState waits until the node exists and has every state of s.

func (*Locator) WaitValue ​

go
func (l *Locator) WaitValue(v string) a11y.Node

WaitValue waits until the node exists and its Value is v.

func (*Locator) WithState ​

go
func (l *Locator) WithState(s a11y.State) *Locator

WithState keeps nodes that have every state of s (a11y.Checked|a11y.Focused).

func (*Locator) WithValue ​

go
func (l *Locator) WithValue(v string) *Locator

WithValue keeps nodes whose Value is v.

func (*Locator) WithoutState ​

go
func (l *Locator) WithoutState(s a11y.State) *Locator

WithoutState keeps nodes that have none of the states of s.

Options ​

go
type Options struct {
	// Args are the program's arguments.
	Args []string
	// Env adds KEY=VALUE entries to the inherited environment.
	Env []string
	// Dir is the working directory of the build and the program ("" the
	// test's).
	Dir string
	// BuildFlags are extra `go build` flags (e.g. "-tags", "mytag") when
	// the target is a package. Launch always adds the tango_dev tag:
	// production builds (the default) have no control channel.
	BuildFlags []string
	// Timeout is the default for waits; 0 means DefaultTimeout.
	Timeout time.Duration
	// StartTimeout bounds the wait for the program to open its control
	// channel after it started (the build is not counted); 0 means 30s.
	StartTimeout time.Duration
	// Addr is the TANGO_AUTOMATION value; "" means "127.0.0.1:0" (a free
	// loopback port).
	Addr string
	// Output, when set, also receives the program's stdout and stderr
	// (they are always kept and logged when the test fails). Setting
	// TANGO_E2E_VERBOSE has the same effect with os.Stderr.
	Output io.Writer
}

Options configure Launch.

Query ​

go
type Query = wire.Query

Query selects nodes of a self-drawn window's semantics tree; see Window.Locate.

Window ​

go
type Window struct {
	// contains filtered or unexported fields
}

Window is one window of the app under test.

func (*Window) Click ​

go
func (w *Window) Click(x, y float32)

Click clicks the primary button at (x, y).

func (*Window) ClickSelector ​

go
func (w *Window) ClickSelector(sel string)

ClickSelector waits for the element matching sel, scrolls it into view and clicks it (HTMLElement.click).

func (*Window) DoubleClick ​

go
func (w *Window) DoubleClick(x, y float32)

DoubleClick double-clicks at (x, y).

func (*Window) Drag ​

go
func (w *Window) Drag(x1, y1, x2, y2 float32)

Drag presses at (x1, y1), moves to (x2, y2) in a few steps and releases.

func (*Window) Dump ​

go
func (w *Window) Dump() string

Dump returns the semantics tree as an indented outline (a11y.Tree.Dump), handy in failure messages.

func (*Window) Eval ​

go
func (w *Window) Eval(expr string) json.RawMessage

Eval evaluates the JavaScript expression expr in the page and returns its value as JSON (a Promise is awaited). For statements wrap them: `(() => { ...; return x })()`.

func (*Window) EvalInto ​

go
func (w *Window) EvalInto(expr string, v any)

EvalInto evaluates expr and decodes its value into v.

func (*Window) EvalString ​

go
func (w *Window) EvalString(expr string) string

EvalString evaluates expr and returns its value as a string (JSON strings unquoted, other values as JSON text).

func (*Window) Fill ​

go
func (w *Window) Fill(sel, text string)

Fill waits for the input, textarea or contenteditable element matching sel, replaces its value with text and fires input and change events (so frameworks such as React and Vue see the change).

func (*Window) Find ​

go
func (w *Window) Find(role a11y.Role, name string) *Locator

Find locates nodes with role whose name is exactly name ("" any name; role a11y.RoleNone any role). The locator is lazy: every use looks the node up again, waiting for it.

func (*Window) FindContains ​

go
func (w *Window) FindContains(role a11y.Role, sub string) *Locator

FindContains locates nodes with role whose name contains sub (case-insensitive).

func (*Window) FindRegexp ​

go
func (w *Window) FindRegexp(role a11y.Role, pattern string) *Locator

FindRegexp locates nodes with role whose name matches the Go regular expression pattern.

func (*Window) ID ​

go
func (w *Window) ID() uint64

ID returns the window's surface ID.

func (*Window) Info ​

go
func (w *Window) Info() WindowInfo

Info returns the window as listed when it was found.

func (*Window) Kind ​

go
func (w *Window) Kind() string

Kind returns KindCanvas or KindWebView.

func (*Window) Locate ​

go
func (w *Window) Locate(q Query) *Locator

Locate locates the nodes q selects.

func (*Window) Move ​

go
func (w *Window) Move(x, y float32)

Move moves the pointer to (x, y).

func (*Window) Nodes ​

go
func (w *Window) Nodes() []a11y.Node

Nodes returns the current semantics tree's nodes (waiting for a first tree).

func (*Window) Press ​

go
func (w *Window) Press(keys string)

Press presses and releases a key with modifiers: "Enter", "Tab", "a", "Cmd+N", "CmdOrCtrl+Shift+Z" (key names as ui/input.Key.String prints them; CmdOrCtrl is Cmd on macOS). Window shortcuts see it first, as for real keys.

func (*Window) RightClick ​

go
func (w *Window) RightClick(x, y float32)

RightClick clicks the secondary button at (x, y).

func (*Window) SaveScreenshot ​

go
func (w *Window) SaveScreenshot(path string)

SaveScreenshot writes Screenshot to path (creating directories).

func (*Window) Screenshot ​

go
func (w *Window) Screenshot() []byte

Screenshot returns a PNG of the window's content: the last frame of a self-drawn window (painted again on the CPU), the visible page of a WebView window.

func (*Window) Scroll ​

go
func (w *Window) Scroll(x, y, dx, dy float32)

Scroll scrolls by (dx, dy) with the pointer at (x, y).

func (*Window) String ​

go
func (w *Window) String() string

func (*Window) TextOf ​

go
func (w *Window) TextOf(sel string) string

TextOf waits for the element matching sel and returns its innerText.

func (*Window) Title ​

go
func (w *Window) Title() string

Title returns the title the window had when it was found.

func (*Window) Type ​

go
func (w *Window) Type(text string)

Type sends committed text to the focused widget.

func (*Window) WaitFor ​

go
func (w *Window) WaitFor(l *Locator) a11y.Node

WaitFor waits until l finds its node and returns it.

func (*Window) WaitGone ​

go
func (w *Window) WaitGone(l *Locator)

WaitGone waits until l finds no node.

func (*Window) WaitJS ​

go
func (w *Window) WaitJS(expr string)

WaitJS waits until the JavaScript expression expr is truthy.

func (*Window) WaitSelector ​

go
func (w *Window) WaitSelector(sel string)

WaitSelector waits until an element matches the CSS selector sel.

WindowInfo ​

go
type WindowInfo = wire.WindowInfo

WindowInfo describes an open window of the app.

以 MIT 许可发布