//go:build browser package memoriatest // Steps the browser tests share, on chromedp v0.21's generic API // (rastrillo v0.43). They keep the shapes the drives were written // against on chromedp v0.14, so the port is a rename and not a rewrite // of what a drive reads. evalInto, evalValue, decodeV014, cdpStep and // attributeInto are Slides' (internal/slidestest/browser_steps_test.go). import ( "context" "encoding/json" "reflect" "amadan.net/rastrillo/rastrillo/harness" cdpbrowser "github.com/chromedp/cdproto/browser" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/runtime" "github.com/chromedp/chromedp" ) // evalInto is a step that evaluates js and decodes the result into out, // as chromedp v0.14's Evaluate(js, out) did: a script that returns null // leaves out as it was (or nil, for a slice, map or pointer), and one // that returns undefined fails unless out can be nil. v0.21's Evaluate // fails on null for a type that cannot be nil. func evalInto[T any](out *T, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { return evalValue(out, js, opts...) } // evalOnly is a step that evaluates js for its effect alone, as v0.14's // Evaluate(js, nil) did. func evalOnly(js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { return evalValue(nil, js, opts...) } // evalValue is evalInto for an out that is already an any — a pointer, // or nil to drop the result — as v0.14's Evaluate took it. func evalValue(out any, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { // By value, set before the caller's options as v0.14 set it. all := append([]chromedp.EvaluateOption{func(p *runtime.EvaluateParams) { p.ReturnByValue = new(true) }}, opts...) return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { v, err := chromedp.Evaluate[*runtime.RemoteObject](js, all...)(ctx, t) if err != nil || out == nil { return err } return decodeV014(v, out) }) } // decodeV014 is chromedp v0.14's parseRemoteObject for a by-value // result. func decodeV014(v *runtime.RemoteObject, out any) error { if raw, ok := out.(*[]byte); ok { *raw = v.Value return nil } value := []byte(v.Value) if value == nil { if rv := reflect.ValueOf(out); rv.Kind() == reflect.Pointer { switch rv.Elem().Kind() { case reflect.Pointer, reflect.Map, reflect.Slice, reflect.Chan, reflect.Func, reflect.Interface: default: if v.Type == runtime.RemoteObjectTypeUndefined { return chromedp.ErrJSUndefined } // v0.14's protocol decoder kept a JSON null as the // bytes "null", which leave out as it was. } } value = []byte("null") } return json.Unmarshal(value, out) } // cdpStep is a step that sends one protocol command and drops its // result, as running a v0.14 cdproto command as an action did. func cdpStep[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 }) } // attributeInto is a step that reads the attribute name of the first // node sel matches into value, and whether it exists into ok when ok is // not nil, as v0.14's AttributeValue(sel, name, value, ok) did. func attributeInto(sel chromedp.CSS, name string, value *string, ok *bool, opts ...chromedp.QueryOption) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { r, err := chromedp.AttributeValue(sel, name, opts...)(ctx, t) if err != nil { return err } *value = r.Value if ok != nil { *ok = r.Exists } return nil }) } // locationInto is a step that reads the document's location into out, // as v0.14's Location(&out) did. func locationInto(out *string) chromedp.Action[chromedp.Void] { return harness.Into(out, chromedp.Location()) } // valueInto is a step that reads the value of the first node sel // matches into out, as v0.14's Value(sel, &out) did. func valueInto(sel chromedp.CSS, out *string) chromedp.Action[chromedp.Void] { return harness.Into(out, chromedp.Value(sel)) } // grantPermissionsParams is Browser.grantPermissions, which cdproto // v0.158 no longer carries (the protocol deprecated it for // setPermission); sent as v0.14's cdpbrowser.GrantPermissions sent it. type grantPermissionsParams struct { Permissions []cdpbrowser.PermissionType `json:"permissions"` Origin string `json:"origin,omitempty"` } var grantPermissionsCommand = cdp.Command[grantPermissionsParams, cdp.Empty]{Method: "Browser.grantPermissions"} // grantPermissions is a step that grants origin the permissions, as // v0.14's cdpbrowser.GrantPermissions(permissions).WithOrigin(origin). func grantPermissions(origin string, permissions ...cdpbrowser.PermissionType) chromedp.Action[chromedp.Void] { return cdpStep(grantPermissionsCommand, grantPermissionsParams{Permissions: permissions, Origin: origin}) }