Skip to content

Commit

Permalink
Refactor Locator.GetAttribute to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 13, 2024
1 parent 6dadf7a commit f524f45
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ type locatorAPI interface {
IsHidden(opts goja.Value) (bool, error)
Fill(value string, opts goja.Value) error
Focus(opts goja.Value) error
GetAttribute(name string, opts goja.Value) goja.Value
GetAttribute(name string, opts goja.Value) (any, error)
InnerHTML(opts goja.Value) string
InnerText(opts goja.Value) string
TextContent(opts goja.Value) string
Expand Down
19 changes: 7 additions & 12 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,27 +323,22 @@ func (l *Locator) focus(opts *FrameBaseOptions) error {
}

// GetAttribute of the element using locator's selector with strict mode on.
func (l *Locator) GetAttribute(name string, opts goja.Value) any {
func (l *Locator) GetAttribute(name string, opts goja.Value) (any, error) {
l.log.Debugf(
"Locator:GetAttribute", "fid:%s furl:%q sel:%q name:%q opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, name, opts,
)

var err error
defer func() { panicOrSlowMo(l.ctx, err) }()

copts := NewFrameBaseOptions(l.frame.defaultTimeout())
if err = copts.Parse(l.ctx, opts); err != nil {
err = fmt.Errorf("parsing get attribute options: %w", err)
return nil
if err := copts.Parse(l.ctx, opts); err != nil {
return nil, fmt.Errorf("parsing get attribute options: %w", err)
}
var v any
if v, err = l.getAttribute(name, copts); err != nil {
err = fmt.Errorf("getting attribute %q of %q: %w", name, l.selector, err)
return nil
v, err := l.getAttribute(name, copts)
if err != nil {
return nil, fmt.Errorf("getting attribute %q of %q: %w", name, l.selector, err)
}

return v
return v, nil
}

func (l *Locator) getAttribute(name string, opts *FrameBaseOptions) (any, error) {
Expand Down
12 changes: 9 additions & 3 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ func TestLocator(t *testing.T) {
},
},
{
"GetAttribute", func(tb *testBrowser, p *common.Page) {
"GetAttribute", func(_ *testBrowser, p *common.Page) {
l := p.Locator("#inputText", nil)
v := l.GetAttribute("value", nil)
v, err := l.GetAttribute("value", nil)
require.NoError(t, err)
require.NotNil(t, v)
require.Equal(t, "something", v)
},
Expand Down Expand Up @@ -343,7 +344,12 @@ func TestLocator(t *testing.T) {
},
},
{
"GetAttribute", func(l *common.Locator, tb *testBrowser) { l.GetAttribute("value", timeout(tb)) },
"GetAttribute", func(l *common.Locator, tb *testBrowser) {
if _, err := l.GetAttribute("value", timeout(tb)); err != nil {
// TODO: remove panic and update tests when all locator methods return error.
panic(err)
}
},
},
{
"Hover", func(l *common.Locator, tb *testBrowser) { l.Hover(timeout(tb)) },
Expand Down

0 comments on commit f524f45

Please sign in to comment.