From 77a5872aa40b07e4338da44f226232c5946a2a48 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Thu, 14 Nov 2024 17:20:46 +0000 Subject: [PATCH] Fix frame.title The fixes involves casting the value from evaluate to a string, and if that doesn't cast to a string we should return an error. The case was missing from the earlier implementation. --- browser/frame_mapping.go | 2 +- common/frame.go | 17 +++++++++++------ tests/frame_test.go | 5 ++++- 3 files changed, 16 insertions(+), 8 deletions(-) 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) {