Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/jshandle evaluate #1380

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/js_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
res, err := h.execCtx.Eval(h.ctx, pageFunc, args...)
if err != nil {
return nil, fmt.Errorf("evaluating element: %w", err)
Expand All @@ -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)
Expand Down
109 changes: 109 additions & 0 deletions tests/js_handle_test.go
Original file line number Diff line number Diff line change
@@ -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(`<html><head><title>Some title</title></head></html>`, 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(`<html><head><title>Some title</title></head></html>`, 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)
})
}
}
Loading