apptest

package
v0.97.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 18 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 apptest is for an app's tests of its Worker (the app's test/ folder), which go tool charter test runs against the Worker on wrangler dev, and go tool charter test --deployed against the deployed one. The same tests ask both; a test that only one can answer says so with Local.

func TestHome(t *testing.T) {
	if !strings.Contains(apptest.Get(t, "/"), "<h1>") { … }
}

test/dev holds tests for a local Worker only, test/deployed for a deployed one only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base

func Base(t testing.TB) string

Base is the Worker's address (charter's BASE), without a trailing slash. A test run without one (go test ./..., which asks no Worker) is skipped.

func Events

func Events(t testing.TB, path string, header http.Header) (next func() string)

Events opens a server-sent event stream at path and returns a function that gives its next event (its lines, without comments), failing the test when none comes in time. The stream closes with the test.

func Get

func Get(t testing.TB, path string, headers ...string) string

Get is the body of a GET of path, which must answer 200.

func Local

func Local() bool

Local says the Worker is wrangler dev on this machine (charter's TARGET), which can do what a deployed one cannot: have a Cron Trigger fired by hand, be handed an email. It also answers at once where Cloudflare takes its time.

func Mailed added in v0.82.0

func Mailed(t testing.TB, to string, since time.Time) string

Mailed is the link in the newest message the local Worker sent to an address after the moment since, as a path of the app: for a test of what an app sends (a way back in, a confirmation). A message with a page goes a moment after its reader was answered, so this waits for it. wrangler dev sends nothing: it keeps each message as a file under the app's .wrangler, which is where this reads it. A deployed Worker's mail goes to its reader alone.

func MailedFile added in v0.86.0

func MailedFile(t testing.TB, to string, since time.Time) []byte

MailedFile is the file that went with the newest message the local Worker sent to an address after the moment since (a page of the app as a PDF): nil for a message with none.

func MailedPage added in v0.83.0

func MailedPage(t testing.TB, to string, since time.Time) string

MailedPage is what the newest message the local Worker sent to an address shows: its HTML, "" for a message that is its words alone. It waits for a message that was sent after the moment since.

func Secret

func Secret(t testing.TB, secret cloud.Variable) string

Secret is a secret of the Worker for a test that needs one (a token, by its value in the app's env: apptest.Secret(t, env.WRITE_TOKEN)): the environment's, or for a local Worker the throwaway value in the app's .dev.vars. A deployed Worker's secret that the environment lacks skips the test.

Types

type Page added in v0.31.0

type Page struct {
	// contains filtered or unexported fields
}

Page is a tab of a real browser (Chrome, headless) on the Worker under test: what a reader sees and does. A test of a page is written with it, so that htmx, the stream and the scripts Vite built are part of what is tested.

home := apptest.Browser(t)
home.Open("/")
home.Type(`#post input[name=body]`, "hello")
home.Click(`#post button`)
home.WaitText("#notes", "hello")

func Browser added in v0.31.0

func Browser(t testing.TB) *Page

Browser starts Chrome and gives its first tab. It is closed with the test. A machine without Chrome fails the test with what is missing: a page is not tested by leaving its test out.

func (*Page) AsDocument added in v0.87.0

func (p *Page) AsDocument(as string)

func (*Page) Attr added in v0.31.0

func (p *Page) Attr(selector, name string) string

Attr is an attribute of the element a CSS selector names, "" without it.

func (*Page) Click added in v0.31.0

func (p *Page) Click(selector string)

Click clicks the element a CSS selector names. A form's submit button submits the form, as a reader's click does.

func (*Page) Fits added in v0.74.0

func (p *Page) Fits()

Fits fails the test when the page being shown is wider than its window, or when something a reader presses in its header (a link, a button) is partly outside it, or when something that opens over the page (a menu) is seen before it is opened: what a layout is for, at whatever width the window is (Phone, Wide). A table or a sample that is too wide scrolls where it is, and the page does not.

func (*Page) Has added in v0.41.0

func (p *Page) Has(selector string) bool

Has reports whether the page has an element a CSS selector names, now: for what must not be there.

func (*Page) InApp added in v0.94.0

func (p *Page) InApp()

InApp makes the tab the app's own phone app: Hotwire Native around the pages, which says so in its user agent as the iOS app's does ("… Hotwire Native iOS; Turbo Native iOS; bridge-components: […]"). Before Open.

func (*Page) JS added in v0.46.6

func (p *Page) JS(expression string) string

JS evaluates an expression in the page and gives its value as text: for what a test cannot do as a reader does, such as standing in for the device's share sheet.

func (*Page) Leave added in v0.45.0

func (p *Page) Leave(signIn string)

Leave deletes the account the tab is signed up with when the test is done, for a test that signed up by hand (SignUp does this itself).

func (*Page) Open added in v0.31.0

func (p *Page) Open(path string)

Open goes to a path of the Worker and waits for the page to load.

func (*Page) Path added in v0.31.0

func (p *Page) Path() string

Path is the path of the page the tab is on.

func (*Page) Phone added in v0.65.0

func (p *Page) Phone()

Phone makes the tab as wide as a phone held upright (390 by 800), for what a page shows only there: a menu in place of a list at the side.

func (*Page) PrefersDark added in v0.87.0

func (p *Page) PrefersDark()

PrefersDark has the tab say its machine prefers dark, as a reader's does who set theirs so; AsDocument has every request the tab makes ask for its page as a document (app.DocumentPDF, app.DocumentLetter), as the app's own browser does when it draws one. For a test of what a page is on such a machine, and as such a document.

func (*Page) Select added in v0.41.0

func (p *Page) Select(selector, value string)

Select chooses the option with that value in the menu a CSS selector names, as a reader does: its change event follows.

func (*Page) SignIn added in v0.40.0

func (p *Page) SignIn(signIn string)

SignIn signs in on the sign-in screen with the passkey the tab's authenticator has (after SignUp and signing out), and waits until the tab is signed in.

func (*Page) SignUp added in v0.40.0

func (p *Page) SignUp(signIn, name string)

SignUp makes an account on the app's sign-in screen (charter add screen:sign-in), at its path (with ?invite= and a code, as an invitation's link leads there), under a name, and waits until the tab is signed in: the passkey is made by the tab's virtual authenticator. It is how a test of a page for someone signed in gets there, as a reader would. A tab that is on that page already (a link took it there) signs up on the page as it stands; any other opens it.

func (*Page) Tab added in v0.31.0

func (p *Page) Tab() *Page

Tab is another tab of the same browser: a second reader.

func (*Page) Text added in v0.31.0

func (p *Page) Text(selector string) string

Text is the text of the element a CSS selector names.

func (*Page) Type added in v0.31.0

func (p *Page) Type(selector, text string)

Type puts text into the field a CSS selector names, as typing does: the field's value, then its input event.

func (*Page) WaitText added in v0.31.0

func (p *Page) WaitText(selector, text string)

WaitText waits until the element a CSS selector names contains text, and fails the test with what it shows instead when it does not in time.

func (*Page) Wide added in v0.74.0

func (p *Page) Wide(width int)

Wide makes the tab this many CSS pixels wide (and 800 high): a tablet's 768, a small laptop's 1024, a desk's 1280. A layout shows and hides its parts by the window's width, and a test says at which it looks.

Jump to

Keyboard shortcuts

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