| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | // Steps the browser tests share, on chromedp v0.21's generic API |
| 6 | // (rastrillo v0.30 and later). They keep the shapes the drives were |
| 7 | // written against on chromedp v0.14, so a port is a rename and not a |
| 8 | // rewrite of what a drive reads. |
| 9 | |
| 10 | import ( |
| 11 | "context" |
| 12 | "encoding/json" |
| 13 | "reflect" |
| 14 | |
| 15 | "github.com/chromedp/cdproto/cdp" |
| 16 | "github.com/chromedp/cdproto/network" |
| 17 | "github.com/chromedp/cdproto/runtime" |
| 18 | "github.com/chromedp/chromedp" |
| 19 | ) |
| 20 | |
| 21 | // evalInto is a step that evaluates js and decodes the result into out, |
| 22 | // as chromedp v0.14's Evaluate(js, out) did: a script that returns null |
| 23 | // leaves out as it was (or nil, for a slice, map or pointer), and one |
| 24 | // that returns undefined fails unless out can be nil. v0.21's Evaluate |
| 25 | // fails on null for a type that cannot be nil. |
| 26 | func evalInto[T any](out *T, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { |
| 27 | return evalValue(out, js, opts...) |
| 28 | } |
| 29 | |
| 30 | // evalValue is evalInto for an out that is already an any — a pointer, |
| 31 | // or nil to drop the result — as v0.14's Evaluate took it. |
| 32 | func evalValue(out any, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { |
| 33 | // By value, set before the caller's options as v0.14 set it. |
| 34 | all := append([]chromedp.EvaluateOption{func(p *runtime.EvaluateParams) { |
| 35 | p.ReturnByValue = new(true) |
| 36 | }}, opts...) |
| 37 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 38 | v, err := chromedp.Evaluate[*runtime.RemoteObject](js, all...)(ctx, t) |
| 39 | if err != nil || out == nil { |
| 40 | return err |
| 41 | } |
| 42 | return decodeV014(v, out) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | // decodeV014 is chromedp v0.14's parseRemoteObject for a by-value |
| 47 | // result. |
| 48 | func decodeV014(v *runtime.RemoteObject, out any) error { |
| 49 | if raw, ok := out.(*[]byte); ok { |
| 50 | *raw = v.Value |
| 51 | return nil |
| 52 | } |
| 53 | value := []byte(v.Value) |
| 54 | if value == nil { |
| 55 | if rv := reflect.ValueOf(out); rv.Kind() == reflect.Pointer { |
| 56 | switch rv.Elem().Kind() { |
| 57 | case reflect.Pointer, reflect.Map, reflect.Slice, |
| 58 | reflect.Chan, reflect.Func, reflect.Interface: |
| 59 | default: |
| 60 | if v.Type == runtime.RemoteObjectTypeUndefined { |
| 61 | return chromedp.ErrJSUndefined |
| 62 | } |
| 63 | // v0.14's protocol decoder kept a JSON null as the |
| 64 | // bytes "null", which leave out as it was. |
| 65 | } |
| 66 | } |
| 67 | value = []byte("null") |
| 68 | } |
| 69 | return json.Unmarshal(value, out) |
| 70 | } |
| 71 | |
| 72 | // cdpStep is a step that sends one protocol command and drops its |
| 73 | // result, as running a v0.14 cdproto command as an action did. |
| 74 | func cdpStep[P, R any](c cdp.Command[P, R], params P) chromedp.Action[chromedp.Void] { |
| 75 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 76 | _, err := cdp.Call(ctx, t, c, params) |
| 77 | return err |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | // attributeInto is a step that reads the attribute name of the first |
| 82 | // node sel matches into value, and whether it exists into ok when ok is |
| 83 | // not nil, as v0.14's AttributeValue(sel, name, value, ok) did. |
| 84 | func attributeInto(sel chromedp.CSS, name string, value *string, ok *bool, opts ...chromedp.QueryOption) chromedp.Action[chromedp.Void] { |
| 85 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 86 | r, err := chromedp.AttributeValue(sel, name, opts...)(ctx, t) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | *value = r.Value |
| 91 | if ok != nil { |
| 92 | *ok = r.Exists |
| 93 | } |
| 94 | return nil |
| 95 | }) |
| 96 | } |
| 97 | |
| 98 | // blockedURLsParams is Network.setBlockedURLs with the urls field the |
| 99 | // drives were written against: wildcard patterns, matched anywhere in a |
| 100 | // URL. cdproto v0.158 carries only urlPatterns, which take the |
| 101 | // URLPattern syntax and must be absolute, so the old field is sent |
| 102 | // as it was. |
| 103 | type blockedURLsParams struct { |
| 104 | URLs []string `json:"urls"` |
| 105 | } |
| 106 | |
| 107 | var setBlockedURLs = cdp.Command[blockedURLsParams, cdp.Empty]{Method: network.CommandSetBlockedURLs} |
| 108 | |
| 109 | // blockURLs is a step that blocks the URLs matching patterns, or none |
| 110 | // when it has none, as v0.14's network.SetBlockedURLs(patterns) did. |
| 111 | func blockURLs(patterns ...string) chromedp.Action[chromedp.Void] { |
| 112 | return cdpStep(setBlockedURLs, blockedURLsParams{URLs: append([]string{}, patterns...)}) |
| 113 | } |
| 114 | |