Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 245011f

Browse files
committed
Add tests for jshandle.evaluate
1 parent 02b26f5 commit 245011f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/js_handle_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestJSHandleEvaluate(t *testing.T) {
11+
t.Parallel()
12+
13+
tests := []struct {
14+
name string
15+
pageFunc string
16+
args []any
17+
expected string
18+
}{
19+
{
20+
name: "no_args",
21+
pageFunc: `handle => handle.innerText`,
22+
args: nil,
23+
expected: "Some title",
24+
},
25+
{
26+
name: "with_args",
27+
pageFunc: `(handle, a, b) => {
28+
const c = a + b;
29+
return handle.innerText + " " + c
30+
}`,
31+
args: []any{1, 2},
32+
expected: "Some title 3",
33+
},
34+
}
35+
for _, tt := range tests {
36+
tt := tt
37+
38+
t.Run(tt.name, func(t *testing.T) {
39+
t.Parallel()
40+
41+
tb := newTestBrowser(t)
42+
p := tb.NewPage(nil)
43+
44+
err := p.SetContent(`<html><head><title>Some title</title></head></html>`, nil)
45+
require.NoError(t, err)
46+
47+
result, err := p.EvaluateHandle(`() => document.head`)
48+
require.NoError(t, err)
49+
require.NotNil(t, result)
50+
51+
got, err := result.Evaluate(tt.pageFunc, tt.args...)
52+
assert.NoError(t, err)
53+
assert.Equal(t, tt.expected, got)
54+
})
55+
}
56+
}

0 commit comments

Comments
 (0)