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 ¶
- func Base(t testing.TB) string
- func Events(t testing.TB, path string, header http.Header) (next func() string)
- func Get(t testing.TB, path string, headers ...string) string
- func Local() bool
- func Mailed(t testing.TB, to string, since time.Time) string
- func MailedFile(t testing.TB, to string, since time.Time) []byte
- func MailedPage(t testing.TB, to string, since time.Time) string
- func Secret(t testing.TB, secret cloud.Variable) string
- type Page
- func (p *Page) AsDocument(as string)
- func (p *Page) Attr(selector, name string) string
- func (p *Page) Click(selector string)
- func (p *Page) Fits()
- func (p *Page) Has(selector string) bool
- func (p *Page) InApp()
- func (p *Page) JS(expression string) string
- func (p *Page) Leave(signIn string)
- func (p *Page) Open(path string)
- func (p *Page) Path() string
- func (p *Page) Phone()
- func (p *Page) PrefersDark()
- func (p *Page) Select(selector, value string)
- func (p *Page) SignIn(signIn string)
- func (p *Page) SignUp(signIn, name string)
- func (p *Page) Tab() *Page
- func (p *Page) Text(selector string) string
- func (p *Page) Type(selector, text string)
- func (p *Page) WaitText(selector, text string)
- func (p *Page) Wide(width int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Base ¶
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 ¶
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 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
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
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
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 ¶
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
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 (*Page) Attr ¶ added in v0.31.0
Attr is an attribute of the element a CSS selector names, "" without it.
func (*Page) Click ¶ added in v0.31.0
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
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
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
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
Open goes to a path of the Worker and waits for the page to load.
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
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
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
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) Type ¶ added in v0.31.0
Type puts text into the field a CSS selector names, as typing does: the field's value, then its input event.