Skip to content

Commit

Permalink
Fix frame.title
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ankur22 committed Nov 14, 2024
1 parent 19585e0 commit 77a5872
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion browser/frame_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 11 additions & 6 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion tests/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 77a5872

Please sign in to comment.