| 1 | //go:build browser |
| 2 | |
| 3 | package sheets |
| 4 | |
| 5 | // Steps the browser tests share, on chromedp v0.21's generic API |
| 6 | // (rastrillo v0.30). |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "net/http" |
| 11 | |
| 12 | "github.com/chromedp/cdproto/cdp" |
| 13 | "github.com/chromedp/cdproto/network" |
| 14 | "github.com/chromedp/chromedp" |
| 15 | ) |
| 16 | |
| 17 | // evalInto is a step that evaluates js and decodes the result into out. |
| 18 | // A script that returns null or undefined leaves out as it was, as |
| 19 | // chromedp v0.14's Evaluate did; v0.21's fails on null for a type that |
| 20 | // cannot be nil, so the result is read through a pointer. |
| 21 | func evalInto[T any](out *T, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { |
| 22 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 23 | v, err := chromedp.Evaluate[*T](js, opts...)(ctx, t) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | if v != nil && out != nil { |
| 28 | *out = *v |
| 29 | } |
| 30 | return nil |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | // command is a step that sends one protocol command and drops its result. |
| 35 | func command[P, R any](c cdp.Command[P, R], params P) chromedp.Action[chromedp.Void] { |
| 36 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 37 | _, err := cdp.Call(ctx, t, c, params) |
| 38 | return err |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | // setCookie is a step that sets a sign-in cookie for the rig's origin. |
| 43 | func setCookie(origin string, c *http.Cookie) chromedp.Action[chromedp.Void] { |
| 44 | return command(network.SetCookie, network.SetCookieParams{ |
| 45 | Name: c.Name, Value: c.Value, URL: origin + "/", |
| 46 | HTTPOnly: &c.HttpOnly, Secure: &c.Secure, SameSite: network.CookieSameSiteLax, |
| 47 | }) |
| 48 | } |
| 49 | |