Go client
Beta
The Go client is tested in CI, but its API may still change.
The go.puth.io module drives a browser from go test. Its Browser methods are generated from the server source, like those of the other clients, and puth.NewBrowser(t) starts a browser that ends with its test. The module has no dependencies. For a Laravel Go application, see the Laravel Go integration.
Install the module
go get go.puth.ioThe module needs Go 1.25 or newer.
Start the server
Run a Puth server as described in Getting started. Tests connect to http://127.0.0.1:7345; set PUTH_INSTANCE_URL to use another instance.
Write a test
package example_test
import (
"testing"
puth "go.puth.io"
)
func TestThePageLoads(t *testing.T) {
browser := puth.NewBrowser(t)
browser.Visit("https://playground.puth.dev").AssertSee("Puth")
}Run your tests
go test ./...Open the Puth GUI to follow each test while it runs. Tests are listed by their name and grouped by their package.
Browsers
puth.NewBrowser(t) starts a browser for the test and closes it when the test ends. A test that failed is marked as failed on the instance. Every call starts a browser with a session of its own:
func TestTwoVisitors(t *testing.T) {
first, second := puth.NewBrowser(t), puth.NewBrowser(t)
first.Visit("https://example.com").AssertTitle("Example Domain")
second.Visit("https://example.com/missing").AssertDontSee("Example Domain")
}All browsers of a test belong to one context on the instance, so the GUI shows them as one test. A subtest of t.Run is a test of its own. Tests may run in parallel with t.Parallel().
Options go after t:
browser := puth.NewBrowser(t, puth.Viewport(390, 844), puth.Timeout(2000))| Option | Default | Sets |
|---|---|---|
Viewport(width, height) | 1280x720 | The size of the page in pixels. |
Headless(bool) | true | Whether the browser runs without a window. |
Timeout(milliseconds) | 3000 | How long a call waits for what it needs, like an element or a text. |
TimeoutMultiplier(factor) | 1 | What a timeout passed to a call is multiplied with; 1000 makes those timeouts seconds. |
ResolverPrefix(selector) | body | The selector that every resolved selector is searched in. |
FitOnFailure(bool) | true | Whether the window grows to the page before the screenshot of a failed test. |
Actionability(bool) | true | Whether actions wait until their element can be used. |
LaunchOption(key, value) | A launch option of the browser, as Puppeteer names it. | |
BrowserOption(key, value) | An option of the returned Browser, as the server names it. | |
Instance(url) | PUTH_INSTANCE_URL | The Puth instance of the test. |
Snapshot(bool) | true | Whether the instance records the test. |
Instance and Snapshot belong to the context, so they only count on the first browser of a test or in puth.Use.
Options for every browser of a test
puth.Use(t, options...) sets options for every browser that the test starts afterwards. Call it where your tests are set up, before the first browser:
func setUp(t *testing.T) {
puth.Use(t, puth.Timeout(5000), puth.Portal("http://app.test", handler))
}Use does not reach the instance, so a test that starts no browser costs nothing.
Method names
Methods are named like those of the other clients, with a capital letter: assertSee is AssertSee. This documentation writes them that way while the Go client is selected. A method returns the browser, so calls chain, and each one runs when it is called:
browser.
Visit("https://app.test/login").
Type("email", "ada@example.com").
Type("password", "secret").
Press("Log in").
AssertPathIs("/dashboard")
heading := browser.Text("h1")Value(selector) reads a value and SetValue(selector, value) sets it.
Optional parameters
Go has no optional parameters, so a method that has them comes in two forms. The plain one takes the required parameters, and the one that ends in With takes all of them:
browser.WaitFor(".result")
browser.WaitForWith(".result", 10_000)
browser.AssertSee("welcome")
browser.AssertSeeWith("welcome", true) // ignores caseSignatures in this documentation mark optional parameters with ?. Pass them to the With form. A timeout or a selector that should keep its default is nil, and options, arrays and objects are maps and slices:
browser.WaitForTextWith("welcome", nil, true)
browser.ClickWith("#save", map[string]any{"force": true})
browser.SetCookie([]map[string]any{{"name": "theme", "value": "dark", "url": "https://app.test"}})Failures
Puth is a testing tool, so a step that fails ends the test with t.Fatal. The message names the step, and the failure is reported at the line of the test. Try() returns the browser for steps that may fail; Err() returns the first error, and the steps after it are skipped:
if browser.Try().WaitForWith(".cookie-banner", 500).Err() == nil {
browser.Press("Accept")
}The error is a *puth.Error when the instance reported it, with the step, the message, and for a failed assertion what was expected and what was found:
var failed *puth.Error
if errors.As(browser.Try().AssertSee("Welcome").Err(), &failed) {
t.Log(failed.Step, failed.Expected, failed.Actual)
}Recording
The GUI and snapshots show what a context recorded: its commands, the DOM of its pages, console output and requests. The Go client turns recording on for every test. puth.Snapshot(false) turns it off, unless the server runs in debug mode, which records everything.
Answer requests from the test
Portal lets the test process answer the requests of the browser, so an application needs no server of its own and shares its state with the test. puth.Portal takes the URL of the application and an http.Handler:
func TestTheApplicationAnswers(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "You asked for %s", r.URL.Path)
})
browser := puth.NewBrowser(t, puth.Portal("http://app.test", handler))
browser.Visit("/hello").AssertSee("You asked for /hello")
}Paths like /hello are resolved against the URL of the portal. Requests to other URLs load normally. The handler answers each request with one whole response; see the limits of portal. The instance needs the portal-socket and portal-browser-id capabilities, which a current server has.
On the goroutine of the test
A handler runs on the goroutine of the test, while the test is inside a call on its browser. So the test and its handlers never run at the same time:
- What the test set up is what the handler sees, without a lock: a mock, a fake, a clock.
t.Fatal, and the mock libraries that call it, work inside a handler and end the test.- A panic of a handler is a panic of the test.
The other side of it: nothing is answered while the test does something else, like time.Sleep. A page that polls gets its answers during the next call. To let time pass in the browser, use browser.Pause(milliseconds), which is a call.
Beside the test
puth.Concurrent() answers every request on a goroutine of its own, at any time:
puth.Use(t, puth.Portal("http://app.test", handler), puth.Concurrent())A test needs it when a page has to be answered while the test waits for something outside the browser, or when a handler does not return for a long time. In return, t.Fatal must not be called inside a handler, as Go only allows it on the goroutine of the test, and what the test and its handlers share needs a lock. A panic of a handler fails the request in the browser and the test with t.Errorf. When the test ends, it waits for the handlers that still run.
Several browsers
Every browser of a test has the same portal URL. The handler may differ per browser:
puth.Use(t, puth.Portal("http://app.test", guest))
visitor := puth.NewBrowser(t)
admin := puth.NewBrowser(t, puth.Portal("http://app.test", asAdmin))One handler can also tell the browsers apart: puth.BrowserID(r) returns the browser.ID() of the browser that sent a request, also for its tabs and frames.
The API of the module is documented on pkg.go.dev.
