From f524f45b1246ba1f054cfa8a73aa16da248738f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 13 May 2024 16:24:54 +0300 Subject: [PATCH] Refactor Locator.GetAttribute to return err --- browser/mapping_test.go | 2 +- common/locator.go | 19 +++++++------------ tests/locator_test.go | 12 +++++++++--- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/browser/mapping_test.go b/browser/mapping_test.go index d1fc078bd..370586930 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -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 diff --git a/common/locator.go b/common/locator.go index 9a68b5d21..9311466ae 100644 --- a/common/locator.go +++ b/common/locator.go @@ -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) { diff --git a/tests/locator_test.go b/tests/locator_test.go index 1aba3b70d..92ccd788d 100644 --- a/tests/locator_test.go +++ b/tests/locator_test.go @@ -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) }, @@ -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)) },