| 1 | //go:build browser |
| 2 | |
| 3 | package memoriatest |
| 4 | |
| 5 | // Steps the browser tests share, on chromedp v0.21's generic API |
| 6 | // (rastrillo v0.43). They keep the shapes the drives were written |
| 7 | // against on chromedp v0.14, so the port is a rename and not a rewrite |
| 8 | // of what a drive reads. evalInto, evalValue, decodeV014, cdpStep and |
| 9 | // attributeInto are Slides' (internal/slidestest/browser_steps_test.go). |
| 10 | |
| 11 | import ( |
| 12 | "context" |
| 13 | "encoding/json" |
| 14 | "reflect" |
| 15 | |
| 16 | "amadan.net/rastrillo/rastrillo/harness" |
| 17 | cdpbrowser "github.com/chromedp/cdproto/browser" |
| 18 | "github.com/chromedp/cdproto/cdp" |
| 19 | "github.com/chromedp/cdproto/runtime" |
| 20 | "github.com/chromedp/chromedp" |
| 21 | ) |
| 22 | |
| 23 | // evalInto is a step that evaluates js and decodes the result into out, |
| 24 | // as chromedp v0.14's Evaluate(js, out) did: a script that returns null |
| 25 | // leaves out as it was (or nil, for a slice, map or pointer), and one |
| 26 | // that returns undefined fails unless out can be nil. v0.21's Evaluate |
| 27 | // fails on null for a type that cannot be nil. |
| 28 | func evalInto[T any](out *T, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { |
| 29 | return evalValue(out, js, opts...) |
| 30 | } |
| 31 | |
| 32 | // evalOnly is a step that evaluates js for its effect alone, as v0.14's |
| 33 | // Evaluate(js, nil) did. |
| 34 | func evalOnly(js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { |
| 35 | return evalValue(nil, js, opts...) |
| 36 | } |
| 37 | |
| 38 | // evalValue is evalInto for an out that is already an any — a pointer, |
| 39 | // or nil to drop the result — as v0.14's Evaluate took it. |
| 40 | func evalValue(out any, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { |
| 41 | // By value, set before the caller's options as v0.14 set it. |
| 42 | all := append([]chromedp.EvaluateOption{func(p *runtime.EvaluateParams) { |
| 43 | p.ReturnByValue = new(true) |
| 44 | }}, opts...) |
| 45 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 46 | v, err := chromedp.Evaluate[*runtime.RemoteObject](js, all...)(ctx, t) |
| 47 | if err != nil || out == nil { |
| 48 | return err |
| 49 | } |
| 50 | return decodeV014(v, out) |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | // decodeV014 is chromedp v0.14's parseRemoteObject for a by-value |
| 55 | // result. |
| 56 | func decodeV014(v *runtime.RemoteObject, out any) error { |
| 57 | if raw, ok := out.(*[]byte); ok { |
| 58 | *raw = v.Value |
| 59 | return nil |
| 60 | } |
| 61 | value := []byte(v.Value) |
| 62 | if value == nil { |
| 63 | if rv := reflect.ValueOf(out); rv.Kind() == reflect.Pointer { |
| 64 | switch rv.Elem().Kind() { |
| 65 | case reflect.Pointer, reflect.Map, reflect.Slice, |
| 66 | reflect.Chan, reflect.Func, reflect.Interface: |
| 67 | default: |
| 68 | if v.Type == runtime.RemoteObjectTypeUndefined { |
| 69 | return chromedp.ErrJSUndefined |
| 70 | } |
| 71 | // v0.14's protocol decoder kept a JSON null as the |
| 72 | // bytes "null", which leave out as it was. |
| 73 | } |
| 74 | } |
| 75 | value = []byte("null") |
| 76 | } |
| 77 | return json.Unmarshal(value, out) |
| 78 | } |
| 79 | |
| 80 | // cdpStep is a step that sends one protocol command and drops its |
| 81 | // result, as running a v0.14 cdproto command as an action did. |
| 82 | func cdpStep[P, R any](c cdp.Command[P, R], params P) chromedp.Action[chromedp.Void] { |
| 83 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 84 | _, err := cdp.Call(ctx, t, c, params) |
| 85 | return err |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | // attributeInto is a step that reads the attribute name of the first |
| 90 | // node sel matches into value, and whether it exists into ok when ok is |
| 91 | // not nil, as v0.14's AttributeValue(sel, name, value, ok) did. |
| 92 | func attributeInto(sel chromedp.CSS, name string, value *string, ok *bool, opts ...chromedp.QueryOption) chromedp.Action[chromedp.Void] { |
| 93 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 94 | r, err := chromedp.AttributeValue(sel, name, opts...)(ctx, t) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | *value = r.Value |
| 99 | if ok != nil { |
| 100 | *ok = r.Exists |
| 101 | } |
| 102 | return nil |
| 103 | }) |
| 104 | } |
| 105 | |
| 106 | // locationInto is a step that reads the document's location into out, |
| 107 | // as v0.14's Location(&out) did. |
| 108 | func locationInto(out *string) chromedp.Action[chromedp.Void] { |
| 109 | return harness.Into(out, chromedp.Location()) |
| 110 | } |
| 111 | |
| 112 | // valueInto is a step that reads the value of the first node sel |
| 113 | // matches into out, as v0.14's Value(sel, &out) did. |
| 114 | func valueInto(sel chromedp.CSS, out *string) chromedp.Action[chromedp.Void] { |
| 115 | return harness.Into(out, chromedp.Value(sel)) |
| 116 | } |
| 117 | |
| 118 | // grantPermissionsParams is Browser.grantPermissions, which cdproto |
| 119 | // v0.158 no longer carries (the protocol deprecated it for |
| 120 | // setPermission); sent as v0.14's cdpbrowser.GrantPermissions sent it. |
| 121 | type grantPermissionsParams struct { |
| 122 | Permissions []cdpbrowser.PermissionType `json:"permissions"` |
| 123 | Origin string `json:"origin,omitempty"` |
| 124 | } |
| 125 | |
| 126 | var grantPermissionsCommand = cdp.Command[grantPermissionsParams, cdp.Empty]{Method: "Browser.grantPermissions"} |
| 127 | |
| 128 | // grantPermissions is a step that grants origin the permissions, as |
| 129 | // v0.14's cdpbrowser.GrantPermissions(permissions).WithOrigin(origin). |
| 130 | func grantPermissions(origin string, permissions ...cdpbrowser.PermissionType) chromedp.Action[chromedp.Void] { |
| 131 | return cdpStep(grantPermissionsCommand, grantPermissionsParams{Permissions: permissions, Origin: origin}) |
| 132 | } |
| 133 | |