//go:build browser package calendartest // Steps the browser tests share, on chromedp v0.21's generic API (rastrillo // v0.30): what v0.14's builder calls did, in one place. import ( "context" "net/http" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" ) // evalInto is a step that evaluates js and decodes the result into out. A // script that returns null or undefined leaves out as it was, as chromedp // v0.14's Evaluate did; v0.21's fails on null for a type that cannot be // nil, so the result is read through a pointer. A nil out drops it. func evalInto[T any](out *T, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { v, err := chromedp.Evaluate[*T](js, opts...)(ctx, t) if err != nil { return err } if v != nil && out != nil { *out = *v } return nil }) } // evalOnly is a step that evaluates js for its effect alone. func evalOnly(js string) chromedp.Action[chromedp.Void] { return evalInto[any](nil, js) } // command is a step that sends one protocol command and drops its result. func command[P, R any](c cdp.Command[P, R], params P) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { _, err := cdp.Call(ctx, t, c, params) return err }) } // setCookie is a step that sets a sign-in cookie for the rig's origin. func setCookie(origin string, c *http.Cookie) chromedp.Action[chromedp.Void] { return command(network.SetCookie, network.SetCookieParams{ Name: c.Name, Value: c.Value, URL: origin + "/", HTTPOnly: &c.HttpOnly, SameSite: network.CookieSameSiteLax, }) } // deviceMetrics is a step that sets a mobile viewport width × height at 1x. func deviceMetrics(width, height int64) chromedp.Action[chromedp.Void] { return command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{ Width: width, Height: height, DeviceScaleFactor: 1, Mobile: true, }) } // forceLight is a step that puts the page in the light scheme, whatever the // machine's (this Mac is dark). func forceLight() chromedp.Action[chromedp.Void] { return command(emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{ Features: []*emulation.MediaFeature{{Name: "prefers-color-scheme", Value: "light"}}, }) }