webtest

package
v1.9.6 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: BSD-3-Clause Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package webtest is the harness that runs a Glutter web app in a headless browser (SPEC §17 "Browser tests", §23.8): an httptest server that serves a build made by internal/webtool and collects the PNG and the results the page posts, launchers for Chromium and Firefox with temporary profiles that run under timeout in their own process group and are killed when the test ends, and Shot, which glutter-web's shot command and the browser tests use. No WebDriver or DevTools library is used: the page reports to the server.

Flutter: no counterpart (flutter test --platform chrome)

Index

Constants

View Source
const ExtraFlagsEnv = "GLUTTER_WEBTEST_FLAGS"

ExtraFlagsEnv names an environment variable whose value (flags separated by blanks) is added to the command line of Chromium, for an environment that needs a flag the harness does not set, such as --no-sandbox in a container without user namespaces.

Flutter: no counterpart

View Source
const FramePath = "/__glutter/frame.html"

FramePath is where Shot serves the page that holds the app in a frame.

Flutter: no counterpart

View Source
const PerfAppPath = "/__glutter/perf-app.html"

PerfAppPath is where Perf serves the app's page with the counter of requestAnimationFrame.

Flutter: no counterpart

Variables

This section is empty.

Functions

func EachBrowser

func EachBrowser(t *testing.T, fn func(t *testing.T, b Browser))

EachBrowser runs fn as a subtest for Chromium and for Firefox. A browser that is not installed fails its subtest with the error of Locate: the browser tests exist to run in both (SPEC §17), so a missing one is not a pass.

Flutter: no counterpart

func FramePage

func FramePage(src string, width, height int, resizes []Resize) []byte

FramePage makes the page that shows the app (src, a URL on the same server) in a frame of exactly width x height CSS pixels, and changes the size of the frame at the times of the resizes. A browser window cannot be made an exact size (Chromium and Firefox keep a minimum width and show toolbars even when headless), but the app only sees the viewport of its page, and a frame gives it the one that is asked for.

Flutter: no counterpart

func InjectRAFCounter

func InjectRAFCounter(index []byte) []byte

InjectRAFCounter returns the page with a script that counts the calls of requestAnimationFrame, before any script of the page runs, and offers the count as window.glutterRafCount(); the calls of setTimeout and setInterval are counted too (window.glutterTimerCount()), and the times between the starts of two callbacks of requestAnimationFrame are kept until window.glutterIntervals() takes them. The page is served from another path than the build's, so it also gets a base address that keeps its relative files (wasm_exec.js, app.wasm) where they are.

Flutter: no counterpart

func Locate

func Locate(b Browser, lookPath func(string) (string, error)) (string, error)

Locate finds the program of the browser with lookPath (exec.LookPath when nil). It fails with BrowserNotFound, naming the programs it looked for.

Flutter: no counterpart

func Median

func Median(d []time.Duration) time.Duration

Median is the middle value of the durations (the upper one for an even count), or zero for none.

Flutter: no counterpart

func ModuleRoot

func ModuleRoot(tb testing.TB) string

ModuleRoot finds the directory of go.mod above the working directory of the test, which the packages of the apps to build are relative to.

Flutter: no counterpart

func ParseLogLine

func ParseLogLine(line string) map[string]string

