automation
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.
常量
const (
KindCanvas = wire.KindCanvas // self-drawn (package ui)
KindWebView = wire.KindWebView // WebView page
)Window kinds.
const (
MatchExact = wire.MatchExact
MatchContains = wire.MatchContains
MatchRegexp = wire.MatchRegexp
)Name matching of Query.Match.
const DefaultTimeout = 10 * time.SecondDefaultTimeout is how long waits (Locator, Window, Eval) last unless Options.Timeout or SetTimeout says otherwise.
const EnvE2E = "TANGO_E2E"EnvE2E must be set for Launch to run (it skips the test otherwise).
const EnvLaunchBackground = client.EnvLaunchBackgroundEnvLaunchBackground 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
type App struct {
// contains filtered or unexported fields
}App is a running application under test.
func Connect
func Connect(t testing.TB, addr, token string) *AppConnect 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
func Launch(t testing.TB, target string, args ...string) *AppLaunch 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
func LaunchOptions(t testing.TB, target string, opts Options) *AppLaunchOptions is Launch with options.
func (*App) Exited
func (a *App) Exited() boolExited reports whether the program has exited (always false for Connect).
func (*App) Output
func (a *App) Output() stringOutput returns what the program printed so far (the last 256 KiB); empty for Connect.
func (*App) Quit
func (a *App) Quit()Quit asks the app to quit and waits until the program exits (killing it after 10s).
func (*App) SetTimeout
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
func (a *App) Timeout() time.DurationTimeout returns the default wait.
func (*App) Window
func (a *App) Window(title string) *WindowWindow waits for a window titled title and returns it.
func (*App) WindowMatching
func (a *App) WindowMatching(what string, match func(WindowInfo) bool) *WindowWindowMatching waits for a window for which match reports true; what describes it in the failure message.
func (*App) Windows
func (a *App) Windows() []WindowInfoWindows lists the open windows.
Error
type Error = wire.ErrorError is an error reported by the app (Code is one of the Code* constants of the protocol: "timeout", "hot_reload", ...).
Locator
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
func (l *Locator) All() []a11y.NodeAll returns every node matching now (Nth ignored).
func (*Locator) Bounds
func (l *Locator) Bounds() a11y.RectBounds waits for the node and returns its rectangle (logical pixels from the top-left of the window's content).
func (*Locator) Click
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
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
func (l *Locator) Collapse()Collapse closes it.
func (*Locator) Count
func (l *Locator) Count() intCount returns how many nodes match now (Nth ignored).
func (*Locator) Decrement
func (l *Locator) Decrement()Decrement steps it down.
func (*Locator) Dismiss
func (l *Locator) Dismiss()Dismiss closes a dialog, popup or message.
func (*Locator) Exists
func (l *Locator) Exists() boolExists 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
func (l *Locator) Expand()Expand opens the node (combobox, tree item, collapse panel).
func (*Locator) First
func (l *Locator) First() *LocatorFirst is Nth(0); Last is Nth(-1).
func (*Locator) Focus
func (l *Locator) Focus()Focus moves the keyboard focus to the node.
func (*Locator) Has
func (l *Locator) Has(s a11y.State) boolHas waits for the node and reports whether it has every state of s.
func (*Locator) Increment
func (l *Locator) Increment()Increment steps a slider or spin button up.
func (*Locator) Last
func (l *Locator) Last() *LocatorLast picks the last match.
func (*Locator) Name
func (l *Locator) Name() stringName waits for the node and returns its accessible name.
func (*Locator) Node
func (l *Locator) Node() a11y.NodeNode waits for the node and returns a copy of it.
func (*Locator) Nth
func (l *Locator) Nth(i int) *LocatorNth picks the i-th match in reading order (0 the first; negative counts from the end, -1 the last).
func (*Locator) Perform
func (l *Locator) Perform(act a11y.Action, arg string) a11y.NodePerform 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
func (l *Locator) Query() QueryQuery returns the locator's query.
func (*Locator) ScrollIntoView
func (l *Locator) ScrollIntoView()ScrollIntoView scrolls the node's ancestors so it is visible.
func (*Locator) SetValue
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
func (l *Locator) String() stringfunc (*Locator) Timeout
func (l *Locator) Timeout(d time.Duration) *LocatorTimeout sets how long this locator waits.
func (*Locator) Type
func (l *Locator) Type(text string)Type focuses the node and sends text as typed (committed) input.
func (*Locator) Value
func (l *Locator) Value() stringValue waits for the node and returns its value.
func (*Locator) Wait
func (l *Locator) Wait() a11y.NodeWait is Node: it waits until the node exists.
func (*Locator) WaitGone
func (l *Locator) WaitGone()WaitGone waits until no node matches.
func (*Locator) WaitState
func (l *Locator) WaitState(s a11y.State) a11y.NodeWaitState waits until the node exists and has every state of s.
func (*Locator) WaitValue
func (l *Locator) WaitValue(v string) a11y.NodeWaitValue waits until the node exists and its Value is v.
func (*Locator) WithState
func (l *Locator) WithState(s a11y.State) *LocatorWithState keeps nodes that have every state of s (a11y.Checked|a11y.Focused).
func (*Locator) WithValue
func (l *Locator) WithValue(v string) *LocatorWithValue keeps nodes whose Value is v.
func (*Locator) WithoutState
func (l *Locator) WithoutState(s a11y.State) *LocatorWithoutState keeps nodes that have none of the states of s.
Options
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
type Query = wire.QueryQuery selects nodes of a self-drawn window's semantics tree; see Window.Locate.
Window
type Window struct {
// contains filtered or unexported fields
}Window is one window of the app under test.
func (*Window) Click
func (w *Window) Click(x, y float32)Click clicks the primary button at (x, y).
func (*Window) ClickSelector
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
func (w *Window) DoubleClick(x, y float32)DoubleClick double-clicks at (x, y).
func (*Window) Drag
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
func (w *Window) Dump() stringDump returns the semantics tree as an indented outline (a11y.Tree.Dump), handy in failure messages.
func (*Window) Eval
func (w *Window) Eval(expr string) json.RawMessageEval 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
func (w *Window) EvalInto(expr string, v any)EvalInto evaluates expr and decodes its value into v.
func (*Window) EvalString
func (w *Window) EvalString(expr string) stringEvalString evaluates expr and returns its value as a string (JSON strings unquoted, other values as JSON text).
func (*Window) Fill
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
func (w *Window) Find(role a11y.Role, name string) *LocatorFind 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
func (w *Window) FindContains(role a11y.Role, sub string) *LocatorFindContains locates nodes with role whose name contains sub (case-insensitive).
func (*Window) FindRegexp
func (w *Window) FindRegexp(role a11y.Role, pattern string) *LocatorFindRegexp locates nodes with role whose name matches the Go regular expression pattern.
func (*Window) ID
func (w *Window) ID() uint64ID returns the window's surface ID.
func (*Window) Info
func (w *Window) Info() WindowInfoInfo returns the window as listed when it was found.
func (*Window) Kind
func (w *Window) Kind() stringKind returns KindCanvas or KindWebView.
func (*Window) Locate
func (w *Window) Locate(q Query) *LocatorLocate locates the nodes q selects.
func (*Window) Move
func (w *Window) Move(x, y float32)Move moves the pointer to (x, y).
func (*Window) Nodes
func (w *Window) Nodes() []a11y.NodeNodes returns the current semantics tree's nodes (waiting for a first tree).
func (*Window) Press
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
func (w *Window) RightClick(x, y float32)RightClick clicks the secondary button at (x, y).
func (*Window) SaveScreenshot
func (w *Window) SaveScreenshot(path string)SaveScreenshot writes Screenshot to path (creating directories).
func (*Window) Screenshot
func (w *Window) Screenshot() []byteScreenshot 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
func (w *Window) Scroll(x, y, dx, dy float32)Scroll scrolls by (dx, dy) with the pointer at (x, y).
func (*Window) String
func (w *Window) String() stringfunc (*Window) TextOf
func (w *Window) TextOf(sel string) stringTextOf waits for the element matching sel and returns its innerText.
func (*Window) Title
func (w *Window) Title() stringTitle returns the title the window had when it was found.
func (*Window) Type
func (w *Window) Type(text string)Type sends committed text to the focused widget.
func (*Window) WaitFor
func (w *Window) WaitFor(l *Locator) a11y.NodeWaitFor waits until l finds its node and returns it.
func (*Window) WaitGone
func (w *Window) WaitGone(l *Locator)WaitGone waits until l finds no node.
func (*Window) WaitJS
func (w *Window) WaitJS(expr string)WaitJS waits until the JavaScript expression expr is truthy.
func (*Window) WaitSelector
func (w *Window) WaitSelector(sel string)WaitSelector waits until an element matches the CSS selector sel.
WindowInfo
type WindowInfo = wire.WindowInfoWindowInfo describes an open window of the app.