Skip to content

Commit

Permalink
Refactor locator.Type to return an error
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Apr 22, 2024
1 parent 71027b8 commit ba771ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
19 changes: 9 additions & 10 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,26 +509,25 @@ func (l *Locator) press(key string, opts *FramePressOptions) error {

// Type text on the element found that matches the locator's
// selector with strict mode on.
func (l *Locator) Type(text string, opts goja.Value) {
func (l *Locator) Type(text string, opts goja.Value) error {
l.log.Debugf(
"Locator:Type", "fid:%s furl:%q sel:%q text:%q opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, text, opts,
)
_, span := TraceAPICall(l.ctx, l.frame.page.targetID.String(), "locator.type")
defer span.End()

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

copts := NewFrameTypeOptions(l.frame.defaultTimeout())
if err = copts.Parse(l.ctx, opts); err != nil {
err = fmt.Errorf("parsing type options: %w", err)
return
if err := copts.Parse(l.ctx, opts); err != nil {
return fmt.Errorf("parsing type options: %w", err)
}
if err = l.typ(text, copts); err != nil {
err = fmt.Errorf("typing %q in %q: %w", text, l.selector, err)
return
if err := l.typ(text, copts); err != nil {
return fmt.Errorf("typing %q in %q: %w", text, l.selector, err)
}

applySlowMo(l.ctx)

return nil
}

func (l *Locator) typ(text string, opts *FrameTypeOptions) error {
Expand Down
8 changes: 7 additions & 1 deletion tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,13 @@ func TestLocator(t *testing.T) {
},
},
{
"Type", func(l *common.Locator, tb *testBrowser) { l.Type("a", timeout(tb)) },
"Type", func(l *common.Locator, tb *testBrowser) {
err := l.Type("a", timeout(tb))
if err != nil {
// TODO: remove panic and update tests when all locator methods return error.
panic(err)
}
},
},
{
"TextContent", func(l *common.Locator, tb *testBrowser) { l.TextContent(timeout(tb)) },
Expand Down

0 comments on commit ba771ec

Please sign in to comment.