Skip to content

Commit

Permalink
Refactor Locator.Focus to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 13, 2024
1 parent c610031 commit 6dadf7a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ type locatorAPI interface {
IsVisible(opts goja.Value) (bool, error)
IsHidden(opts goja.Value) (bool, error)
Fill(value string, opts goja.Value) error
Focus(opts goja.Value)
Focus(opts goja.Value) error
GetAttribute(name string, opts goja.Value) goja.Value
InnerHTML(opts goja.Value) string
InnerText(opts goja.Value) string
Expand Down
19 changes: 9 additions & 10 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,20 @@ func (l *Locator) fill(value string, opts *FrameFillOptions) error {
}

// Focus on the element using locator's selector with strict mode on.
func (l *Locator) Focus(opts goja.Value) {
func (l *Locator) Focus(opts goja.Value) error {
l.log.Debugf("Locator:Focus", "fid:%s furl:%q sel:%q opts:%+v", l.frame.ID(), l.frame.URL(), l.selector, 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 focus options: %w", err)
return
if err := copts.Parse(l.ctx, opts); err != nil {
return fmt.Errorf("parsing focus options: %w", err)
}
if err = l.focus(copts); err != nil {
err = fmt.Errorf("focusing on %q: %w", l.selector, err)
return
if err := l.focus(copts); err != nil {
return fmt.Errorf("focusing on %q: %w", l.selector, err)
}

applySlowMo(l.ctx)

return nil
}

func (l *Locator) focus(opts *FrameBaseOptions) error {
Expand Down
13 changes: 9 additions & 4 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ func TestLocator(t *testing.T) {
},
},
{
"Focus", func(tb *testBrowser, p *common.Page) {
"Focus", func(_ *testBrowser, p *common.Page) {
focused := func() bool {
v := p.Evaluate(
`() => document.activeElement == document.getElementById('inputText')`,
)
return asBool(t, v)
}
l := p.Locator("#inputText", nil)
lo := p.Locator("#inputText", nil)
require.False(t, focused(), "should not be focused first")
l.Focus(nil)
require.NoError(t, lo.Focus(nil))
require.True(t, focused(), "should be focused")
},
},
Expand Down Expand Up @@ -327,7 +327,12 @@ func TestLocator(t *testing.T) {
},
},
{
"Focus", func(l *common.Locator, tb *testBrowser) { l.Focus(timeout(tb)) },
"Focus", func(l *common.Locator, tb *testBrowser) {
if err := l.Focus(timeout(tb)); err != nil {
// TODO: remove panic and update tests when all locator methods return error.
panic(err)
}
},
},
{
"Fill", func(l *common.Locator, tb *testBrowser) {
Expand Down

0 comments on commit 6dadf7a

Please sign in to comment.