From 4b4ef18bba2532b92c986f1b3f968c8b2c4ca98b Mon Sep 17 00:00:00 2001 From: ankur22 Date: Tue, 11 Jun 2024 15:05:30 +0100 Subject: [PATCH 1/5] Fix jshandle.evaluate Adding itself as the first argument which is the expected behaviour. --- common/js_handle.go | 1 + 1 file changed, 1 insertion(+) diff --git a/common/js_handle.go b/common/js_handle.go index 3f17b79ac..1a6bf6b1a 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) From f1125b4ea002357ae3909ffdb6f3b278b7286d19 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Tue, 11 Jun 2024 15:06:25 +0100 Subject: [PATCH 2/5] Fix jshandle.evaluateHandle Adding itself as the first argument which is the expected behaviour. --- common/js_handle.go | 1 + 1 file changed, 1 insertion(+) diff --git a/common/js_handle.go b/common/js_handle.go index 1a6bf6b1a..a33cf054f 100644 --- a/common/js_handle.go +++ b/common/js_handle.go @@ -135,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) From 3c732b3a654b4da0f4ec515340d1f0c58bb993d5 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Tue, 11 Jun 2024 15:07:29 +0100 Subject: [PATCH 3/5] Add tests for jshandle.evaluate --- tests/js_handle_test.go | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/js_handle_test.go diff --git a/tests/js_handle_test.go b/tests/js_handle_test.go new file mode 100644 index 000000000..a5c2e6092 --- /dev/null +++ b/tests/js_handle_test.go @@ -0,0 +1,56 @@ +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...) + assert.NoError(t, err) + assert.Equal(t, tt.expected, got) + }) + } +} From 51a12cec383bfb688b2fbb0a20e0b19978ca261c Mon Sep 17 00:00:00 2001 From: ankur22 Date: Tue, 11 Jun 2024 15:07:44 +0100 Subject: [PATCH 4/5] Add tests for jshandle.evaluateHandle --- tests/js_handle_test.go | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/js_handle_test.go b/tests/js_handle_test.go index a5c2e6092..bed6fb7e7 100644 --- a/tests/js_handle_test.go +++ b/tests/js_handle_test.go @@ -54,3 +54,56 @@ func TestJSHandleEvaluate(t *testing.T) { }) } } + +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...) + assert.NoError(t, err) + assert.NotNil(t, got) + + j, err := got.JSONValue() + assert.NoError(t, err) + assert.Equal(t, tt.expected, j) + }) + } +} From b5ff202df57e398bb4f40c7a0e875a6c547bdd54 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Tue, 11 Jun 2024 15:17:29 +0100 Subject: [PATCH 5/5] Update jshandle tests with require This will stop the test before a NPD occurs. --- tests/js_handle_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/js_handle_test.go b/tests/js_handle_test.go index bed6fb7e7..7b1bb33e3 100644 --- a/tests/js_handle_test.go +++ b/tests/js_handle_test.go @@ -49,7 +49,7 @@ func TestJSHandleEvaluate(t *testing.T) { require.NotNil(t, result) got, err := result.Evaluate(tt.pageFunc, tt.args...) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, tt.expected, got) }) } @@ -98,11 +98,11 @@ func TestJSHandleEvaluateHandle(t *testing.T) { require.NotNil(t, result) got, err := result.EvaluateHandle(tt.pageFunc, tt.args...) - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, got) j, err := got.JSONValue() - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, tt.expected, j) }) }