diff --git a/common/js_handle.go b/common/js_handle.go index 3f17b79ac..a33cf054f 100644 --- a/common/js_handle.go +++ b/common/js_handle.go @@ -124,6 +124,7 @@ func (h *BaseJSHandle) dispose() error { // Evaluate will evaluate provided page function within an execution context. func (h *BaseJSHandle) Evaluate(pageFunc string, args ...any) (any, error) { + args = append([]any{h}, args...) res, err := h.execCtx.Eval(h.ctx, pageFunc, args...) if err != nil { return nil, fmt.Errorf("evaluating element: %w", err) @@ -134,6 +135,7 @@ func (h *BaseJSHandle) Evaluate(pageFunc string, args ...any) (any, error) { // EvaluateHandle will evaluate provided page function within an execution context. func (h *BaseJSHandle) EvaluateHandle(pageFunc string, args ...any) (JSHandleAPI, error) { + args = append([]any{h}, args...) eh, err := h.execCtx.EvalHandle(h.ctx, pageFunc, args...) if err != nil { return nil, fmt.Errorf("evaluating handle for element: %w", err) diff --git a/tests/js_handle_test.go b/tests/js_handle_test.go new file mode 100644 index 000000000..7b1bb33e3 --- /dev/null +++ b/tests/js_handle_test.go @@ -0,0 +1,109 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestJSHandleEvaluate(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + pageFunc string + args []any + expected string + }{ + { + name: "no_args", + pageFunc: `handle => handle.innerText`, + args: nil, + expected: "Some title", + }, + { + name: "with_args", + pageFunc: `(handle, a, b) => { + const c = a + b; + return handle.innerText + " " + c + }`, + args: []any{1, 2}, + expected: "Some title 3", + }, + } + for _, tt := range tests { + tt := tt + + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + p := tb.NewPage(nil) + + err := p.SetContent(`Some title`, nil) + require.NoError(t, err) + + result, err := p.EvaluateHandle(`() => document.head`) + require.NoError(t, err) + require.NotNil(t, result) + + got, err := result.Evaluate(tt.pageFunc, tt.args...) + require.NoError(t, err) + assert.Equal(t, tt.expected, got) + }) + } +} + +func TestJSHandleEvaluateHandle(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + pageFunc string + args []any + expected string + }{ + { + name: "no_args", + pageFunc: `handle => { + return {"innerText": handle.innerText}; + }`, + args: nil, + expected: `{"innerText":"Some title"}`, + }, + { + name: "with_args", + pageFunc: `(handle, a, b) => { + return {"innerText": handle.innerText, "sum": a + b}; + }`, + args: []any{1, 2}, + expected: `{"innerText":"Some title","sum":3}`, + }, + } + for _, tt := range tests { + tt := tt + + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + p := tb.NewPage(nil) + + err := p.SetContent(`Some title`, nil) + require.NoError(t, err) + + result, err := p.EvaluateHandle(`() => document.head`) + require.NoError(t, err) + require.NotNil(t, result) + + got, err := result.EvaluateHandle(tt.pageFunc, tt.args...) + require.NoError(t, err) + assert.NotNil(t, got) + + j, err := got.JSONValue() + require.NoError(t, err) + assert.Equal(t, tt.expected, j) + }) + } +}