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

Commit c08e2ed

Browse files
author
Ivan Mirić
committed
Fix Page.TextContent()
This would previously panic with `interface conversion: interface {} is goja.asciiString, not string`
1 parent 585625a commit c08e2ed

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

common/frame.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,7 @@ func (f *Frame) Tap(selector string, opts goja.Value) {
14251425
applySlowMo(f.ctx)
14261426
}
14271427

1428+
// TextContent returns the textContent attribute of the element located by selector.
14281429
func (f *Frame) TextContent(selector string, opts goja.Value) string {
14291430
f.log.Debugf("Frame:TextContent", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)
14301431

@@ -1445,7 +1446,17 @@ func (f *Frame) TextContent(selector string, opts goja.Value) string {
14451446
}
14461447

14471448
applySlowMo(f.ctx)
1448-
return value.(string)
1449+
1450+
if value == nil {
1451+
return ""
1452+
}
1453+
1454+
val, ok := value.(goja.Value)
1455+
if !ok {
1456+
k6Throw(f.ctx, "unexpected textContent value type: %T", value)
1457+
}
1458+
1459+
return val.ToString().String()
14491460
}
14501461

14511462
func (f *Frame) Title() string {

tests/page_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,41 @@ func TestPageInnerText(t *testing.T) {
249249
})
250250
}
251251

252+
func TestPageTextContent(t *testing.T) {
253+
t.Parallel()
254+
255+
t.Run("ok", func(t *testing.T) {
256+
t.Parallel()
257+
258+
p := newTestBrowser(t).NewPage(nil)
259+
p.SetContent(sampleHTML, nil)
260+
assert.Equal(t, "TestOne", p.TextContent("div", nil))
261+
})
262+
263+
t.Run("err_empty_selector", func(t *testing.T) {
264+
t.Parallel()
265+
266+
defer func() {
267+
assertPanicErrorContains(t, recover(), "The provided selector is empty")
268+
}()
269+
270+
p := newTestBrowser(t).NewPage(nil)
271+
p.TextContent("", nil)
272+
t.Error("did not panic")
273+
})
274+
275+
t.Run("err_wrong_selector", func(t *testing.T) {
276+
t.Parallel()
277+
278+
tb := newTestBrowser(t)
279+
p := tb.NewPage(nil)
280+
p.SetContent(sampleHTML, nil)
281+
assert.Equal(t, "", p.TextContent("p", tb.rt.ToValue(jsFrameBaseOpts{
282+
Timeout: "100",
283+
})))
284+
})
285+
}
286+
252287
func TestPageInputValue(t *testing.T) {
253288
p := newTestBrowser(t).NewPage(nil)
254289

0 commit comments

Comments
 (0)