diff --git a/browser/frame_mapping.go b/browser/frame_mapping.go index f300070ce..d414c7dcb 100644 --- a/browser/frame_mapping.go +++ b/browser/frame_mapping.go @@ -230,7 +230,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop }, "title": func() *sobek.Promise { return k6ext.Promise(vu.Context(), func() (any, error) { - return f.Title(), nil + return f.Title() }) }, "type": func(selector, text string, opts sobek.Value) *sobek.Promise { diff --git a/common/frame.go b/common/frame.go index 390f85e09..7f35757f6 100644 --- a/common/frame.go +++ b/common/frame.go @@ -1738,15 +1738,20 @@ func (f *Frame) Timeout() time.Duration { } // Title returns the title of the frame. -func (f *Frame) Title() string { +func (f *Frame) Title() (string, error) { f.log.Debugf("Frame:Title", "fid:%s furl:%q", f.ID(), f.URL()) - script := `() => document.title` - - // TODO: return error + js := `() => document.title` + v, err := f.Evaluate(js) + if err != nil { + return "", fmt.Errorf("getting frame title: %w", err) + } + s, ok := v.(string) + if !ok { + return "", fmt.Errorf("getting frame title: expected string, got %T", v) + } - v, _ := f.Evaluate(script) - return v.(string) //nolint:forcetypeassert + return s, nil } // Type text on the first element found matches the selector. diff --git a/tests/frame_test.go b/tests/frame_test.go index f948996bc..a43bcceba 100644 --- a/tests/frame_test.go +++ b/tests/frame_test.go @@ -168,7 +168,10 @@ func TestFrameTitle(t *testing.T) { nil, ) require.NoError(t, err) - assert.Equal(t, "Some title", p.MainFrame().Title()) + + title, err := p.MainFrame().Title() + assert.NoError(t, err) + assert.Equal(t, "Some title", title) } func TestFrameGetAttribute(t *testing.T) {