ParseLogLine splits a log line of the engine (slog's text format, key=value with values in quotes when they hold blanks) into its pairs.

Flutter: no counterpart

func PerfFramePage

func PerfFramePage(src string, width, height, loads int, clicks [][2]float64, gap time.Duration) []byte

PerfFramePage makes the page that runs the app (src, a URL on the same server) in a frame of width x height CSS pixels, loads it loads times one after the other (the frame's address changes, so that each load is a new page), reads when each one drew its first frame from window.glutterTimings, which the loader sets, and posts {kind: "starts", data: [...]} when all have; then, when there are clicks, it dispatches them at the canvas of the app, gap apart, and posts {kind: "clicks", data: [...]} with the frames that followed each (window.glutterFrames, which the counter of InjectRAFCounter keeps: the start and end of each requestAnimationFrame callback); else it posts {kind: "raf", data: {count, timers}} every two seconds.

Flutter: no counterpart

func Shot

func Shot(ctx context.Context, o ShotOptions) ([]byte, error)

Shot opens the page of the build in a headless browser and returns the PNG that the engine posts after its first idle frame (proof method D, SPEC §23.8). It starts the server and the browser and stops both before it returns. It fails with WebAppFailed when the program ends with an error (the error carries what it wrote), BrowserExited when the browser ends first, and BrowserTimeout when the screenshot does not come in time; the errors of Launch come back unchanged.

Flutter: no counterpart (flutter drive --screenshot)

Types

type Browser

type Browser string

Browser is a browser the harness can launch.

Flutter: no counterpart

const (
	// Chromium is Chromium, or Chrome.
	//
	// Flutter: no counterpart
	Chromium Browser = "chromium"
	// Firefox is Firefox.
	//
	// Flutter: no counterpart
	Firefox Browser = "firefox"
)

The browsers of the harness (SPEC §17).

func ParseBrowser

func ParseBrowser(name string) (Browser, error)

ParseBrowser reads the name of a browser: "chromium" or "firefox". It fails with BrowserUnknown for another name.

Flutter: no counterpart

type LaunchOptions

type LaunchOptions struct {
	// Path is the program of the browser; Locate finds it when empty.
	Path string
	// Width and Height are the size of the browser window in CSS pixels at the start;
	// 800x600 when zero.
	Width, Height int
	// Lifetime is how long the browser may run at most: it runs under `timeout -k 5s`, which
	// stops it when the time is up even if nothing else does. Two minutes when zero.
	Lifetime time.Duration
	// ScaleFactor is the device pixel ratio of the pages, 1 when zero. Frames are compared
	// pixel for pixel with the goldens, so the ratio is fixed instead of being the screen's
	// (Chromium: --force-device-scale-factor, Firefox: layout.css.devPixelsPerPx).
	ScaleFactor float64
	// Output receives what the browser writes to its standard output and error; nothing
	// when nil.
	Output io.Writer
}

LaunchOptions are the optional parameters of Launch.

Flutter: no counterpart

type PerfClick

type PerfClick struct {
	X, Y float64
	// Latency is the time from the release of the mouse button to the end of the first frame that
	// began after it: the frame's callback ends when the Go frame (build, layout, paint and the
	// renderer's calls) has run; the GPU's work after it is not counted.
	Latency time.Duration
	// Frames are the durations of the callbacks of the first frames after the release (at most
	// five), Max the longest callback and Gap the longest time between the ends of two frames, in
	// the second after the release.
	Frames   []time.Duration
	Max, Gap time.Duration
}

PerfClick is what followed one click.

Flutter: no counterpart

type PerfFrames

type PerfFrames = perfstats.Frames

PerfFrames is one "frames" line of the engine's statistics (GLUTTER_FRAME_STATS=1): the frames of one second.

Flutter: no counterpart

func ParseFramesLine

func ParseFramesLine(line string) (PerfFrames, bool)

ParseFramesLine reads a "frames" line of the engine's statistics; ok is false for a line that is not one or has a value that is not a number or a duration.

Flutter: no counterpart

type PerfOptions

type PerfOptions struct {
	// Dir is the build to measure (the directory webtool.Build wrote).
	Dir string
	// Browser is the browser to open it in.
	Browser Browser
	// BrowserPath is the program of the browser; found when empty.
	BrowserPath string
	// Query is added to the URL's query. GLUTTER_FRAME_STATS=1 is always added.
	Query url.Values
	// Width and Height are the size of the viewport the app sees in CSS pixels; 1536x864 (a
	// 1920x1080 display at a ratio of 1.25, SPEC §15) when zero.
	Width, Height int
	// ScaleFactor is the device pixel ratio of the browser; 1.25 when zero.
	ScaleFactor float64
	// Loads is how many times the page is loaded to time the start: the first with the browser's
	// cache empty, the others with app.wasm in it. Four when zero.
	Loads int
	// Settle is how long the app runs after the last load before the measurement starts; three
	// seconds when zero.
	Settle time.Duration
	// Measure is how long the frames, the CPU time and the calls of requestAnimationFrame are
	// measured; ten seconds when zero.
	Measure time.Duration
	// Clicks, when set, make the run a measurement of the response to clicks instead of the steady
	// state: after the start, a pointer move, a press and a release (as the mouse does) are
	// dispatched at each position (CSS pixels from the top left of the view), ClickGap apart, and
	// the frames that follow each are timed (PerfResult.Clicks).
	Clicks [][2]float64
	// ClickGap is the time between two clicks; a second and a half when zero.
	ClickGap time.Duration
	// Blank measures a page that has no app at all, for the CPU time that the browser itself
	// takes (the baseline that the idle CPU of an app is read against): there are no loads and no
	// frames.
	Blank bool
	// Timeout is how long the whole run may take; three minutes when zero.
	Timeout time.Duration
	// Output receives the browser's standard output and error.
	Output io.Writer
}

PerfOptions are the parameters of Perf.

Flutter: no counterpart

type PerfResult

type PerfResult struct {
	// Renderer is the renderer and device that the first frame says.
	Renderer string
	// FirstFrames are the times from the start of the page to the first frame of the app, one per
	// load: the first with an empty cache. SinceRun are the times from the start of Run to the
	// first frame, as the engine logs them.
	FirstFrames, SinceRun []time.Duration
	// Frames are the statistics lines of the measured window.
	Frames []PerfFrames
	// RAFCalls is the number of calls of requestAnimationFrame in the window, and TimerCalls the
	// number of calls of setTimeout and setInterval (a timer armed: the Go runtime wakes with
	// setTimeout), or -1 when the page could not tell.
	RAFCalls, TimerCalls int
	// CPU is the CPU time of the browser (every process of its group, and of its X server) in
	// the window, which lasted Window.
	CPU, Window time.Duration
	// Intervals are the times between the starts of two callbacks of requestAnimationFrame in the
	// window, which is the cadence of the frames the browser asked for.
	Intervals []time.Duration
	// Slow are the frames over a 60th of a second that the engine logged on their own, in order.
	Slow []PerfSlow
	// Clicks are the responses to the clicks of PerfOptions.Clicks, in order.
	Clicks []PerfClick
}

PerfResult is what Perf measured.

Flutter: no counterpart

func Perf

func Perf(ctx context.Context, o PerfOptions) (PerfResult, error)

Perf opens the build in a headless browser, times its start in several loads, lets it settle, and then measures for a while: the engine's frame statistics, the calls of requestAnimationFrame and the CPU time of the browser. The app is run with GLUTTER_FRAME_STATS=1. It starts the server and the browser and stops both before it returns; it fails with WebAppFailed when the program ends with an error, BrowserExited when the browser ends first, BrowserTimeout when the run takes too long, and BrowserCPUUnavailable where the CPU time of the browser cannot be read.

Flutter: no counterpart (flutter run --profile, the timeline)

func (PerfResult) Summary

func (r PerfResult) Summary() string

Summary is the result as lines of "key value", for scripts (scripts/perf-web.sh reads them). Times are in milliseconds.

Flutter: no counterpart

type PerfSlow

type PerfSlow struct{ Build, Raster time.Duration }

PerfSlow is a "slow frame" line of the engine.

Flutter: no counterpart

type Process

type Process struct {

	// Err is how the browser ended, valid once Done is closed.
	Err error
	// contains filtered or unexported fields
}

Process is a browser that Launch started.

Flutter: no counterpart

func Launch

func Launch(b Browser, url string, o LaunchOptions) (*Process, error)

Launch starts the browser, headless, with a temporary profile and a home directory of its own (so that it touches nothing of the user's), at the URL. Firefox also gets a private Xvfb display, which Stop ends (see the environment below). It runs under `timeout -k` in a process group of its own; Stop kills the group and removes the profile. It fails with BrowserNotFound when the program is not there and BrowserLaunchFailed when it does not start.

Flutter: no counterpart

func (*Process) CPUTime

func (p *Process) CPUTime() (time.Duration, error)

CPUTime is the CPU time, user and system, that the browser's processes have used so far: every live process of its group and, for Firefox, of the group of its X server. A process that has ended is not counted, so a browser that starts and ends many short processes is read low. It fails with BrowserCPUUnavailable when /proc cannot be read.

Flutter: no counterpart

func (*Process) Done

func (p *Process) Done() <-chan struct{}

Done is closed when the browser has ended, by itself, by the lifetime or by Stop.

Flutter: no counterpart

func (*Process) Stop

func (p *Process) Stop()

Stop kills the browser and every process it started, waits for it, and removes the profile. It is safe to call twice.

Flutter: no counterpart

type Resize

type Resize struct {
	// After is the time after the app's canvas is in the page.
	After time.Duration
	// Width and Height are the new size in CSS pixels.
	Width, Height int
}

Resize is a change of the size of the frame that the app is in, some time after the app has put its canvas into the page, which is when it starts drawing. The app sees it as the browser window getting that size.

Flutter: no counterpart

type Result

type Result struct {
	// Kind says what the result is: "exit" from the loader.
	Kind string `json:"kind"`
	// ExitCode is the exit code of the Go program, for Kind "exit".
	ExitCode int `json:"exitCode"`
	// Message is a message of the page: the error of a program that did not start.
	Message string `json:"message"`
	// Log is what the Go program wrote to its standard output and error.
	Log string `json:"log"`
	// Shown is the text of the message that the loader put into the page when the program ended
	// with an error (the element #glutter-message), or empty.
	Shown string `json:"shown"`
	// Data is the rest of what a test program sent, as it sent it.
	Data json.RawMessage `json:"data,omitempty"`
}

Result is what a page posts to ResultsPath: the loader's report of how the program ended (Kind "exit": the exit code and what it wrote to its standard output and error), or what a test program of its own sends.

Flutter: no counterpart

func Await

func Await(ctx context.Context, o ShotOptions, kind string) (Result, error)

Await opens the page of the build in a headless browser, like Shot, but waits for a result of the program (a Result posted to web.ResultsPath with the Kind) instead of a screenshot: a test program that dispatches its own events and reports what it saw. GLUTTER_SCREENSHOT is not set. It starts the server and the browser and stops both before it returns. It fails with WebAppFailed when the program ends before it posted the result (the error carries what it wrote), BrowserExited when the browser ends first, and BrowserTimeout when the result does not come in time.

Flutter: no counterpart

type Server

type Server struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Server serves a web build over HTTP for a browser, and keeps what the page posts: the PNG of the engine's self-capture (web.ScreenshotPath) and results (web.ResultsPath). It is an httptest.Server: Close stops it.

Flutter: no counterpart

func NewCachingServer

func NewCachingServer(dir string) *Server

NewCachingServer is NewServer for a server that lets the browser keep the files of the build for a while, as a deployed site does (webtool.CachingHandler): the measurement of the start with app.wasm in the browser's cache needs it.

Flutter: no counterpart

func NewServer

func NewServer(dir string) *Server

NewServer starts a server for the build in dir (the directory webtool.Build wrote). Close it when done.

Flutter: no counterpart

func (*Server) AddPage

func (s *Server) AddPage(path string, html []byte)

AddPage serves html at the path (such as "/resize.html"), beside the files of the build: a page of a test that embeds the app.

Flutter: no counterpart

func (*Server) Results

func (s *Server) Results() <-chan Result

Results is the channel of the results the page posted, in order.

Flutter: no counterpart

func (*Server) Screenshots

func (s *Server) Screenshots() <-chan []byte

Screenshots is the channel of the PNGs the page posted, in order.

Flutter: no counterpart

func (*Server) WaitResult

func (s *Server) WaitResult(ctx context.Context) (Result, error)

WaitResult waits for the next result the page posts. It fails with BrowserTimeout when ctx ends first.

Flutter: no counterpart

func (*Server) WaitScreenshot

func (s *Server) WaitScreenshot(ctx context.Context) ([]byte, error)

WaitScreenshot waits for the next PNG the page posts. It fails with BrowserTimeout when ctx ends first.

Flutter: no counterpart

type ShotOptions

type ShotOptions struct {
	// Dir is the build to serve (the directory webtool.Build wrote).
	Dir string
	// Browser is the browser to open it in.
	Browser Browser
	// BrowserPath is the program of the browser; found when empty.
	BrowserPath string
	// Page is the path of the page of the app, "/index.html" when empty.
	Page string
	// Query is added to the URL's query: GLUTTER_* parameters become the program's
	// environment (SPEC §23.1). GLUTTER_SCREENSHOT=1 is always added, which makes the
	// engine post its first idle frame (SPEC §23.8).
	Query url.Values
	// Width and Height are the size of the viewport the app sees, in CSS pixels; 480x640
	// when zero. The app runs in a frame of that size (FramePage).
	Width, Height int
	// ScaleFactor is the device pixel ratio of the browser; 1 when zero.
	ScaleFactor float64
	// Resizes change the size of the frame some time after the page loads.
	Resizes []Resize
	// Timeout is how long to wait for the screenshot; one minute when zero.
	Timeout time.Duration
	// Output receives the browser's standard output and error.
	Output io.Writer
	// Server, when set, is used instead of a new one for Dir: a test that has added pages to
	// it. Shot does not close it.
	Server *Server
}

ShotOptions are the parameters of Shot.

Flutter: no counterpart

Jump to

Keyboard shortcuts

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