diff --git a/internal/driver/chrome/driver_test.go b/internal/driver/chrome/driver_test.go index 103a8b8..f54d5a4 100644 --- a/internal/driver/chrome/driver_test.go +++ b/internal/driver/chrome/driver_test.go @@ -23,6 +23,47 @@ import ( "github.com/priyanshujain/sanderling/internal/verifier" ) +// launchChrome starts a browser on target and returns it with the context every +// later driver call must use. The browser, and the deadline that bounds a call +// against a wedged one, are torn down when the test ends. +// +// A test whose subject is the launch itself (a clearState wipe, a caller +// deadline, a browser outliving its caller's context) builds this by hand: the +// flag, the timeout and the cancel are what it is measuring. +func launchChrome(t *testing.T, target string) (*Driver, context.Context) { + t.Helper() + d := New() + t.Cleanup(func() { _ = d.Terminate(context.Background()) }) + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + t.Cleanup(cancel) + if err := d.Launch(ctx, target, false, nil); err != nil { + t.Fatalf("Launch: %v", err) + } + return d, ctx +} + +// servePage serves html at a real http origin for the duration of the test. A +// data: URL carries the same markup but has an opaque origin, where storage, +// routing and everything else keyed by origin behaves as it never would in an +// app. +func servePage(t *testing.T, html string) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/html") + _, _ = w.Write([]byte(html)) + })) + t.Cleanup(server.Close) + return server +} + +// testdataServer serves the fixture pages in testdata, each at its own path. +func testdataServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.FileServer(http.Dir("testdata"))) + t.Cleanup(server.Close) + return server +} + // TestLaunch_ClearStateWipesStorageForTheTargetOrigin covers the CLI's default // path (--clear-data). The tab sits on about:blank when Launch runs, an opaque // origin that denies storage access, so clearing by script there throws @@ -33,11 +74,7 @@ func TestLaunch_ClearStateWipesStorageForTheTargetOrigin(t *testing.T) { localStorage.setItem("visits", String(visits)); sessionStorage.setItem("tab", "dirty");
` + - `` + - `` + - `` + - `