Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Refactor Locator.Tap to return error
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Apr 17, 2024
1 parent e4c6ed3 commit 2f3be40
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ type locatorAPI interface {
Press(key string, opts goja.Value)
Type(text string, opts goja.Value)
Hover(opts goja.Value)
Tap(opts goja.Value)
Tap(opts goja.Value) error
DispatchEvent(typ string, eventInit, opts goja.Value)
WaitFor(opts goja.Value)
}
Expand Down
19 changes: 9 additions & 10 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,21 +561,20 @@ func (l *Locator) hover(opts *FrameHoverOptions) error {
}

// Tap the element found that matches the locator's selector with strict mode on.
func (l *Locator) Tap(opts goja.Value) {
func (l *Locator) Tap(opts goja.Value) error {
l.log.Debugf("Locator:Tap", "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 := NewFrameTapOptions(l.frame.defaultTimeout())
if err = copts.Parse(l.ctx, opts); err != nil {
err = fmt.Errorf("parsing tap options: %w", err)
return
if err := copts.Parse(l.ctx, opts); err != nil {
return fmt.Errorf("parsing tap options: %w", err)
}
if err = l.tap(copts); err != nil {
err = fmt.Errorf("tapping on %q: %w", l.selector, err)
return
if err := l.tap(copts); err != nil {
return fmt.Errorf("tapping on %q: %w", l.selector, err)
}

applySlowMo(l.ctx)

return nil
}

func (l *Locator) tap(opts *FrameTapOptions) 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 @@ -190,13 +190,14 @@ func TestLocator(t *testing.T) {
},
},
{
"Tap", func(tb *testBrowser, p *common.Page) {
"Tap", func(_ *testBrowser, p *common.Page) {
result := func() bool {
v := p.Evaluate(`() => window.result`)
return asBool(t, v)
}
require.False(t, result(), "should not be tapped first")
p.Locator("#inputText", nil).Tap(nil)
err := p.Locator("#inputText", nil).Tap(nil)
require.NoError(t, err)
require.True(t, result(), "should be tapped")
},
},
Expand Down Expand Up @@ -332,7 +333,12 @@ func TestLocator(t *testing.T) {
"SelectOption", func(l *common.Locator, tb *testBrowser) { l.SelectOption(tb.toGojaValue(""), timeout(tb)) },
},
{
"Tap", func(l *common.Locator, tb *testBrowser) { l.Tap(timeout(tb)) },
"Tap", func(l *common.Locator, tb *testBrowser) {
if err := l.Tap(timeout(tb)); err != nil {
// TODO: remove panic and update tests when all locator methods return error.
panic(err)
}
},
},
{
"Type", func(l *common.Locator, tb *testBrowser) { l.Type("a", timeout(tb)) },
Expand Down

0 comments on commit 2f3be40

Please sign in to comment.