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

Commit 585625a

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

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
@@ -1009,6 +1009,7 @@ func (f *Frame) InnerHTML(selector string, opts goja.Value) string {
10091009
return val.ToString().String()
10101010
}
10111011

1012+
// InnerText returns the innerText attribute of the element located by selector.
10121013
func (f *Frame) InnerText(selector string, opts goja.Value) string {
10131014
f.log.Debugf("Frame:InnerText", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)
10141015

@@ -1029,7 +1030,17 @@ func (f *Frame) InnerText(selector string, opts goja.Value) string {
10291030
}
10301031

10311032
applySlowMo(f.ctx)
1032-
return value.(string)
1033+
1034+
if value == nil {
1035+
return ""
1036+
}
1037+
1038+
val, ok := value.(goja.Value)
1039+
if !ok {
1040+
k6Throw(f.ctx, "unexpected innerText value type: %T", value)
1041+
}
1042+
1043+
return val.ToString().String()
10331044
}
10341045

10351046
func (f *Frame) InputValue(selector string, opts goja.Value) string {

tests/page_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,41 @@ func TestPageInnerHTML(t *testing.T) {
214214
})
215215
}
216216

217+
func TestPageInnerText(t *testing.T) {
218+
t.Parallel()
219+
220+
t.Run("ok", func(t *testing.T) {
221+
t.Parallel()
222+
223+
p := newTestBrowser(t).NewPage(nil)
224+
p.SetContent(sampleHTML, nil)
225+
assert.Equal(t, "Test\nOne", p.InnerText("div", nil))
226+
})
227+
228+
t.Run("err_empty_selector", func(t *testing.T) {
229+
t.Parallel()
230+
231+
defer func() {
232+
assertPanicErrorContains(t, recover(), "The provided selector is empty")
233+
}()
234+
235+
p := newTestBrowser(t).NewPage(nil)
236+
p.InnerText("", nil)
237+
t.Error("did not panic")
238+
})
239+
240+
t.Run("err_wrong_selector", func(t *testing.T) {
241+
t.Parallel()
242+
243+
tb := newTestBrowser(t)
244+
p := tb.NewPage(nil)
245+
p.SetContent(sampleHTML, nil)
246+
assert.Equal(t, "", p.InnerText("p", tb.rt.ToValue(jsFrameBaseOpts{
247+
Timeout: "100",
248+
})))
249+
})
250+
}
251+
217252
func TestPageInputValue(t *testing.T) {
218253
p := newTestBrowser(t).NewPage(nil)
219254

0 commit comments

Comments
 (0)